Skip to content

Commit

Permalink
Merge pull request #10 from muzzammilshahid/handle-remote-candidates
Browse files Browse the repository at this point in the history
Handle remote candidates
  • Loading branch information
muzzammilshahid authored Dec 17, 2024
2 parents b563c97 + 2289844 commit 0eda894
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"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"
Expand All @@ -16,6 +17,7 @@ type ClientConfig struct {
Realm string
ProcedureWebRTCOffer string
TopicAnswererOnCandidate string
TopicOffererOnCandidate string
Serializer xconn.WSSerializerSpec
Authenticator auth.ClientAuthenticator
}
Expand All @@ -33,6 +35,33 @@ func connectWebRTC(config *ClientConfig) (*WebRTCSession, error) {
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 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
procedureWebRTCOffer = "io.xconn.webrtc.offer"
topicAnswererOnCandidate = "io.xconn.webrtc.answerer.on_candidate"
topicOffererOnCandidate = "io.xconn.webrtc.offerer.on_candidate"
)

func main() {
Expand All @@ -19,6 +20,7 @@ func main() {
Realm: "realm1",
ProcedureWebRTCOffer: procedureWebRTCOffer,
TopicAnswererOnCandidate: topicAnswererOnCandidate,
TopicOffererOnCandidate: topicOffererOnCandidate,
Serializer: xconn.CBORSerializerSpec,
Authenticator: auth.NewCRAAuthenticator("john", map[string]any{}, "hello"),
}
Expand Down
4 changes: 4 additions & 0 deletions offerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func (o *Offerer) HandleAnswer(answer Answer) error {
return nil
}

func (o *Offerer) AddICECandidate(candidate webrtc.ICECandidateInit) error {
return o.connection.AddICECandidate(candidate)
}

func (o *Offerer) WaitReady() chan *webrtc.DataChannel {
return o.channel
}

0 comments on commit 0eda894

Please sign in to comment.