-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.go
79 lines (61 loc) · 1.89 KB
/
ws.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
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/lxzan/gws"
)
// react socket handler
type WebSocketHandler struct{}
func (h *WebSocketHandler) OnOpen(conn *gws.Conn) {
log.Println("Connection opened")
}
func (h *WebSocketHandler) OnMessage(conn *gws.Conn, message *gws.Message) {
log.Println("Received message:", string(message.Data.String()))
err := conn.WriteMessage(gws.OpcodeText, []byte("Hello, World!"))
if err != nil {
log.Println("Write error:", err)
}
}
func (h *WebSocketHandler) OnClose(conn *gws.Conn, err error) {
log.Println("Connection closed:", err)
}
func (c *WebSocketHandler) OnPing(socket *gws.Conn, payload []byte) {
_ = socket.SetDeadline(time.Now().Add(PingInterval + PingWait))
_ = socket.WritePong(nil)
}
func (c *WebSocketHandler) OnPong(socket *gws.Conn, payload []byte) {}
const (
PingInterval = 5 * time.Second
PingWait = 10 * time.Second
)
// got reciver socket handler
type LogReceiverSocketHandler struct{}
func (h *LogReceiverSocketHandler) OnOpen(conn *gws.Conn) {
log.Println("Connection opened")
}
// I WANT MAX VALUE FOR sub.key1 OVER 1 MINUTE.
func (h *LogReceiverSocketHandler) OnMessage(conn *gws.Conn, message *gws.Message) {
msg := make(map[string]interface{})
err := json.Unmarshal(message.Data.Bytes(), &msg)
_, ok := msg["sub"]
if ok {
sub, ok := msg["sub"].(map[string]interface{})
if ok {
fmt.Println("KEY1:", sub["key1"])
}
}
err = conn.WriteMessage(gws.OpcodeText, []byte("Hello, World!"))
if err != nil {
log.Println("Write error:", err)
}
}
func (h *LogReceiverSocketHandler) OnClose(conn *gws.Conn, err error) {
log.Println("Connection closed:", err)
}
func (c *LogReceiverSocketHandler) OnPing(socket *gws.Conn, payload []byte) {
_ = socket.SetDeadline(time.Now().Add(PingInterval + PingWait))
_ = socket.WritePong(nil)
}
func (c *LogReceiverSocketHandler) OnPong(socket *gws.Conn, payload []byte) {}