/
server.go
42 lines (36 loc) · 837 Bytes
/
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
package ws
import (
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"net/http"
"time"
)
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
maxMessageSize = 512
)
var (
newline = []byte{'\n'}
space = []byte{' '}
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logrus.Errorln(err)
return
}
id := uuid.New().String()
logrus.Infoln("New client: ", id)
client := &Client{hub: hub, ClientID: id, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client
client.unSub = hub.replier(client)
go client.writePump()
go client.readPump()
}