Add the WebRTC server in Cryptpad

pull/1/head
Yann Flory 9 years ago
parent c4f62fb812
commit 870b2dbb7e

@ -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')
}
}
})
})
}

@ -8,6 +8,7 @@ var Fs = require('fs');
var WebSocketServer = require('ws').Server; var WebSocketServer = require('ws').Server;
var ChainPadSrv = require('./ChainPadSrv'); var ChainPadSrv = require('./ChainPadSrv');
var NetfluxSrv = require('./NetFluxWebsocketServer'); var NetfluxSrv = require('./NetFluxWebsocketServer');
var WebRTCSrv = require('./WebRTCSrv');
var config = require('./config'); var config = require('./config');
config.websocketPort = config.websocketPort || config.httpPort; config.websocketPort = config.websocketPort || config.httpPort;
@ -81,5 +82,6 @@ var wsSrv = new WebSocketServer(wsConfig);
Storage.create(config, function (store) { Storage.create(config, function (store) {
console.log('DB connected'); console.log('DB connected');
// ChainPadSrv.create(wsSrv, store); // ChainPadSrv.create(wsSrv, store);
NetfluxSrv.run(store, wsSrv); // NetfluxSrv.run(store, wsSrv);
WebRTCSrv.run(store, wsSrv);
}); });

@ -113,8 +113,8 @@ define([
}; };
var options = { var options = {
// signaling: websocketUrl, signaling: websocketUrl,
signaling: 'ws://localhost:8000', // signaling: 'ws://localhost:8000',
key: channel key: channel
// topology: 'StarTopologyService', // topology: 'StarTopologyService',
// protocol: 'WebSocketProtocolService', // protocol: 'WebSocketProtocolService',

Loading…
Cancel
Save