Skip to content

Commit

Permalink
Pass connectionState to onDisconnect()
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed Feb 18, 2020
1 parent 48fb892 commit 241e2d2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cheatsheet.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ io.onConnection(channel => {
const { id } = channel const { id } = channel


// whenever the channel got disconnected // whenever the channel got disconnected
channel.onDisconnect(() => {}) // the event will be 'disconnected', 'failed' or 'closed'
channel.onDisconnect(event => {})


// listen for a custom event // listen for a custom event
channel.on('chat message', data => {}) channel.on('chat message', data => {})
Expand Down
5 changes: 3 additions & 2 deletions packages/chatApp/src/server.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ server.listen(3000, () => {
}) })


io.onConnection((channel: ServerChannel) => { io.onConnection((channel: ServerChannel) => {
channel.onDisconnect(() => { channel.onDisconnect(event => {
console.log(`${channel.id} got disconnected`) 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}!`) channel.emit('chat message', `Welcome to the chat ${channel.id}!`)
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/typings.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export interface ConnectionEventCallbackServer {
(channel: any): void (channel: any): void
} }


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

export interface EventOptions { export interface EventOptions {
roomId?: RoomId roomId?: RoomId
senderId?: ChannelId senderId?: ChannelId
Expand Down
13 changes: 7 additions & 6 deletions packages/server/src/wrtc/channel.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RawMessage, RawMessage,
ChannelId, ChannelId,
EventName, EventName,
ConnectionEventCallbackServer, DisconnectEventCallbackServer,
EventCallbackRawMessage, EventCallbackRawMessage,
ServerOptions, ServerOptions,
EmitOptions EmitOptions
Expand Down Expand Up @@ -72,12 +72,13 @@ export default class ServerChannel {


/** /**
* Listen for the disconnect event. * 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) { onDisconnect(callback: DisconnectEventCallbackServer) {
this.eventEmitter.on(EVENTS.DISCONNECT, (channel: ServerChannel) => { this.eventEmitter.on(EVENTS.DISCONNECT, (connectionState: 'disconnected' | 'failed' | 'closed') => {
let cb: ConnectionEventCallbackServer = channel => callback(channel) let cb: DisconnectEventCallbackServer = connectionState => callback(connectionState)
cb(channel) cb(connectionState)
}) })
} }


Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/wrtc/connectionsManager.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class ConnectionsManagerServer {


pc.onconnectionstatechange = () => { pc.onconnectionstatechange = () => {
if (pc.connectionState === 'disconnected' || pc.connectionState === 'failed' || pc.connectionState === 'closed') { 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) this.deleteConnection(connection)
} }
} }
Expand Down

0 comments on commit 241e2d2

Please sign in to comment.