forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
49 lines (41 loc) · 941 Bytes
/
websocket.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
package socketio
import (
"bytes"
"io"
"time"
"golang.org/x/net/websocket"
)
func init() {
DefaultTransports.RegisterTransport("websocket")
}
type webSocket struct {
session *Session
timeout time.Duration
conn *websocket.Conn
}
func newWebSocket(session *Session) *webSocket {
ret := &webSocket{
session: session,
timeout: session.heartbeatTimeout / 10,
}
session.transport = ret
return ret
}
func (ws *webSocket) Send(data []byte) error {
ws.conn.SetWriteDeadline(time.Now().Add(ws.timeout))
return websocket.Message.Send(ws.conn, string(data))
}
func (ws *webSocket) Read() (io.Reader, error) {
var ret string
ws.conn.SetReadDeadline(time.Now().Add(ws.timeout))
err := websocket.Message.Receive(ws.conn, &ret)
if err != nil {
return nil, err
}
reader := bytes.NewBufferString(ret)
return reader, nil
}
func (ws *webSocket) webSocketHandler(conn *websocket.Conn) {
ws.conn = conn
ws.session.loop()
}