mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-25 17:26:18 -04:00
Lots of small improvements, websockets fallback
This commit is contained in:
parent
e5eab64c6b
commit
22be7c5cb9
35 changed files with 2672 additions and 916 deletions
106
server/ws-server.js
Normal file
106
server/ws-server.js
Normal file
|
@ -0,0 +1,106 @@
|
|||
'use strict';
|
||||
var fs = require('fs');
|
||||
var parser = require('ua-parser-js');
|
||||
|
||||
// Serve client side statically
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
|
||||
var https = require('https');
|
||||
var server = https.createServer({
|
||||
key: fs.readFileSync('/var/www/sharewithme/ssl/privkey.pem').toString(),
|
||||
cert: fs.readFileSync('/var/www/sharewithme/ssl/fullchain.pem').toString()
|
||||
}, app);
|
||||
|
||||
// var http = require('http');
|
||||
// var server = http.createServer(app);
|
||||
|
||||
// Start Binary.js server
|
||||
var BinaryServer = require('binaryjs').BinaryServer;
|
||||
|
||||
// link it to express
|
||||
var bs = BinaryServer({
|
||||
server: server
|
||||
});
|
||||
|
||||
function getDeviceName(req) {
|
||||
var ua = parser(req.headers['user-agent']);
|
||||
return {
|
||||
model: ua.device.model,
|
||||
os: ua.os.name,
|
||||
browser: ua.browser.name,
|
||||
type: ua.device.type
|
||||
};
|
||||
}
|
||||
// Wait for new user connections
|
||||
bs.on('connection', function(client) {
|
||||
console.log('connection received!');
|
||||
|
||||
|
||||
client.deviceName = getDeviceName(client._socket.upgradeReq);
|
||||
|
||||
// Incoming stream from browsers
|
||||
client.on('stream', function(stream, meta) {
|
||||
console.log('stream received!', meta);
|
||||
if (meta.handshake) {
|
||||
client.uuid = meta.handshake;
|
||||
return;
|
||||
}
|
||||
meta.from = client.uuid;
|
||||
|
||||
// broadcast to all other clients
|
||||
for (var id in bs.clients) {
|
||||
if (bs.clients.hasOwnProperty(id)) {
|
||||
var otherClient = bs.clients[id];
|
||||
if (otherClient !== client && meta.toPeer === otherClient.uuid) {
|
||||
var send = otherClient.createStream(meta);
|
||||
stream.pipe(send, meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
function forEachClient(fn) {
|
||||
for (var id in bs.clients) {
|
||||
if (bs.clients.hasOwnProperty(id)) {
|
||||
var client = bs.clients[id];
|
||||
fn(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getIP(socket) {
|
||||
return socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress;
|
||||
}
|
||||
|
||||
function notifyBuddies() {
|
||||
//TODO: This should be possible in linear time
|
||||
forEachClient(function(client1) {
|
||||
var buddies = [];
|
||||
var myIP = getIP(client1._socket);
|
||||
forEachClient(function(client2) {
|
||||
var otherIP = getIP(client2._socket);
|
||||
console.log(myIP, otherIP);
|
||||
if (client1 !== client2 && myIP === otherIP) {
|
||||
buddies.push({
|
||||
peerId: client2.uuid,
|
||||
name: client2.deviceName
|
||||
});
|
||||
}
|
||||
});
|
||||
var msg = {
|
||||
buddies: buddies,
|
||||
isSystemEvent: true,
|
||||
type: 'buddies'
|
||||
};
|
||||
client1.send(msg);
|
||||
});
|
||||
}
|
||||
setInterval(notifyBuddies, 4000);
|
||||
|
||||
server.listen(9001);
|
||||
console.log('HTTP and BinaryJS server started on port 9001');
|
Loading…
Add table
Add a link
Reference in a new issue