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
|
|
|
)
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
var AppPath = "/home/m/projects/programming/flutter_projects/mchess/build/web"
|
|
|
|
|
2022-12-14 21:19:47 +00:00
|
|
|
var player_number = 0
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
var upgrader = websocket.Upgrader{}
|
2022-10-22 18:01:55 +00:00
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
func PlayHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-19 12:25:00 +00:00
|
|
|
log.Println("SocketHandler invoked")
|
2023-04-18 20:12:05 +00:00
|
|
|
w.Write([]byte("Hello, this is your websocket connection speaking."))
|
2022-12-18 00:41:44 +00:00
|
|
|
// Allow connections from any origin
|
|
|
|
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
_, 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-12-13 02:33:05 +00:00
|
|
|
|
2022-10-22 18:01:55 +00:00
|
|
|
}
|
2022-12-18 00:05:23 +00:00
|
|
|
|
|
|
|
func WriteMessageToPlayer(player *Player, msg []byte, msgType int) error {
|
|
|
|
log.Printf("Writing message: %s (with messagetype %d) to player %d", string(msg), msgType, player.id)
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
return player.conn.WriteMessage(msgType, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadMessageFromPlayer(player *Player) (messageType int, p []byte, err error) {
|
|
|
|
msgType, msg, err := player.conn.ReadMessage()
|
|
|
|
log.Printf("Reading message: %s (with messagetype %d) from player %d", string(msg), msgType, player.id)
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2022-12-18 00:05:23 +00:00
|
|
|
return msgType, msg, err
|
|
|
|
}
|