Skip to content

Commit

Permalink
better support for webrtc
Browse files Browse the repository at this point in the history
  • Loading branch information
folkvir committed Jul 10, 2019
1 parent 7fd1d05 commit 23e6802
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 29 deletions.
45 changes: 35 additions & 10 deletions bin/n2n-wrtc.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ class N2N extends EventEmitter {
}

/**
* Simulate a crash by disconnecting all sockets from inview/outview
* Simulate a crash by disconnecting all sockets from inview/outview including all signaling services
* @return {void}
* @example
* // Offline connection
Expand All @@ -876,6 +876,7 @@ class N2N extends EventEmitter {
* const b = new N2N({n2n: {id: 'b'}})
* a.connect(b)
* a.crash()
* b.crash()
* // will receive events on crash_out and crash_in respectively for a and b
*/
crash () {
Expand All @@ -885,6 +886,9 @@ class N2N extends EventEmitter {
this.livingInview.forEach(p => {
p.socket.disconnect()
})
for (let key in this.signaling) {
this.signaling[key].disconnect()
}
}

/**
Expand Down Expand Up @@ -2492,8 +2496,8 @@ class OnlineSignaling extends SignalingAPI {
super(options)
this.parent = n2n
this.on(events.signaling.RECEIVE_OFFER, ({ jobId, initiator, destination, type, offer }) => {
if (!initiator || !destination || !offer || !type) throw new Error('PLEASE REPORT, Problem with the offline signaling service. provide at least initiator, destination, type a,d the offer as properties in the object received')
if (!this.parent.livingInview.has(initiator) && type === 'new') {
if (!initiator || !destination || !offer || !type) throw new Error('PLEASE REPORT, Problem with the signaling service. provide at least initiator, destination, type and the offer as properties in the object received')
if (!this.parent.livingInview.has(initiator) && !this.parent.pendingInview.has(initiator) && type === 'new') {
// we do have the socket for the moment, create it
this.parent.createNewSocket(this.parent.options.socket, initiator, false)
// ATTENTION: LISTENERS HAVE TO BE DECLARED ONCE!
Expand All @@ -2506,7 +2510,7 @@ class OnlineSignaling extends SignalingAPI {
})
})
this.parent.pendingInview.get(initiator).socket.on(events.socket.EMIT_OFFER_RENEGOCIATE, (socketOffer) => {
console.log('[bridge] receive a negociate offer')
// console.log('[bridge] receive a negociate offer')
const off = {
jobId,
initiator,
Expand Down Expand Up @@ -2586,6 +2590,19 @@ class OnlineSignaling extends SignalingAPI {
})
}

/**
* Disconnect the signaling service from the signaling server
* @return {void}
*/
disconnect () {
if (this.socket) {
this.socket.close()
this.socket.off('connect')
this.socket.off('connect_failed')
this.socket.off('connect_error')
}
}

/**
* Initialize the Socket.io socket.
* @param {Socket.io} socket
Expand All @@ -2601,24 +2618,24 @@ class OnlineSignaling extends SignalingAPI {
this.emit(events.signaling.RECEIVE_OFFER, offer)
})
socket.on('error', (error) => {
console.error('SS error: ', error)
console.error('Signaling Service error: ', error)
})
socket.on('disconnect', (error) => {
console.log('SS disconnection: ', error)
console.log('Signaling Service disconnection: ', error)
})
socket.on('reconnect_error', (error) => {
socket.close()
console.error('SS disconnection: ', error)
console.error('Signaling Service disconnection: ', error)
})
socket.on('reconnect_failed', (error) => {
socket.close()
console.error('SS disconenction: ', error)
console.error('Signaling Service disconenction: ', error)
})
socket.on('reconnect_attempt', () => {
console.log('SS attempting a reconnection')
console.log('Signaling Service attempting a reconnection')
})
socket.on('reconnect', (number) => {
console.log('SS successfull reconnection when attempting a reconnection: ', number)
console.log('Signaling Service successfull reconnection when attempting a reconnection: ', number)
})
}

Expand Down Expand Up @@ -2689,6 +2706,14 @@ class Signaling extends EventEmitter {
this.options = options
}

/**
* Disconnect the signaling service
* @return {Promise} Resolved when disconnect.
*/
async disconnect () {
return Promise.resolve()
}

/**
* @description (**Abstract**) Connect the signaling service
* @param {Object} options options for the connection if needed
Expand Down
2 changes: 1 addition & 1 deletion bin/n2n-wrtc.bundle.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bin/n2n-wrtc.bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bin/n2n-wrtc.bundle.min.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ class N2N extends EventEmitter {
}

/**
* Simulate a crash by disconnecting all sockets from inview/outview
* Simulate a crash by disconnecting all sockets from inview/outview including all signaling services
* @return {void}
* @example
* // Offline connection
Expand All @@ -646,6 +646,7 @@ class N2N extends EventEmitter {
* const b = new N2N({n2n: {id: 'b'}})
* a.connect(b)
* a.crash()
* b.crash()
* // will receive events on crash_out and crash_in respectively for a and b
*/
crash () {
Expand All @@ -655,6 +656,9 @@ class N2N extends EventEmitter {
this.livingInview.forEach(p => {
p.socket.disconnect()
})
for (let key in this.signaling) {
this.signaling[key].disconnect()
}
}

/**
Expand Down
31 changes: 22 additions & 9 deletions lib/signaling/online.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class OnlineSignaling extends SignalingAPI {
super(options)
this.parent = n2n
this.on(events.signaling.RECEIVE_OFFER, ({ jobId, initiator, destination, type, offer }) => {
if (!initiator || !destination || !offer || !type) throw new Error('PLEASE REPORT, Problem with the offline signaling service. provide at least initiator, destination, type a,d the offer as properties in the object received')
if (!this.parent.livingInview.has(initiator) && type === 'new') {
if (!initiator || !destination || !offer || !type) throw new Error('PLEASE REPORT, Problem with the signaling service. provide at least initiator, destination, type and the offer as properties in the object received')
if (!this.parent.livingInview.has(initiator) && !this.parent.pendingInview.has(initiator) && type === 'new') {
// we do have the socket for the moment, create it
this.parent.createNewSocket(this.parent.options.socket, initiator, false)
// ATTENTION: LISTENERS HAVE TO BE DECLARED ONCE!
Expand All @@ -31,7 +31,7 @@ class OnlineSignaling extends SignalingAPI {
})
})
this.parent.pendingInview.get(initiator).socket.on(events.socket.EMIT_OFFER_RENEGOCIATE, (socketOffer) => {
console.log('[bridge] receive a negociate offer')
// console.log('[bridge] receive a negociate offer')
const off = {
jobId,
initiator,
Expand Down Expand Up @@ -111,6 +111,19 @@ class OnlineSignaling extends SignalingAPI {
})
}

/**
* Disconnect the signaling service from the signaling server
* @return {void}
*/
disconnect () {
if (this.socket) {
this.socket.close()
this.socket.off('connect')
this.socket.off('connect_failed')
this.socket.off('connect_error')
}
}

/**
* Initialize the Socket.io socket.
* @param {Socket.io} socket
Expand All @@ -126,24 +139,24 @@ class OnlineSignaling extends SignalingAPI {
this.emit(events.signaling.RECEIVE_OFFER, offer)
})
socket.on('error', (error) => {
console.error('SS error: ', error)
console.error('Signaling Service error: ', error)
})
socket.on('disconnect', (error) => {
console.log('SS disconnection: ', error)
console.log('Signaling Service disconnection: ', error)
})
socket.on('reconnect_error', (error) => {
socket.close()
console.error('SS disconnection: ', error)
console.error('Signaling Service disconnection: ', error)
})
socket.on('reconnect_failed', (error) => {
socket.close()
console.error('SS disconenction: ', error)
console.error('Signaling Service disconenction: ', error)
})
socket.on('reconnect_attempt', () => {
console.log('SS attempting a reconnection')
console.log('Signaling Service attempting a reconnection')
})
socket.on('reconnect', (number) => {
console.log('SS successfull reconnection when attempting a reconnection: ', number)
console.log('Signaling Service successfull reconnection when attempting a reconnection: ', number)
})
}

Expand Down
11 changes: 6 additions & 5 deletions lib/signaling/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ const Server = function (config = require('./config.json')) {
* @return {void}
* @private
*/
this.receiveOffer = (self, socket, { initiator, destination, type, offer }) => {
this.receiveOffer = (self, socket, { jobId, initiator, destination, type, offer }) => {
let sock
if (type === 'back') {
sock = lfind(self.io.sockets.sockets, sock => sock.userId === initiator)
} else {
} else if (type === 'new') {
sock = lfind(self.io.sockets.sockets, sock => sock.userId === destination)
} else {
console.error(new Error('the offer is not handled due its type: ' + type))
}
if (!sock) {
throw new Error('PLEASE REPORT: cant find: ' + initiator + ' or ' + destination)
}
console.log('[user=%s][type=%s] receiving an offer to emit to: %s', socket.userId, type, sock.userId)
sock.emit('offer', { initiator, destination, type, offer })
sock.emit('offer', { jobId, initiator, destination, type, offer })
}
/**
* Emit on the event 'getNewPeer' a random peer from the room of the asker
Expand All @@ -74,7 +76,6 @@ const Server = function (config = require('./config.json')) {
let i = 0
while (i < config.max && i < socks.length) {
const userId = self.io.sockets.sockets[socks[i]].userId
console.log('User id: ', userId, ' Socket id: ', myId)
if (userId !== myId) val.push(userId)
i++
}
Expand Down Expand Up @@ -125,7 +126,7 @@ const Server = function (config = require('./config.json')) {
socket.userId = id
socket.userRoom = room
socket.join(room)
console.log('user: ' + id + ' joined room #' + room)
console.log('New user: %s (%s) joined room #%s', id, socket.userId, room)

/**
* Socket event when an offer is received from the client
Expand Down
8 changes: 8 additions & 0 deletions lib/signaling/signaling.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class Signaling extends EventEmitter {
this.options = options
}

/**
* Disconnect the signaling service
* @return {Promise} Resolved when disconnect.
*/
async disconnect () {
return Promise.resolve()
}

/**
* @description (**Abstract**) Connect the signaling service
* @param {Object} options options for the connection if needed
Expand Down

0 comments on commit 23e6802

Please sign in to comment.