This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wshandler.go
235 lines (202 loc) · 5.53 KB
/
wshandler.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package kwiscale
import "github.com/gorilla/websocket"
var (
// keep connection by path.
rooms = make(map[string]*wsroom, 0)
)
type wsroom struct {
// connections for the room
conns map[*WebSocketHandler]bool
}
// Returns the room named "path", or create one if not exists.
func getRoom(path string) *wsroom {
if r, ok := rooms[path]; !ok {
r = new(wsroom)
r.conns = make(map[*WebSocketHandler]bool)
rooms[path] = r
}
return rooms[path]
}
// Add a websocket handler to the room.
func (room *wsroom) add(c *WebSocketHandler) {
room.conns[c] = true
}
// Remove a websocket handler from the room.
func (room *wsroom) remove(c *WebSocketHandler) {
if _, ok := room.conns[c]; ok {
Log("Remove websocket connection", c)
delete(room.conns, c)
}
}
// WSHandler is the base interface to implement to be able to use
// Websocket.
type WSHandler interface {
// Serve is the method to implement inside the project
upgrade() error
OnConnect() error
OnClose() error
GetConnection() *websocket.Conn
Close()
}
// WebSocketHandler type to compose a web socket handler.
// To use it, compose a handler with this type and implement one of
// OnJSON(), OnMessage() or Serve() method.
// Example:
//
// type Example_WebSocketHandler struct{ WebSocketHandler }
//
// func (m *Example_WebSocketHandler) OnJSON(i interface{}, err error) {
// if err != nil {
// m.SendJSON(map[string]string{
// "error": err.Error(),
// })
// return
// }
//
// m.SendJSON(map[string]interface{}{
// "greeting": "Hello !",
// "data": i,
// })
// }
//
// Previous example send back the message + a greeting message
type WebSocketHandler struct {
BaseHandler
conn *websocket.Conn
}
// upgrade protocol to use websocket communication
func (ws *WebSocketHandler) upgrade() error {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
var err error
ws.conn, err = upgrader.Upgrade(ws.response, ws.request, nil)
if err == nil {
// record room and append connection
path := ws.request.URL.Path
room := getRoom(path)
room.add(ws)
}
return err
}
// GetConnection returns the websocket client connection.
func (ws *WebSocketHandler) GetConnection() *websocket.Conn {
return ws.conn
}
// OnConnect is called when a client connection is opened.
func (ws *WebSocketHandler) OnConnect() error {
return nil
}
// OnClose is called when a client connection is closed.
func (ws *WebSocketHandler) OnClose() error {
return nil
}
func (ws *WebSocketHandler) Write(b []byte) error {
return ws.conn.WriteMessage(websocket.TextMessage, b)
}
// WriteString is an alias to SendText.
func (ws *WebSocketHandler) WriteString(m string) error {
return ws.SendText(m)
}
// WriteJSON is an alias for SendJSON.
func (ws *WebSocketHandler) WriteJSON(i interface{}) error {
return ws.SendJSON(i)
}
// SendJSON send interface "i" in json form to the current client.
func (ws *WebSocketHandler) SendJSON(i interface{}) error {
return ws.conn.WriteJSON(i)
}
// SendText send string "s" to the current client.
func (ws *WebSocketHandler) SendText(s string) error {
return ws.conn.WriteMessage(websocket.TextMessage, []byte(s))
}
// SendJSONToThisRoom send interface "i" in json form to the client connected
// to the same room of the current client connection.
func (ws *WebSocketHandler) SendJSONToThisRoom(i interface{}) {
ws.SendJSONToRoom(ws.request.URL.Path, i)
}
// SendJSONToRoom send the interface "i" in json form to the client connected
// to the the room named "name".
func (ws *WebSocketHandler) SendJSONToRoom(room string, i interface{}) {
for w := range rooms[room].conns {
w.SendJSON(i)
}
}
// SendJSONToAll send the interface "i" in json form to the entire
// client list.
func (ws *WebSocketHandler) SendJSONToAll(i interface{}) {
for name := range rooms {
ws.SendJSONToRoom(name, i)
}
}
// SendTextToThisRoom send message s to the room of the
// current client connection.
func (ws *WebSocketHandler) SendTextToThisRoom(s string) {
ws.SendTextToRoom(ws.request.URL.Path, s)
}
// SendTextToRoom send message "s" to the room named "name".
func (ws *WebSocketHandler) SendTextToRoom(name, s string) {
for w := range rooms[name].conns {
w.SendText(s)
}
}
// SendTextToAll send message "s" to the entire list of connected clients.
func (ws *WebSocketHandler) SendTextToAll(s string) {
for name := range rooms {
ws.SendTextToRoom(name, s)
}
}
// Close connection after having removed handler from the rooms stack.
func (ws *WebSocketHandler) Close() {
defer ws.conn.Close()
if room, ok := rooms[ws.request.URL.Path]; ok {
room.remove(ws)
if len(room.conns) == 0 {
delete(rooms, ws.request.URL.Path)
}
}
}
// WSServerHandler interface to serve continuously.
type WSServerHandler interface {
Serve()
}
// WSJsonHandler interface, framework will
// read socket and call OnJSON each time a json message is received.
type WSJsonHandler interface {
OnJSON(interface{}, error)
}
// WSStringHandler interface, framework
// will read socket and call OnMessage() each time a string is received.
type WSStringHandler interface {
OnMessage(int, string, error)
}
func serveWS(w WSHandler) {
defer w.Close()
w.(WSServerHandler).Serve()
}
// Serve JSON.
func serveJSON(w WSHandler) {
c := w.GetConnection()
defer w.Close()
for {
var i interface{}
err := c.ReadJSON(&i)
w.(WSJsonHandler).OnJSON(i, err)
if err != nil {
return
}
}
}
// Serve string messages
func serveString(w WSHandler) {
c := w.GetConnection()
defer w.Close()
for {
i, p, err := c.ReadMessage()
w.(WSStringHandler).OnMessage(i, string(p), err)
if err != nil {
return
}
}
}