diff --git a/WebRTCSrv.js b/WebRTCSrv.js new file mode 100644 index 000000000..06dfd61f4 --- /dev/null +++ b/WebRTCSrv.js @@ -0,0 +1,68 @@ +'use strict' +let WebSocketServer = require('ws').Server +const PORT = 8000 +const UNSUPPORTED_DATA = 1007 +const POLICY_VIOLATION = 1008 +const CLOSE_UNSUPPORTED = 1003 + +// let server = new WebSocketServer({port: PORT}, () => { + // console.log('Server runs on: ws://localhost:' + PORT) +// }) + +var run = module.exports.run = function(storage, server) { + server.on('connection', (socket) => { + socket.on('message', (data) => { + try { + let msg = JSON.parse(data) + console.log(msg); + if (msg.hasOwnProperty('key')) { + for (let master of server.clients) { + if (master.key === msg.key) { + socket.close(POLICY_VIOLATION, 'The key already exists') + console.log('ERROR key exists'); + return + } + } + socket.key = msg.key + socket.joiningClients = [] + } else if (msg.hasOwnProperty('id')) { + for (let index in socket.joiningClients) { + if (index == msg.id) { + socket.joiningClients[index].send(JSON.stringify({data: msg.data})) + return + } + } + socket.close(POLICY_VIOLATION, 'Unknown id') + } else if (msg.hasOwnProperty('join')) { + for (let master of server.clients) { + if (master.key === msg.join) { + console.log('joined'); + socket.master = master + master.joiningClients.push(socket) + let id = master.joiningClients.length - 1 + master.send(JSON.stringify({id, data: msg.data})) + return + } + } + console.log('ERROR unknown key'); + socket.close(POLICY_VIOLATION, 'Unknown key') + } else if (msg.hasOwnProperty('data') && socket.hasOwnProperty('master')) { + let id = socket.master.joiningClients.indexOf(socket) + socket.master.send(JSON.stringify({id, data: msg.data})) + } else { + socket.close(UNSUPPORTED_DATA, 'Unsupported message format') + } + } catch (event) { + socket.close(CLOSE_UNSUPPORTED, 'Server accepts only JSON') + } + }) + + socket.on('close', (event) => { + if (socket.hasOwnProperty('joiningClients')) { + for (let client of socket.joiningClients) { + client.close(POLICY_VIOLATION, 'The peer is no longer available') + } + } + }) + }) +} \ No newline at end of file diff --git a/server.js b/server.js index 3bfacb792..538efce28 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,7 @@ var Fs = require('fs'); var WebSocketServer = require('ws').Server; var ChainPadSrv = require('./ChainPadSrv'); var NetfluxSrv = require('./NetFluxWebsocketServer'); +var WebRTCSrv = require('./WebRTCSrv'); var config = require('./config'); config.websocketPort = config.websocketPort || config.httpPort; @@ -81,5 +82,6 @@ var wsSrv = new WebSocketServer(wsConfig); Storage.create(config, function (store) { console.log('DB connected'); // ChainPadSrv.create(wsSrv, store); - NetfluxSrv.run(store, wsSrv); + // NetfluxSrv.run(store, wsSrv); + WebRTCSrv.run(store, wsSrv); }); diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index 787e66acb..17eb724f5 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -113,8 +113,8 @@ define([ }; var options = { - // signaling: websocketUrl, - signaling: 'ws://localhost:8000', + signaling: websocketUrl, + // signaling: 'ws://localhost:8000', key: channel // topology: 'StarTopologyService', // protocol: 'WebSocketProtocolService',