-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
133 lines (109 loc) · 3.09 KB
/
client.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
package wamp_webrtc_go
import (
"context"
"encoding/json"
"github.com/google/uuid"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
"github.com/xconnio/wampproto-go/auth"
"github.com/xconnio/xconn-go"
)
type ClientConfig struct {
URL string
Realm string
ProcedureWebRTCOffer string
TopicAnswererOnCandidate string
TopicOffererOnCandidate string
Serializer xconn.WSSerializerSpec
Authenticator auth.ClientAuthenticator
}
func connectWebRTC(config *ClientConfig) (*WebRTCSession, error) {
session, err := xconn.Connect(context.Background(), config.URL, config.Realm)
if err != nil {
return nil, err
}
offerer := NewOfferer()
offerConfig := &OfferConfig{
Protocol: config.Serializer.SubProtocol(),
ICEServers: []webrtc.ICEServer{},
Ordered: true,
TopicAnswererOnCandidate: config.TopicAnswererOnCandidate,
}
_, err = session.Subscribe(config.TopicOffererOnCandidate, func(event *xconn.Event) {
if len(event.Arguments) < 2 {
log.Errorf("invalid arguments length")
return
}
candidateJSON, ok := event.Arguments[1].(string)
if !ok {
log.Errorln("offer must be a string")
return
}
var candidate webrtc.ICECandidateInit
if err := json.Unmarshal([]byte(candidateJSON), &candidate); err != nil {
log.Errorln(err)
return
}
if err = offerer.AddICECandidate(candidate); err != nil {
log.Errorln(err)
}
}, nil)
if err != nil {
return nil, err
}
requestID := uuid.New().String()
offer, err := offerer.Offer(offerConfig, session, requestID)
if err != nil {
return nil, err
}
offerJSON, err := json.Marshal(offer)
if err != nil {
return nil, err
}
result, err := session.Call(context.Background(), config.ProcedureWebRTCOffer,
[]any{requestID, string(offerJSON)}, nil, nil)
if err != nil {
return nil, err
}
answerText := result.Arguments[0].(string)
var answer Answer
if err = json.Unmarshal([]byte(answerText), &answer); err != nil {
return nil, err
}
if err = offerer.HandleAnswer(answer); err != nil {
return nil, err
}
channel := <-offerer.WaitReady()
return &WebRTCSession{
Channel: channel,
Connection: offerer.connection,
}, nil
}
func ConnectWebRTC(config *ClientConfig) (*WebRTCSession, error) {
webRTCSession, err := connectWebRTC(config)
if err != nil {
return nil, err
}
peer := NewWebRTCPeer(webRTCSession.Channel)
_, err = xconn.Join(peer, config.Realm, config.Serializer.Serializer(), config.Authenticator)
if err != nil {
return nil, err
}
return &WebRTCSession{
Channel: webRTCSession.Channel,
Connection: webRTCSession.Connection,
}, nil
}
func ConnectWAMP(config *ClientConfig) (*xconn.Session, error) {
webRTCConnection, err := connectWebRTC(config)
if err != nil {
return nil, err
}
peer := NewWebRTCPeer(webRTCConnection.Channel)
base, err := xconn.Join(peer, config.Realm, config.Serializer.Serializer(), config.Authenticator)
if err != nil {
return nil, err
}
wampSession := xconn.NewSession(base, config.Serializer.Serializer())
return wampSession, nil
}