forked from reugn/go-streams
/
websocket.go
174 lines (156 loc) · 4.17 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
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
166
167
168
169
170
171
172
173
174
package extension
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/websocket"
"github.com/hzw456/go-streams"
"github.com/hzw456/go-streams/flow"
)
// Message represents a message from peer
type Message struct {
// The message types are defined in RFC 6455, section 11.8.
MsgType int
Payload []byte
}
// WebSocketSource connector
type WebSocketSource struct {
ctx context.Context
connection *websocket.Conn
out chan interface{}
errsChan chan *flow.Error
}
// NewWebSocketSource returns a new WebSocketSource instance
func NewWebSocketSource(ctx context.Context, url string, errsChan chan *flow.Error) (*WebSocketSource, error) {
return NewWebSocketSourceWithDialer(ctx, url, websocket.DefaultDialer, errsChan)
}
// NewWebSocketSourceWithDialer returns a new WebSocketSource instance
func NewWebSocketSourceWithDialer(ctx context.Context, url string, dialer *websocket.Dialer, errsChan chan *flow.Error) (*WebSocketSource, error) {
conn, _, err := dialer.Dial(url, nil)
if err != nil {
return nil, err
}
source := &WebSocketSource{
ctx: ctx,
connection: conn,
out: make(chan interface{}),
errsChan: errsChan,
}
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)
retry := 0
loop:
for {
select {
case <-sigchan:
break loop
case <-wsock.ctx.Done():
break loop
default:
t, msg, err := wsock.connection.ReadMessage()
if err != nil {
retry++
if retry >= 5 {
wsock.errsChan <- &flow.Error{wsock.ctx, fmt.Errorf("Error on ws ReadMessage: err=%v url=%v", err, wsock.connection.LocalAddr().String())}
break loop
}
time.Sleep(3 * time.Second)
} else {
retry = 0
wsock.out <- Message{
MsgType: t,
Payload: msg,
}
// exit on CloseMessage
if t == websocket.CloseMessage {
break loop
}
}
}
}
log.Print("Closing the 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 interface{} {
return wsock.out
}
// WebSocketSink connector
type WebSocketSink struct {
ctx context.Context
connection *websocket.Conn
in chan interface{}
}
// NewWebSocketSink returns a new WebSocketSink instance
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 interface{}),
}
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.TextMessage, m)
default:
log.Printf("WebSocketSink Unsupported message type %v", m)
}
if err != nil {
log.Printf("Error on ws WriteMessage: %v", err)
}
}
log.Print("Closing the WebSocketSink connection")
wsock.connection.Close()
}
// NewKafkaSink returns a new KafkaSink instance
func NewWebSocketSinkWithConn(ctx context.Context, conn *websocket.Conn) (*WebSocketSink, error) {
defer func() {
if r := recover(); r != nil {
panic(r)
}
}()
sink := &WebSocketSink{
connection: conn,
in: make(chan interface{}),
ctx: ctx,
}
go sink.init()
return sink, nil
}
// In returns an input channel for receiving data
func (wsock *WebSocketSink) In() chan<- interface{} {
return wsock.in
}