-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
51 lines (43 loc) · 1.11 KB
/
websocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"github.com/gorilla/websocket"
"net/http"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
// Upgrade the HTTP connection to a WebSocket connection
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Error upgrading to websocket:", err)
return
}
defer ws.Close()
for {
// Read message from WebSocket
messageType, msg, err := ws.ReadMessage()
if err != nil {
fmt.Println("Error reading message:", err)
break
}
// Process the received message (here we just print it)
fmt.Printf("Received: %s\n", msg)
// Send a response back to the client
responseMsg := fmt.Sprintf("Server received: %s", msg)
if err := ws.WriteMessage(messageType, []byte(responseMsg)); err != nil {
fmt.Println("Error writing message:", err)
break
}
}
}
func main() {
http.HandleFunc("/ws", handleWebSocket)
fmt.Println("Server started at :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}