From a79fe7705d1d83d7ec9b920f8155a8cfd11cc909 Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Fri, 6 Dec 2024 17:17:19 +0500 Subject: [PATCH] Refactor into separate functions for WebRTC connection and WAMP session --- client.go | 16 ++++++++++++++-- cmd/client/main.go | 2 +- types.go | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 5c04c71..8e89ff1 100644 --- a/client.go +++ b/client.go @@ -18,7 +18,7 @@ type ClientConfig struct { Serializer xconn.WSSerializerSpec } -func ConnectWebRTC(config *ClientConfig) (*xconn.Session, error) { +func ConnectWebRTC(config *ClientConfig) (*WebRTCSession, error) { session, err := xconn.Connect(context.Background(), config.URL, config.Realm) if err != nil { return nil, err @@ -60,7 +60,19 @@ func ConnectWebRTC(config *ClientConfig) (*xconn.Session, error) { channel := <-offerer.WaitReady() - peer := NewWebRTCPeer(channel) + return &WebRTCSession{ + Channel: channel, + Connection: offerer.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(), nil) if err != nil { return nil, err diff --git a/cmd/client/main.go b/cmd/client/main.go index 38c61b8..d9ea073 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -20,7 +20,7 @@ func main() { TopicAnswererOnCandidate: topicAnswererOnCandidate, Serializer: xconn.CBORSerializerSpec, } - session, err := wamp_webrtc_go.ConnectWebRTC(config) + session, err := wamp_webrtc_go.ConnectWAMP(config) if err != nil { log.Fatal(err) } diff --git a/types.go b/types.go index f5e2a67..3f54050 100644 --- a/types.go +++ b/types.go @@ -33,3 +33,8 @@ type ProviderConfig struct { TopicPublishLocalCandidate string Serializer serializers.Serializer } + +type WebRTCSession struct { + Connection *webrtc.PeerConnection + Channel *webrtc.DataChannel +}