package engine import ( "log" "net/http" "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{} // use default options func serve() { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { serveWs(w, r) }) err := http.ListenAndServe(":7636", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } func serveWs(w http.ResponseWriter, r *http.Request) { // Upgrade our raw HTTP connection to a websocket based one conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("Error during connection upgradation:", err) return } defer conn.Close() // The event loop for { var cmd Command var resp CommandResult err := conn.ReadJSON(&cmd) if err != nil { log.Println("Error during message reading:", err) break } log.Printf("Received: %s", cmd) err = conn.WriteJSON(resp) if err != nil { log.Println("Error during message writing:", err) break } } }