-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
153 lines (134 loc) · 3.13 KB
/
conn.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
// Copyright 2013 Gary Burd. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wssf
import (
"errors"
"time"
"github.com/gorilla/websocket"
)
type wattingMsg struct {
mt int
data []byte
}
type timerNotify struct {
fn func()
}
type receiveNotify struct {
mt int
data []byte
err error
}
type sendErrNotify struct {
err error
}
// Connection is an middleman between the websocket Connection and the hub.
type Connection struct {
// The websocket Connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan wattingMsg
handler ConnectionHandler
notifyChan chan interface{}
}
//notify this connection something
//concurrent safe
func (c *Connection) Notify(v interface{}) {
c.notifyChan <- v
}
//send message to peer, mt: wssf.TextMessage or wssf.BinaryMessage
//concurrent safe
func (c *Connection) Send(mt int, data []byte) {
m := wattingMsg{mt, data}
c.send <- m
}
//add a timer function, called from same goroutine with Handler.OnReceived()
//to stop timer, use returned channel
//for example: rc := c.AddTimer(d, fn)
//rc <- true
func (c *Connection) AddTimer(d time.Duration, fn func()) chan bool {
stop := make(chan bool)
go func() {
select {
case <-time.After(d):
c.Notify(timerNotify{fn})
case <-stop: //cancel timer
}
}()
return stop
}
// readPump pumps messages from the websocket Connection to the hub.
func (c *Connection) readPump() {
defer func() {
c.handler.OnDisconnected(c)
h.unregister <- c
c.ws.Close()
}()
c.ws.SetReadLimit(Config.MaxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(Config.PongWait))
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(Config.PongWait)); return nil })
go func() {
for {
mt, data, err := c.ws.ReadMessage()
c.Notify(receiveNotify{mt, data, err})
if err != nil {
break
}
}
}()
L:
for {
v := <-c.notifyChan
switch r := v.(type) {
case timerNotify:
r.fn()
case receiveNotify:
if r.err != nil {
c.handler.OnError(r.err)
break L
}
if !c.handler.OnReceived(c, r.mt, r.data) {
break L
}
case sendErrNotify:
if r.err != nil {
c.handler.OnError(r.err)
break L
}
default:
c.handler.OnNotify(v)
}
}
}
// write writes a message with the given message type and payload.
func (c *Connection) write(mt int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(Config.WriteWait))
return c.ws.WriteMessage(mt, payload)
}
// writePump pumps messages from the hub to the websocket Connection.
func (c *Connection) writePump() {
ticker := time.NewTicker(Config.PingPeriod)
defer func() {
ticker.Stop()
c.ws.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.write(websocket.CloseMessage, []byte{})
c.Notify(sendErrNotify{errors.New("receive msg from send channel failed")})
return
}
if err := c.write(message.mt, message.data); err != nil {
c.Notify(sendErrNotify{err})
return
}
case <-ticker.C:
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
c.Notify(sendErrNotify{err})
return
}
}
}
}