Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure webpubsub signaling client #4

Closed
Closed
71 changes: 71 additions & 0 deletions src/y-webrtc-azure-webpubsub-signaling-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { WebrtcProvider, SignalingConn, publishSignalingMessage } from 'y-webrtc'
import { WebPubSubClient } from "@azure/web-pubsub-client"

export class AzureWebPubSubSignalingConn extends SignalingConn {
constructor (url) {
super(url)
this.connected = false
}

setupClient() {
this.client = new WebPubSubClient(this.url)
this.client.on('connected', e => {
this.connected = true
log(`connected (${url}) with ID ${e.connectionId}`)
// Join all the groups.
const groups = Array.from(rooms.keys())
groups.forEach(group =>
this.subscribe(group)
)
rooms.forEach(room =>
publishSignalingMessage(this, room, { type: 'announce', from: room.peerId })
)
})
this.client.on('disconnected', e => log(`disconnect (${url}): ${e.message}`))
this.client.on('stopped', () => log(`stopped (${url})`))
// Set an event handler for group messages before connecting, so we don't miss any.
this.client.on('group-message', e => {
this.handleMessage(e.message.group, e.message.data)
})
// Connect to the signaling server.
this.client.start()
}

connected () {
return this.connected
}

subscribe (roomName) {
this.client.joinGroup(roomName)
}

unsubscribe (roomName) {
this.client.leaveGroup(roomName)
}

publish (roomName, message) {
let messageType = "json"
if (typeof message === 'string') {
messageType = "text"
}
this.client.sendToGroup(roomName, message, messageType)
}

destroy () {
this.client.stop()
}
}

export class AzureWebPubSubSignalingWebrtcProvider extends WebrtcProvider {
connect () {
this.shouldConnect = true
this.signalingUrls.forEach(url => {
const signalingConn = map.setIfUndefined(signalingConns, url, () => new AzureWebPubSubSignalingConn(url))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line that is different in this overridden method, which is why it would be great to just pass in the new class name AzureWebPubSubSignalingConn somehow.

this.signalingConns.push(signalingConn)
signalingConn.providers.add(this)
})
if (this.room) {
this.room.connect()
}
}
}