-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
76 lines (63 loc) · 1.33 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
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
package main
import (
"sync"
"golang.org/x/net/websocket"
)
type WsService struct {
c *Context
mutex *sync.Mutex
cons []*WsConnection
}
type WsConnection struct {
id string
con *websocket.Conn
msg chan string
}
func NewWebSocketService(c *Context) *WsService {
w := &WsService{c: c, mutex: &sync.Mutex{}}
c.wsService = w
return w
}
func (w *WsService) HandleWebSocket(con *websocket.Conn) {
wsCon := &WsConnection{con: con, msg: make(chan string), id: randomToken(8)}
w.mutex.Lock()
w.cons = append(w.cons, wsCon)
w.mutex.Unlock()
defer con.Close()
for {
msg := <-wsCon.msg
if err := websocket.Message.Send(con, msg); err != nil {
w.removeFromSlice(wsCon)
break
}
}
}
func (w *WsService) Broadcast(msg string) {
for _, wsCon := range w.cons {
wsCon.msg <- msg
}
}
func (w *WsService) removeFromSlice(wsCon *WsConnection) {
w.mutex.Lock()
if index := w.indexOf(wsCon); index >= 0 {
if len(w.cons) == 1 {
w.cons = w.cons[:0]
} else if index+1 == len(w.cons) {
w.cons = w.cons[:index]
} else {
w.cons = append(w.cons[:index], w.cons[index+1])
}
}
w.mutex.Unlock()
}
func (w *WsService) indexOf(wsCon *WsConnection) int {
for i, con := range w.cons {
if wsCon.Equals(con) {
return i
}
}
return -1
}
func (w *WsConnection) Equals(other *WsConnection) bool {
return w.id == other.id
}