/
socket.go
151 lines (123 loc) · 3.36 KB
/
socket.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
package gobwas
import (
"io"
"io/ioutil"
"net"
"net/http"
"sync"
"time"
"github.com/kataras/neffos"
gobwas "github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
)
// Socket completes the `neffos.Socket` interface,
// it describes the underline websocket connection.
type Socket struct {
UnderlyingConn net.Conn
request *http.Request
reader *wsutil.Reader
controlHandler wsutil.FrameHandlerFunc
state gobwas.State
mu sync.Mutex
}
func newSocket(underline net.Conn, request *http.Request, client bool) *Socket {
state := gobwas.StateServerSide
if client {
state = gobwas.StateClientSide
}
controlHandler := wsutil.ControlFrameHandler(underline, state)
reader := &wsutil.Reader{
Source: underline,
State: state,
CheckUTF8: true,
SkipHeaderCheck: false,
// "intermediate" frames, that possibly could
// be received between text/binary continuation frames.
// Read `gobwas/wsutil/reader#NextReader`.
//
OnIntermediate: controlHandler,
}
return &Socket{
UnderlyingConn: underline,
request: request,
state: state,
reader: reader,
controlHandler: controlHandler,
}
}
// NetConn returns the underline net connection.
func (s *Socket) NetConn() net.Conn {
return s.UnderlyingConn
}
// Request returns the http request value.
func (s *Socket) Request() *http.Request {
return s.request
}
// ReadData reads binary or text messages from the remote connection.
func (s *Socket) ReadData(timeout time.Duration) ([]byte, neffos.MessageType, error) {
for {
if timeout > 0 {
s.UnderlyingConn.SetReadDeadline(time.Now().Add(timeout))
}
hdr, err := s.reader.NextFrame()
if err != nil {
if err == io.EOF {
return nil, 0, io.ErrUnexpectedEOF // for io.ReadAll to return an error if connection remotely closed.
}
return nil, 0, err
}
if hdr.OpCode == gobwas.OpClose {
return nil, 0, io.ErrUnexpectedEOF // for io.ReadAll to return an error if connection remotely closed.
}
if hdr.OpCode.IsControl() {
err = s.controlHandler(hdr, s.reader)
if err != nil {
return nil, 0, err
}
continue
}
if hdr.OpCode&gobwas.OpBinary == 0 && hdr.OpCode&gobwas.OpText == 0 {
err = s.reader.Discard()
if err != nil {
return nil, 0, err
}
continue
}
b, err := ioutil.ReadAll(s.reader)
if err != nil {
return nil, 0, err
}
return b, neffos.MessageType(hdr.OpCode), nil
}
// for {
// if timeout > 0 {
// s.UnderlyingConn.SetReadDeadline(time.Now().Add(timeout))
// }
// b, code, err := wsutil.ReadData(s.UnderlyingConn, s.state)
// if err != nil {
// return nil, err
// }
// if code != defaultOp {
// continue
// }
// return b, nil
// }
}
// WriteBinary sends a binary message to the remote connection.
func (s *Socket) WriteBinary(body []byte, timeout time.Duration) error {
return s.write(body, gobwas.OpBinary, timeout)
}
// WriteText sends a text message to the remote connection.
func (s *Socket) WriteText(body []byte, timeout time.Duration) error {
return s.write(body, gobwas.OpText, timeout)
}
func (s *Socket) write(body []byte, op gobwas.OpCode, timeout time.Duration) error {
s.mu.Lock()
if timeout > 0 {
s.UnderlyingConn.SetWriteDeadline(time.Now().Add(timeout))
}
// println("write: " + string(body))
err := wsutil.WriteMessage(s.UnderlyingConn, s.state, op, body)
s.mu.Unlock()
return err
}