-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsocket_connection.go
More file actions
50 lines (41 loc) · 1.71 KB
/
socket_connection.go
File metadata and controls
50 lines (41 loc) · 1.71 KB
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
package velaros
import (
"context"
"github.com/coder/websocket"
)
// WebSocketConnection is a SocketConnection implementation that wraps
// github.com/coder/websocket.Conn. This is the default connection type used by the
// router when handling HTTP WebSocket upgrades.
type WebSocketConnection struct {
webSocketConnection *websocket.Conn
}
var _ SocketConnection = &WebSocketConnection{}
// NewWebSocketConnection creates a WebSocketConnection from a github.com/coder/websocket.Conn.
// This is used internally by the router. Most applications don't need to call this directly
// unless implementing custom connection handling with Router.HandleConnection.
func NewWebSocketConnection(websocketConnection *websocket.Conn) *WebSocketConnection {
return &WebSocketConnection{
webSocketConnection: websocketConnection,
}
}
// Read reads the next message from the WebSocket connection. Blocks until a message
// arrives or an error occurs. Implements SocketConnection.Read.
func (c *WebSocketConnection) Read(ctx context.Context) (*SocketMessage, error) {
messageType, data, err := c.webSocketConnection.Read(ctx)
if err != nil {
return nil, err
}
return &SocketMessage{
Type: messageType,
RawData: data,
}, nil
}
// Write sends a message to the WebSocket connection. Implements SocketConnection.Write.
func (c *WebSocketConnection) Write(ctx context.Context, msg *SocketMessage) error {
return c.webSocketConnection.Write(ctx, msg.Type, msg.Data)
}
// Close closes the WebSocket connection with the given status code and reason.
// Implements SocketConnection.Close.
func (c *WebSocketConnection) Close(status Status, reason string) error {
return c.webSocketConnection.Close(websocket.StatusCode(status), reason)
}