-
Notifications
You must be signed in to change notification settings - Fork 12
/
connection.go
116 lines (96 loc) · 2.23 KB
/
connection.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
package goatee
import (
"encoding/json"
"log"
"time"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 1024
)
type WSClient struct {
Channel string `json:"channel"`
Action string `json:"action"`
Date string `json:"date"`
Payload string `json:"payload"`
Token string `json:"token"`
}
type connection struct {
sid string
ws *websocket.Conn
send chan *Data
client WSClient
}
func (c *connection) writer() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.ws.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.ws.WriteMessage(websocket.CloseMessage, []byte{})
return
}
if c.client.Channel == message.Channel {
err := c.ws.WriteJSON(message.Payload)
if err != nil {
log.Printf("Error in writer: %s", err.Error())
h.unregister <- c
break
}
}
case <-ticker.C:
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.ws.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}
func (c *connection) reader() {
h.rclient.Lock()
defer func() {
h.unregister <- c
h.rclient.Unlock()
c.ws.Close()
}()
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
var wclient WSClient
err := c.ws.ReadJSON(&wclient)
if err != nil {
break
}
if DEBUG {
log.Println("client type is:", wclient)
}
c.client = wclient
switch wclient.Action {
case "bind":
h.rclient.Subscribe(wclient.Channel)
case "unbind":
h.rclient.Unsubscribe(wclient.Channel)
case "message":
d, err := json.Marshal(wclient)
if err != nil {
log.Println("Error marsahling json for publish: ", err)
}
_, err = h.rconn.Do("PUBLISH", wclient.Channel, d)
if err != nil {
log.Println("Error publishing message: ", err)
}
}
}
}