/
connection.go
139 lines (115 loc) · 2.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package exapi
import (
"errors"
"github.com/gorilla/websocket"
"net/http"
"net/http/httputil"
"net/url"
"sync"
)
// 封装websocket连接,可双工读写
type Connection struct {
// 互斥锁
sync.Mutex
// 存放websocket连接
wsConn *websocket.Conn
// 是否已关闭
isClosed bool
// 消息处理函数,当有数据时调用,当断开连接时,data传nil
OnMessage func([]byte) error
}
func connect(wsurl, proxyurl string) (wsConn *websocket.Conn, err error) {
dialer := websocket.DefaultDialer
if proxyurl != "" {
proxy, err := url.Parse(proxyurl)
if err == nil {
dialer.Proxy = http.ProxyURL(proxy)
} else {
return nil, err
}
}
wsConn, resp, err := dialer.Dial(wsurl, nil)
if err != nil {
if resp != nil {
dumpData, _ := httputil.DumpResponse(resp, true)
Error("[ws][%s] websocket dump connect:%s", wsurl, string(dumpData))
}
return nil, err
}
return wsConn, nil
}
// 初始化长连接
func NewConnectionWithURL(wsurl, proxyurl string, onMessage func([]byte) error) (conn *Connection, err error) {
wsConn, err := connect(wsurl, proxyurl)
if err != nil {
return nil, err
}
return NewConnection(wsConn, onMessage)
}
// 初始化长连接
func NewConnection(wsConn *websocket.Conn, onMessage func([]byte) error) (conn *Connection, err error) {
conn = &Connection{
wsConn: wsConn,
isClosed: false,
OnMessage: onMessage,
}
// 启动读协程
go conn.readLoop()
return
}
// 发送数据
func (conn *Connection) SendMessage(data []byte) (err error) {
if conn.isClosed {
return errors.New("Connection is closed")
}
// 上锁,发送数据加锁
conn.Lock()
defer conn.Unlock()
if err = conn.wsConn.WriteMessage(websocket.TextMessage, data); err != nil {
conn.close()
}
return err
}
// 接收数据
func (conn *Connection) ReceiveMessage() (data []byte, err error) {
if conn.isClosed {
return nil, errors.New("Connection is closed")
}
_, data, err = conn.wsConn.ReadMessage()
if err != nil {
Error("[ws] websocket ReadMessage failed:%v", err)
conn.Close()
}
return data, err
}
// 关闭连接
func (conn *Connection) Close() {
conn.Lock()
defer conn.Unlock()
conn.close()
}
// 关闭
func (conn *Connection) close() {
if !conn.isClosed {
conn.wsConn.Close()
conn.isClosed = true
if conn.OnMessage != nil {
conn.OnMessage(nil) // 发送断开消息
}
}
}
// 循环读取数据
func (conn *Connection) readLoop() {
for {
var data []byte
var err error
if _, data, err = conn.wsConn.ReadMessage(); err != nil {
Error("[ws] websocket ReadMessage failed:%v", err)
conn.Close()
break
}
if conn.OnMessage != nil {
conn.OnMessage(data)
}
}
}