forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsconn.go
46 lines (38 loc) · 1.17 KB
/
wsconn.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
package jsonrpc
import (
"sync"
"github.com/gorilla/websocket"
)
// concurrentWsConn is a wrapped web socket connection
// that provide methods to deal with concurrency
type concurrentWsConn struct {
wsConn *websocket.Conn
mutex *sync.Mutex
}
// NewConcurrentWsConn creates a new instance of concurrentWsConn
func newConcurrentWsConn(wsConn *websocket.Conn) *concurrentWsConn {
return &concurrentWsConn{
wsConn: wsConn,
mutex: &sync.Mutex{},
}
}
// ReadMessage reads a message from the inner web socket connection
func (c *concurrentWsConn) ReadMessage() (messageType int, p []byte, err error) {
return c.wsConn.ReadMessage()
}
// WriteMessage writes a message to the inner web socket connection
func (c *concurrentWsConn) WriteMessage(messageType int, data []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.wsConn.WriteMessage(messageType, data)
}
// Close closes the inner web socket connection
func (c *concurrentWsConn) Close() error {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.wsConn.Close()
}
// SetReadLimit sets the read limit to the inner web socket connection
func (c *concurrentWsConn) SetReadLimit(limit int64) {
c.wsConn.SetReadLimit(limit)
}