Skip to content

Commit

Permalink
proper code, bridge connect4u is now on bridgeOO
Browse files Browse the repository at this point in the history
  • Loading branch information
folkvir committed Oct 29, 2018
1 parent 3ce6bee commit 0422cc0
Show file tree
Hide file tree
Showing 30 changed files with 2,444 additions and 2,499 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,26 @@ peer-sampling protocols.
- [ ] Encapsulate each message sent for distinguish admin messages from application messages
- [ ] Minimize the encapsulation
- [ ] Control the size of the object sent and create a mechanism to handle bigger files (chunkification)


## Turn and Stun servers (for tests purposes only)

For production purposes see (https://www.twilio.com/stun-turn)

- a stun server is available through `node-stun-server` [node-stun](https://github.com/enobufs/stun)
- check the node-stun.ini in tests/stun folder.
```javascript
const iceServers = {
url: 'stun:127.0.0.1:3478'
}
```

- a turn server is available through a Dockerfile
- check the Dockerfile in the tests/turn folder.
```javascript
const iceServers = {
urls: 'turn:127.0.0.1:3478?transport=udp'
username: 'username',
password: 'password'
}
```
2,100 changes: 1,031 additions & 1,069 deletions bin/n2n-wrtc.bundle.js

Large diffs are not rendered by default.

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.

96 changes: 74 additions & 22 deletions lib/api/socket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const errors = require('../errors')
const events = require('../events')
const EventEmitter = require('events')
const sizeof = require('object-sizeof')
const lmerge = require('lodash.merge')
const short = require('short-uuid')
const translator = short()

/**
* @class
Expand All @@ -15,22 +19,49 @@ class Socket extends EventEmitter {
*/
constructor (options = {}) {
super()
this.socketId = translator.new()
this._debug = (require('debug'))('n2n:socket')
this._debugMessage = (require('debug'))('n2n:message')
this.options = options
this.options = lmerge({ chunks: 16000 }, options)
this.status = 'disconnected' // connected or disconnected
this._buffer = [] // for preventing messages arrived before the connect event
this.once('connect', () => {
this.status = 'connected'
if (this._buffer.length > 0) {
this._debugMessage('[socket] review our internal buffer of messages arrived sooner...')
this._buffer.forEach(message => {
this._receiveData(message)
})
}
this._debug('status: connected')
})
this.on(events.socket.RECEIVE_OFFER, this._receiveOffer)
this.buffer = []
this.statistics = {
offerSent: 0,
offerReceived: 0
}
}

/**
* Review the buffer of data sent
* @return {void}
*/
reviewBuffer () {
let data
while ((data = this.buffer.shift())) {
this.send(data)
}
}

/**
* Signal to the supervisor of the socket that this socket is conencted and review the internal buffer
* @return {void}
*/
signalConnect () {
this.status = 'connected'
this.emit('connect')
this.reviewBuffer()
}

/**
* Signal to the supervisor of the socket that this socket is disconnected
* @return {void}
*/
signalDisconnect () {
if (this.status === 'connected') {
this.status = 'disconnected'
this.emit('close')
}
}

/**
Expand All @@ -48,7 +79,14 @@ class Socket extends EventEmitter {
* @return {void} [description]
*/
emitOffer (offer) {
this.emit(events.socket.EMIT_OFFER, offer)
this.statistics.offerSent++
this._debug('[socket:%s] Send an accepted offer (status=%s): ', this.socketId, this.status, offer, this.statistics)
if (this.status === 'connected') {
console.log('The socket is connected but you continue to emit offers...', offer, this.status, this.__socket._channel.readyState)
this.emit(events.socket.EMIT_OFFER_RENEGOCIATE, offer)
} else {
this.emit(events.socket.EMIT_OFFER, offer)
}
}

/**
Expand Down Expand Up @@ -81,12 +119,16 @@ class Socket extends EventEmitter {
* @return {void}
*/
_receiveData (data) {
if (this.status !== 'connected') {
this._debugMessage('[socket] buffer the message the socket is not connected yet...')
this._buffer.push(data)
} else {
this.emit('data', data)
if (this.status === 'connecting') {
this.signalConnect()
}
this._debug('[socket:%s] receiving data, size=%f', this.socketId, sizeof(data))
this.emit('data', data)
}

_manageErrors (...args) {
this._debug('Error on the socket: ', ...args)
this.emit('error', ...args)
}

/**
Expand All @@ -107,9 +149,7 @@ class Socket extends EventEmitter {
async connect (options = this.options) {
this.status = 'connecting'
return this._connect(options).then((res) => {
this.status = 'connected'
this._debug('status: connected')
this.emit('connect')
this.signalConnect()
return res
}).catch(e => e)
}
Expand All @@ -121,7 +161,19 @@ class Socket extends EventEmitter {
* @return {Promise} Promise resolved when the message is sent
*/
async send (data, options) {
return this._send(data, options)
const size = sizeof(data)
this._debug('[socket:%s] Data size sent (max allowed=%f Bytes): %f', this.socketId, this.options.chunks, size)
if (size > this.options.chunks) {
return Promise.reject(new Error('Your data is too big. Max allowed: ' + this.options.chunks))
} else {
if (this.status === 'connected') {
return this._send(data, options)
} else if (this.status === 'connecting') {
this.buffer.push(data)
} else if (this.status === 'disconnected') {
throw new Error('socket disconnected.')
}
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
socket: {
RECEIVE_OFFER: 'socket-ro',
EMIT_OFFER: 'socket-eo'
EMIT_OFFER: 'socket-eo',
EMIT_OFFER_RENEGOCIATE: 'socket-eo-r'
},
signaling: {
RECEIVE_OFFER: 'signalig-ro',
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
N2N: require('./main'),
Neighborhood: require('./neighborhood'),
sockets: require('./sockets'),
signaling: require('./signaling'),
api: require('./api')
Expand Down
Loading

0 comments on commit 0422cc0

Please sign in to comment.