/
websocket.go
67 lines (55 loc) · 1.3 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2018 The Grumble Authors
// The use of this source code is governed by a BSD-style
// license that can be found in the LICENSE-file.
package web
import (
"bytes"
"io"
"net"
"time"
"github.com/gorilla/websocket"
)
type conn struct {
ws *websocket.Conn
msgbuf bytes.Buffer
}
func (c *conn) Read(b []byte) (n int, err error) {
if c.msgbuf.Len() == 0 {
_, r, err := c.ws.NextReader()
if err != nil {
if _, ok := err.(*websocket.CloseError); ok {
return 0, io.EOF
}
return 0, err
}
if _, err := c.msgbuf.ReadFrom(r); err != nil {
return 0, err
}
}
// Impossible to read over message boundaries - will generate EOF
return c.msgbuf.Read(b)
}
func (c *conn) Write(b []byte) (n int, err error) {
return len(b), c.ws.WriteMessage(websocket.BinaryMessage, b)
}
func (c *conn) Close() error {
return c.ws.Close()
}
func (c *conn) LocalAddr() net.Addr {
return c.ws.LocalAddr()
}
func (c *conn) RemoteAddr() net.Addr {
return c.ws.RemoteAddr()
}
func (c *conn) SetDeadline(t time.Time) (err error) {
if err = c.ws.SetReadDeadline(t); err != nil {
return err
}
return c.ws.SetWriteDeadline(t)
}
func (c *conn) SetReadDeadline(t time.Time) error {
return c.ws.SetReadDeadline(t)
}
func (c *conn) SetWriteDeadline(t time.Time) error {
return c.ws.SetWriteDeadline(t)
}