Skip to content
Permalink
Browse files

Pass connectionState to onDisconnect()

  • Loading branch information
yandeu committed Feb 18, 2020
1 parent 48fb892 commit 241e2d242029e0006fbaab5350d7b43c4d7a682f
@@ -62,7 +62,8 @@ io.onConnection(channel => {
const { id } = channel
// whenever the channel got disconnected
channel.onDisconnect(() => {})
// the event will be 'disconnected', 'failed' or 'closed'
channel.onDisconnect(event => {})
// listen for a custom event
channel.on('chat message', data => {})
@@ -29,8 +29,9 @@ server.listen(3000, () => {
})

io.onConnection((channel: ServerChannel) => {
channel.onDisconnect(() => {
console.log(`${channel.id} got disconnected`)
channel.onDisconnect(event => {
console.log('onDisconnect event:', event)
if (event === 'closed') console.log(`The connection for channel ${channel.id} got closed!`)
})

channel.emit('chat message', `Welcome to the chat ${channel.id}!`)
@@ -60,6 +60,10 @@ export interface ConnectionEventCallbackServer {
(channel: any): void
}

export interface DisconnectEventCallbackServer {
(connectionState: 'disconnected' | 'failed' | 'closed'): void
}

export interface EventOptions {
roomId?: RoomId
senderId?: ChannelId
@@ -10,7 +10,7 @@ import {
RawMessage,
ChannelId,
EventName,
ConnectionEventCallbackServer,
DisconnectEventCallbackServer,
EventCallbackRawMessage,
ServerOptions,
EmitOptions
@@ -72,12 +72,13 @@ export default class ServerChannel {

/**
* Listen for the disconnect event.
* @param callback The event callback.
* Gets the connectionState 'disconnected', 'failed' or 'closed'. See https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/connectionState
* @param callback The connectionState.
*/
onDisconnect(callback: ConnectionEventCallbackServer) {
this.eventEmitter.on(EVENTS.DISCONNECT, (channel: ServerChannel) => {
let cb: ConnectionEventCallbackServer = channel => callback(channel)
cb(channel)
onDisconnect(callback: DisconnectEventCallbackServer) {
this.eventEmitter.on(EVENTS.DISCONNECT, (connectionState: 'disconnected' | 'failed' | 'closed') => {
let cb: DisconnectEventCallbackServer = connectionState => callback(connectionState)
cb(connectionState)
})
}

@@ -32,7 +32,7 @@ export default class ConnectionsManagerServer {

pc.onconnectionstatechange = () => {
if (pc.connectionState === 'disconnected' || pc.connectionState === 'failed' || pc.connectionState === 'closed') {
connection.channel.eventEmitter.emit(EVENTS.DISCONNECT)
connection.channel.eventEmitter.emit(EVENTS.DISCONNECT, pc.connectionState)
this.deleteConnection(connection)
}
}

0 comments on commit 241e2d2

Please sign in to comment.
You can’t perform that action at this time.