forked from KlairSecure/websocket-server-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
97 lines (77 loc) · 2.15 KB
/
server.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Allow any origin for demonstration purposes. In production, you should perform proper origin checks.
return true
},
}
func handlePing(ws *websocket.Conn) {
defer ws.Close()
for {
// Read a message from the WebSocket connection
_, msg, err := ws.ReadMessage()
if err != nil {
fmt.Println("Error reading message:", err)
return
}
fmt.Println(string(msg))
// Respond with an "ok" message for any received message
err = ws.WriteMessage(websocket.TextMessage, []byte("pong"))
if err != nil {
fmt.Println("Error writing 'pong' message:", err)
return
}
}
}
func sendOkMessagesToClient(ws *websocket.Conn) {
for {
// time.Sleep(time.Second) // Send "ok" messages every seconds
err := ws.WriteMessage(websocket.TextMessage, []byte("ok"))
if err != nil {
fmt.Println("Error writing 'ok' message:", err)
return
}
}
}
func sendOkMessagesToClients(conns []*websocket.Conn) {
for {
time.Sleep(time.Second) // Send "ok" messages every second
for _, ws := range conns {
err := ws.WriteMessage(websocket.TextMessage, []byte("ok"))
if err != nil {
fmt.Println("Error writing 'ok' message:")
}
}
}
}
func main() {
var connections []*websocket.Conn
http.HandleFunc("/ws", func(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
}
fmt.Println("Client Connected")
connections = append(connections, ws)
// Handle WebSocket "ping" messages
go handlePing(ws)
// Start a goroutine to send "ok" messages to all connected clients
// go sendOkMessagesToClients(connections)
// Start a goroutine to send "ok" messages to a new client
// go sendOkMessagesToClient(ws)
})
// render html
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
fmt.Println("WebSocket server listening on :8080")
http.ListenAndServe(":8080", nil)
}