35 lines
683 B
Go
35 lines
683 B
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var upgrader = websocket.Upgrader{} // use default options
|
|
|
|
func SocketHandler(w http.ResponseWriter, r *http.Request) {
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Print("Error during connection upgrading:", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
// The event loop
|
|
for {
|
|
messageType, message, err := conn.ReadMessage()
|
|
if err != nil {
|
|
log.Println("Error during message reading:", err)
|
|
break
|
|
}
|
|
log.Printf("Received: %s", message)
|
|
err = conn.WriteMessage(messageType, message)
|
|
if err != nil {
|
|
log.Println("Error during message writing:", err)
|
|
break
|
|
}
|
|
}
|
|
}
|