-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws.go
73 lines (63 loc) · 2.84 KB
/
ws.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
package openairt
import (
"context"
"errors"
"net/http"
)
// MessageType represents the type of a WebSocket message.
// See https://tools.ietf.org/html/rfc6455#section-5.6
type MessageType int
// MessageType constants.
const (
// MessageText is for UTF-8 encoded text messages like JSON.
MessageText MessageType = iota + 1
// MessageBinary is for binary messages like protobufs.
MessageBinary
)
// WebSocketConn is a WebSocket connection abstraction.
type WebSocketConn interface {
// ReadMessage reads a message from the WebSocket connection.
//
// The ctx could be used to cancel the read operation. It's behavior depends on the underlying implementation.
// If the read succeeds, the returned error should be nil, and the ctx's cancel/timeout shouldn't affect the
// connection and future read operations.
//
// If the returned error is Permanent, the future read operations on the same connection will not succeed,
// that means the connection is broken and should be closed or had already been closed.
//
// In general, once the ctx is canceled before read finishes, the read operation will be canceled and
// the connection will be closed.
//
// There are some exceptions:
// - If the underlying implementation is gorilla/websocket, the read operation will not be canceled
// when the ctx is canceled before its deadline, it will keep reading until the ctx reaches deadline or the connection is closed.
ReadMessage(ctx context.Context) (messageType MessageType, p []byte, err error)
// WriteMessage writes a message to the WebSocket connection.
//
// The ctx could be used to cancel the write operation. It's behavior depends on the underlying implementation.
//
// If the returned error is Permanent, the future write operations on the same connection will not succeed,
// that means the connection is broken and should be closed or had already been closed.
//
// In general, once the ctx is canceled before write finishes, the write operation will be canceled and
// the connection will be closed.
WriteMessage(ctx context.Context, messageType MessageType, data []byte) error
// Close closes the WebSocket connection.
Close() error
// Response returns the *http.Response of the WebSocket connection.
// Commonly used to get response headers.
Response() *http.Response
}
// WebSocketDialer is a WebSocket connection dialer abstraction.
type WebSocketDialer interface {
// Dial establishes a new WebSocket connection to the given URL.
// The ctx could be used to cancel the dial operation. It's effect depends on the underlying implementation.
Dial(ctx context.Context, url string, header http.Header) (WebSocketConn, error)
}
// DefaultDialer returns a default WebSocketDialer.
func DefaultDialer() WebSocketDialer {
return NewCoderWebSocketDialer(CoderWebSocketOptions{})
}
var (
ErrUnsupportedMessageType = errors.New("unsupported message type")
)