Exemplo n.º 1
0
func serverWS(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	c, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		tools.LOG_ERROR.Print("upgrade:", err)
		return
	}

	newServerConnection := new(serverConnection)
	newServerConnection.server = c
	newServerConnection.clients = make(map[string]*websocket.Conn)
	newServerConnection.config = nil

	serverConnections[id] = newServerConnection

	defer func() {
		c.Close()
		//TODO notify all the clients + cleanup everyting correctly
		delete(serverConnections, id)
	}()

	for {
		mt, message, err := c.ReadMessage()
		if err != nil {
			tools.LOG_ERROR.Println("read:", err)
			break
		}
		tools.LOG_DEBUG.Printf("Server recv: %s", message)
		mfs, err := types.MessageFromString(string(message))
		if nil != err {
			tools.LOG_ERROR.Println("Invalid json received error "+string(message), err)
			continue
		}
		if 0 == mfs.Type {
			// Forward message... This is a valid json but not correct message type
			for _, client := range newServerConnection.clients {
				// Writing to client
				tools.LOG_DEBUG.Println("Writing to client")
				client.WriteMessage(mt, message)
			}
		}

		if mfs.Type == types.EnumMessageControl {
			if mfs.SubType == types.EnumControlNewInstance {
				nic, err := types.NewInstanceConfigFromString(mfs.Message)
				if nil != err {
					tools.LOG_ERROR.Println("Error deserializing new Instance ", err)
					continue
				}
				//Check for configuration
				newServerConnection.config = nic
			}
		} else if mfs.Type == types.EnumMessageMove {
			if 0 == len(newServerConnection.clients) {
				tools.LOG_DEBUG.Println("Client is still empty")
				continue
			}
			for _, client := range newServerConnection.clients {
				client.WriteMessage(mt, message)
			}
		} else {
			tools.LOG_ERROR.Println("Invalid type")
		}

	}
}
Exemplo n.º 2
0
func clientWS(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]
	subid := vars["subid"]

	c, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		tools.LOG_ERROR.Print("upgrade:", err)
		return
	}
	serverSocket, found := serverConnections[id]
	if !found {
		tools.LOG_ERROR.Println("Couldn't find server side")
		c.Close()
		return
	}
	defer func() {
		c.Close()
		//TODO notify all the clients + cleanup everyting correctly
		delete(serverSocket.clients, subid)
	}()

	if nil == serverSocket.config {
		tools.LOG_ERROR.Println("Opening connection on an empty configuration. Closing connection")
		return
	}

	if 0 != len(serverSocket.clients) && !serverSocket.config.Multiplayer {
		_, _, _ = c.ReadMessage()
		c.WriteMessage(0, []byte("Someone is already there"))
		return
	}
	serverSocket.clients[subid] = c
	//Notify Server that the client arrived

	newClientMessage := types.Message{
		Type:    types.EnumMessageControl,
		SubType: types.EnumControlStartClientSession,
		Message: subid,
	}
	ncmj, err := newClientMessage.ToString()
	if nil != err {
		tools.LOG_ERROR.Println("Failed to serialize message ", err)
		return
	}
	serverSocket.server.WriteMessage(1, []byte(ncmj))

	for {
		mt, message, err := c.ReadMessage()
		if err != nil {
			tools.LOG_ERROR.Println("read:", err)
			break
		}
		newMssage, err := types.MessageFromString(string(message))
		if nil != err {
			tools.LOG_ERROR.Println("Failed to deserialize message " + string(message) + " " + err.Error())
			continue
		}
		if 0 == newMssage.Type {
			// Invalid message type forward to server
			serverSocket.server.WriteMessage(mt, message)
			continue
		}
		if !serverSocket.config.Websocket && newMssage.Type == types.EnumMessageControl {
			tools.LOG_ERROR.Println("Received move message from client but this should be the case...")
			continue
		}

		newMssage.ClientId = subid
		m, err := newMssage.ToString()
		if nil != err {
			tools.LOG_ERROR.Println("Couldn't serialize message ", err)
			continue
		}
		tools.LOG_DEBUG.Println("Client " + subid + " send :" + m)
		serverSocket.server.WriteMessage(mt, []byte(m))
		//tools.LOG_DEBUG.Printf("recv: %s", message)
	}
}