-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjoiner.go
126 lines (103 loc) · 2.88 KB
/
joiner.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
package xconn
import (
"context"
"fmt"
"net"
netURL "net/url"
"time"
"github.com/gobwas/ws"
"github.com/xconnio/wampproto-go"
"github.com/xconnio/wampproto-go/auth"
"github.com/xconnio/wampproto-go/serializers"
)
type WebSocketJoiner struct {
SerializerSpec WSSerializerSpec
Authenticator auth.ClientAuthenticator
}
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, config *WSDialerConfig) (BaseSession, error) {
parsedURL, err := netURL.Parse(url)
if err != nil {
return nil, err
}
if w.SerializerSpec == nil {
w.SerializerSpec = JSONSerializerSpec
}
if w.Authenticator == nil {
w.Authenticator = auth.NewAnonymousAuthenticator("", nil)
}
peer, err := DialWebSocket(ctx, parsedURL, config)
if err != nil {
return nil, err
}
return Join(peer, realm, w.SerializerSpec.Serializer(), w.Authenticator)
}
func DialWebSocket(ctx context.Context, url *netURL.URL, config *WSDialerConfig) (Peer, error) {
if config == nil {
config = &WSDialerConfig{SubProtocol: JsonWebsocketProtocol}
}
wsDialer := ws.Dialer{
Protocols: []string{config.SubProtocol},
}
if config.DialTimeout == 0 {
wsDialer.Timeout = time.Second * 10
} else {
wsDialer.Timeout = config.DialTimeout
}
if config.NetDial == nil {
if url.Scheme == "unix" {
// Custom dial function for Unix Domain Socket
wsDialer.NetDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", url.Path)
}
url.Scheme = "ws"
}
} else {
wsDialer.NetDial = config.NetDial
}
conn, _, _, err := wsDialer.Dial(ctx, url.String())
if err != nil {
return nil, err
}
isBinary := config.SubProtocol != JsonWebsocketProtocol
peerConfig := WSPeerConfig{
Protocol: config.SubProtocol,
Binary: isBinary,
Server: false,
KeepAliveInterval: config.KeepAliveInterval,
KeepAliveTimeout: config.KeepAliveTimeout,
}
return NewWebSocketPeer(conn, peerConfig)
}
func Join(cl Peer, realm string, serializer serializers.Serializer,
authenticator auth.ClientAuthenticator) (BaseSession, error) {
j := wampproto.NewJoiner(realm, serializer, authenticator)
hello, err := j.SendHello()
if err != nil {
return nil, err
}
if err = cl.Write(hello); err != nil {
return nil, fmt.Errorf("failed to send wamp hello: %w", err)
}
for {
msg, err := cl.Read()
if err != nil {
return nil, fmt.Errorf("failed to parse websocket: %w", err)
}
toSend, err := j.Receive(msg)
if err != nil {
return nil, err
}
// nothing to send, this means the proto was established.
if toSend == nil {
details, err := j.SessionDetails()
if err != nil {
return nil, err
}
base := NewBaseSession(details.ID(), details.Realm(), details.AuthID(), details.AuthRole(), cl, serializer)
return base, nil
}
if err = cl.Write(toSend); err != nil {
return nil, fmt.Errorf("failed to send message: %w", err)
}
}
}