forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
65 lines (58 loc) · 1.11 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
package ws
import (
"github.com/gorilla/websocket"
"io"
"log"
"time"
)
// Wrap an HTTP2 connection over WebSockets and
// use the underlying WebSocket framing for proxy
// compatibility.
type Conn struct {
*websocket.Conn
reader io.Reader
}
func NewConnection(w *websocket.Conn) *Conn {
return &Conn{Conn: w}
}
func (c Conn) Write(b []byte) (int, error) {
err := c.WriteMessage(websocket.BinaryMessage, b)
if err != nil {
return 0, err
}
return len(b), nil
}
func (c Conn) Read(b []byte) (int, error) {
if c.reader == nil {
t, r, err := c.NextReader()
if err != nil {
return 0, err
}
if t != websocket.BinaryMessage {
log.Printf("ws: ignored non-binary message in stream")
return 0, nil
}
c.reader = r
}
n, err := c.reader.Read(b)
if err != nil {
if err == io.EOF {
c.reader = nil
}
return n, err
}
return n, nil
}
func (c Conn) SetDeadline(t time.Time) error {
if err := c.Conn.SetReadDeadline(t); err != nil {
return err
}
if err := c.Conn.SetWriteDeadline(t); err != nil {
return err
}
return nil
}
func (c Conn) Close() error {
err := c.Conn.Close()
return err
}