-
Notifications
You must be signed in to change notification settings - Fork 45
/
web_socket.go
165 lines (135 loc) · 3.86 KB
/
web_socket.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package ws
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/gorilla/websocket"
"github.com/superproj/onex/pkg/streams"
"github.com/superproj/onex/pkg/streams/flow"
)
// Message represents a WebSocket message container.
// Message types are defined in RFC 6455, section 11.8.
type Message struct {
MsgType int
Payload []byte
}
// WebSocketSource represents a WebSocket source connector.
type WebSocketSource struct {
ctx context.Context
connection *websocket.Conn
out chan any
}
// NewWebSocketSource creates and returns a new WebSocketSource using the default dialer.
func NewWebSocketSource(ctx context.Context, url string) (*WebSocketSource, error) {
return NewWebSocketSourceWithDialer(ctx, url, websocket.DefaultDialer)
}
// NewWebSocketSourceWithDialer returns a new WebSocketSource instance.
func NewWebSocketSourceWithDialer(ctx context.Context, url string, dialer *websocket.Dialer) (*WebSocketSource, error) {
conn, _, err := dialer.Dial(url, nil)
if err != nil {
return nil, err
}
source := &WebSocketSource{
ctx: ctx,
connection: conn,
out: make(chan any),
}
go source.init()
return source, nil
}
// init starts the main loop.
func (wsock *WebSocketSource) init() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
loop:
for {
select {
case <-sigchan:
break loop
case <-wsock.ctx.Done():
break loop
default:
t, msg, err := wsock.connection.ReadMessage()
if err != nil {
log.Printf("Error reading WebSocket message: %s", err)
} else {
wsock.out <- Message{
MsgType: t,
Payload: msg,
}
// exit loop on CloseMessage
if t == websocket.CloseMessage {
break loop
}
}
}
}
log.Print("Closing WebSocketSource connection")
close(wsock.out)
wsock.connection.Close()
}
// Via streams data through the given flow.
func (wsock *WebSocketSource) Via(_flow streams.Flow) streams.Flow {
flow.DoStream(wsock, _flow)
return _flow
}
// Out returns an output channel for sending data.
func (wsock *WebSocketSource) Out() <-chan any {
return wsock.out
}
// WebSocketSink represents a WebSocket sink connector.
type WebSocketSink struct {
ctx context.Context
connection *websocket.Conn
in chan any
}
// NewWebSocketSink creates and returns a new WebSocketSink using the default dialer.
func NewWebSocketSink(ctx context.Context, url string) (*WebSocketSink, error) {
return NewWebSocketSinkWithDialer(ctx, url, websocket.DefaultDialer)
}
// NewWebSocketSinkWithDialer returns a new WebSocketSink instance.
func NewWebSocketSinkWithDialer(ctx context.Context, url string, dialer *websocket.Dialer) (*WebSocketSink, error) {
conn, _, err := dialer.Dial(url, nil)
if err != nil {
return nil, err
}
sink := &WebSocketSink{
ctx: ctx,
connection: conn,
in: make(chan any),
}
go sink.init()
return sink, nil
}
// init starts the main loop.
func (wsock *WebSocketSink) init() {
for msg := range wsock.in {
var err error
switch m := msg.(type) {
case Message:
err = wsock.connection.WriteMessage(m.MsgType, m.Payload)
case string:
err = wsock.connection.WriteMessage(websocket.TextMessage, []byte(m))
case []byte:
err = wsock.connection.WriteMessage(websocket.BinaryMessage, m)
default:
log.Printf("WebSocketSink Unsupported message type %v", m)
}
if err != nil {
log.Printf("Error processing WebSocket message: %s", err)
}
}
log.Print("Closing WebSocketSink connection")
wsock.connection.Close()
}
// In returns an input channel for receiving data.
func (wsock *WebSocketSink) In() chan<- any {
return wsock.in
}