From ae8f6f7f2c5cebee3ef8939a7fbb75c5ac127210 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Thu, 10 Mar 2016 14:03:31 +0100 Subject: [PATCH] Ability to choose which protocol to use (Websocket or WebRTC) with Netflux --- WebRTCSrv.js | 35 +------ config.js.dist | 2 + customize.dist/index.html | 6 +- server.js | 43 +++++--- www/common/netflux.js | 119 +++++++++++---------- www/common/realtime-input.js | 196 +++++++++++++++++------------------ www/hack/main.js | 1 + www/vdom/main.js | 1 + 8 files changed, 197 insertions(+), 206 deletions(-) diff --git a/WebRTCSrv.js b/WebRTCSrv.js index 8b0c78321..c97c18b79 100644 --- a/WebRTCSrv.js +++ b/WebRTCSrv.js @@ -1,11 +1,10 @@ 'use strict' let WebSocketServer = require('ws').Server -const PORT = 8000 const UNSUPPORTED_DATA = 1007 const POLICY_VIOLATION = 1008 const CLOSE_UNSUPPORTED = 1003 -var run = module.exports.run = function(storage, server) { +var run = module.exports.run = function(server) { server.on('connection', (socket) => { socket.on('message', (data) => { try { @@ -15,7 +14,6 @@ var run = module.exports.run = function(storage, server) { 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 } } @@ -32,16 +30,13 @@ var run = module.exports.run = function(storage, server) { } 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 - console.log(id); 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) @@ -55,31 +50,11 @@ var run = module.exports.run = function(storage, server) { }) socket.on('close', (event) => { - console.log('someone has closed'); - // If not master - if (socket.hasOwnProperty('master')) { - let masterClients = socket.master.joiningClients - for (let client of masterClients) { - if(client.id === socket.id) { - console.log('close client '+client.key) - client.close(POLICY_VIOLATION, 'The peer is no longer available') - //masterClients.splice(masterClients.indexOf(client),1); - } + if (socket.hasOwnProperty('joiningClients')) { + for (let client of socket.joiningClients) { + client.close(POLICY_VIOLATION, 'The peer is no longer available') } } - else if (socket.hasOwnProperty('joiningClients')) { - let firstClient - let masterClients = socket.joiningClients - for (let client of masterClients) { - firstClient = client - break; - } - firstClient.close(POLICY_VIOLATION, 'The master is no longer available') - //masterClients.splice(masterClients.indexOf(firstClient),1); - firstClient.joiningClients = masterClients - console.log('change master from '+socket.key+' to '+firstClient.key) - socket = firstClient - } - }) + }); }) } \ No newline at end of file diff --git a/config.js.dist b/config.js.dist index f61828e4c..56d7a1fc8 100644 --- a/config.js.dist +++ b/config.js.dist @@ -11,6 +11,8 @@ module.exports = { httpPort: 3000, // the port used for websockets websocketPort: 3001, + // the port used for webrtc (uncomment to use the WebRTC server) + // webrtcPort: 3002, // You now have a choice of storage engines diff --git a/customize.dist/index.html b/customize.dist/index.html index b652894e6..69e2eaa9f 100644 --- a/customize.dist/index.html +++ b/customize.dist/index.html @@ -123,9 +123,12 @@