Esempio n. 1
0
/* Initialize a websocket connection and pair it with the handshaking player */
func PlayerConnect(ws *websocket.Conn) {
	websocketConnection := struct {
		CommandCode int
		Player      int
	}{}

	var message string
	if err := websocket.Message.Receive(ws, &message); err != nil {
		log.Fatal("Reading Socket Error:", err)
	} else {
		err := json.Unmarshal([]byte(message), &websocketConnection)
		if err != nil {
			log.Fatal("Decoding Message Error:", err)
		} else {
			log.Printf("WebSocket Connected: %+v", websocketConnection)
			p := GAME.GetPlayer(websocketConnection.Player)
			p.SetWebSocketConnection(ws)
			p.SendMessage(serializable.NewGameInfo().Json())
			PlayerKeepReading(p, ws)
		}
	}
}
Esempio n. 2
0
/* Initialize a spectator connection; disconnected from players */
func SpectatorConnect(ws *websocket.Conn) {
	var message string
	if err := websocket.Message.Receive(ws, &message); err != nil {
		log.Fatal("Reading Socket Error:", err)
	} else {
		spec := spectator.New()
		spec.SetWebSocketConnection(ws)
		spec.SendMessage(serializable.NewGameInfo().Json())
		GAME.AddSpectator(spec)
		log.Println("Adding board spectator")

		/* Keep reading */
		for {
			if websocket.Message.Receive(ws, &message) != nil {
				break
			}
		}
	}

	/* TODO should probably remove spectators from the game, or limit how many
	 * can be added */
	log.Printf("Closing spectator websocket")
	ws.Close()
}