send/receive

This commit is contained in:
andrzej 2024-11-24 16:54:42 +01:00
parent 8ef8de0943
commit 79293483f8
1 changed files with 15 additions and 2 deletions

17
main.go
View File

@ -27,11 +27,24 @@ func (wsh webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("New websocket connection. There are now %v\n", connections) log.Printf("New websocket connection. There are now %v\n", connections)
wg.Add(1) wg.Add(1)
go func() { go func() {
defer connection.Close()
for { for {
msgType, message, err := connection.ReadMessage()
if err != nil {
log.Printf("Error trying to read message from client: %s", err)
return
}
if msgType == websocket.BinaryMessage {
err = connection.WriteMessage(websocket.TextMessage, []byte("this server does not support binary messages"))
if err != nil {
log.Printf("Error trying to send message to client: %s", err)
}
return
}
log.Printf("received message from client: %s", message)
err = connection.WriteMessage(websocket.TextMessage, []byte("Message received!"))
} }
}() }()
defer connection.Close()
} }
func main() { func main() {