Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
feat: WebRTC in Nodejs wooooot!
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 16, 2016
1 parent bf7ed9c commit 71018bf
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
13 changes: 9 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ const sigServer = require('./src/signalling-server')

let sigS

gulp.task('test:browser:before', (done) => {
gulp.task('test:node:before', boot)
gulp.task('test:node:after', stop)
gulp.task('test:browser:before', boot)
gulp.task('test:browser:after', stop)

function boot (done) {
sigS = sigServer.start(15555, (err, info) => {
if (err) {
throw err
}
console.log('sig-server started on:', info.uri)
done()
})
})
}

gulp.task('test:browser:after', (done) => {
function stop (done) {
sigS.stop(done)
})
}

require('aegir/gulp')(gulp)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"bin": {
"star-sig": "src/signalling-server/bin.js"
},
"browser": {
"wrtc": null
},
"scripts": {
"lint": "gulp lint",
"build": "gulp build",
Expand Down Expand Up @@ -57,7 +60,8 @@
"simple-peer": "^6.0.4",
"socket.io": "^1.4.6",
"socket.io-client": "^1.4.6",
"stream-to-pull-stream": "^1.7.0"
"stream-to-pull-stream": "^1.7.0",
"wrtc": "0.0.61"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion src/signalling-server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ log.error = debug('signalling-server:error')
module.exports = {
log: log,
hapi: {
port: process.env.PORT || 8134,
port: process.env.PORT || 8135,
options: {
connections: {
routes: {
Expand Down
6 changes: 3 additions & 3 deletions src/signalling-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ exports.start = (port, callback) => {
port = undefined
}

if (port) {
config.hapi.port = port
if (!port) {
port = config.hapi.port
}

const http = new Hapi.Server(config.hapi.options)

http.connection({
port: config.hapi.port
port: port
})

http.start((err) => {
Expand Down
21 changes: 19 additions & 2 deletions src/webrtc-star/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const io = require('socket.io-client')
const EE = require('events').EventEmitter
const wrtc = require('wrtc')
const SimplePeer = require('simple-peer')
const peerId = require('peer-id')
const PeerInfo = require('peer-info')
Expand Down Expand Up @@ -40,7 +41,12 @@ function WebRTCStar () {

const intentId = (~~(Math.random() * 1e9)).toString(36) + Date.now()
const sioClient = listeners[Object.keys(listeners)[0]].io
const channel = new SimplePeer({ initiator: true, trickle: false })

const channel = new SimplePeer({
initiator: true,
trickle: false,
wrtc: wrtc
})

const conn = new Connection(toPull.duplex(channel))
let connected = false
Expand Down Expand Up @@ -107,7 +113,15 @@ function WebRTCStar () {
const sioUrl = 'http://' + ma.toString().split('/')[3] + ':' + ma.toString().split('/')[5]

listener.io = io.connect(sioUrl, sioOptions)

listener.io.on('connect_error', callback)
listener.io.on('error', (err) => {
console.log('got error')

listener.emit('error', err)
listener.emit('close')
})

listener.io.on('connect', () => {
listener.io.emit('ss-join', ma.toString())
listener.io.on('ws-handshake', incommingDial)
Expand All @@ -119,7 +133,10 @@ function WebRTCStar () {
function incommingDial (offer) {
if (offer.answer) { return }

const channel = new SimplePeer({ trickle: false })
const channel = new SimplePeer({
trickle: false,
wrtc: wrtc
})
const conn = new Connection(toPull.duplex(channel))

channel.on('connect', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
'use strict'

require('./signalling-server/test-signalling-server.js')
require('./webrtc-star/test-instance.js')
require('./webrtc-star/test-filter.js')
require('./webrtc-star/test-listen.js')
require('./webrtc-star/test-dial.js')
require('./webrtc-star/test-discovery.js')
require('./webrtc-star/test-valid-connection.js')
2 changes: 1 addition & 1 deletion test/signalling-server/test-signalling-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('signalling server', () => {
it('start and stop signalling server (default port)', (done) => {
const sigS = sigServer.start((err, info) => {
expect(err).to.not.exist
expect(info.port).to.equal(8134)
expect(info.port).to.equal(8135)
expect(info.protocol).to.equal('http')
expect(info.address).to.equal('0.0.0.0')
sigS.stop(done)
Expand Down
13 changes: 12 additions & 1 deletion test/webrtc-star/test-dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ describe('dial', () => {
it('dial on IPv4, check callback', (done) => {
ws1.dial(ma2, (err, conn) => {
expect(err).to.not.exist
done()

const data = new Buffer('some data')

pull(
pull.values([data]),
conn,
pull.collect((err, values) => {
expect(err).to.not.exist
expect(values).to.be.eql([data])
done()
})
)
})
})

Expand Down
2 changes: 2 additions & 0 deletions test/webrtc-star/test-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('listen', () => {

it('listen, check for callback', (done) => {
const listener = ws.createListener((conn) => {})

listener.listen(ma, (err) => {
expect(err).to.not.exist
listener.close(done)
Expand All @@ -25,6 +26,7 @@ describe('listen', () => {

it('listen, check for listening event', (done) => {
const listener = ws.createListener((conn) => {})

listener.on('listening', () => {
listener.close(done)
})
Expand Down

0 comments on commit 71018bf

Please sign in to comment.