mchess-server/server/chess.go

50 lines
844 B
Go

package server
import (
"log"
"math/rand"
"github.com/gorilla/websocket"
)
type chessGame struct {
id int
}
func NewChessGame() *chessGame {
var game chessGame = chessGame{
id: rand.Int(),
}
return &game
}
func (game *chessGame) handle(conn *websocket.Conn) {
for {
messageType, receivedMessage, err := conn.ReadMessage()
if err != nil {
log.Println("Error during message reading:", err)
break
}
move, err := parseMove(string(receivedMessage))
if err != nil {
log.Println("Game: ", game.id, err)
return
}
log.Println("Game ", game.id, " move: ", move)
messageToBeSent := moveToString(*move)
log.Println("About to send: ", messageToBeSent)
err = conn.WriteMessage(messageType, []byte(messageToBeSent))
if err != nil {
log.Println("Error during message writing:", err)
break
}
}
}