mchess-server/server/server.go

39 lines
891 B
Go
Raw Normal View History

2022-10-22 18:01:55 +00:00
package server
import (
"log"
2022-11-19 10:41:10 +00:00
"net/http"
2022-10-22 18:01:55 +00:00
2022-11-19 10:41:10 +00:00
"github.com/gorilla/websocket"
2022-10-22 18:01:55 +00:00
)
2022-11-19 10:41:10 +00:00
var upgrader = websocket.Upgrader{} // use default options
2022-10-22 18:01:55 +00:00
2022-11-19 10:41:10 +00:00
func SocketHandler(w http.ResponseWriter, r *http.Request) {
2022-11-19 12:25:00 +00:00
log.Println("SocketHandler invoked")
2022-11-19 10:41:10 +00:00
conn, err := upgrader.Upgrade(w, r, nil)
2022-10-22 18:01:55 +00:00
if err != nil {
2022-11-19 10:41:10 +00:00
log.Print("Error during connection upgrading:", err)
return
2022-10-22 18:01:55 +00:00
}
2022-11-19 10:41:10 +00:00
defer conn.Close()
2022-12-13 02:33:05 +00:00
defer log.Println("SocketHandler exited")
// Check if first message is the passphrase
msg_type, msg, err := conn.ReadMessage()
if err != nil {
return
}
if string(msg) != "pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM" {
conn.WriteMessage(msg_type, []byte("Wrong password. Connection refused"))
return
} else {
conn.WriteMessage(msg_type, []byte("Password correct. Let's play"))
}
2022-10-22 18:01:55 +00:00
2022-11-19 12:25:00 +00:00
var game = NewChessGame()
game.handle(conn)
2022-10-22 18:01:55 +00:00
}