ws-server.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var fs = require('fs');
  3. var parser = require('ua-parser-js');
  4. // Serve client side statically
  5. var express = require('express');
  6. var app = express();
  7. app.use(express.static(__dirname + '/public'));
  8. // var https = require('https');
  9. // var server = https.createServer({
  10. // key: fs.readFileSync('/var/www/sharewithme/ssl/privkey.pem').toString(),
  11. // cert: fs.readFileSync('/var/www/sharewithme/ssl/fullchain.pem').toString()
  12. // }, app);
  13. var http = require('http');
  14. var server = http.createServer(app);
  15. // Start Binary.js server
  16. var BinaryServer = require('binaryjs').BinaryServer;
  17. // link it to express
  18. var bs = BinaryServer({
  19. server: server
  20. });
  21. function getDeviceName(req) {
  22. var ua = parser(req.headers['user-agent']);
  23. return {
  24. model: ua.device.model,
  25. os: ua.os.name,
  26. browser: ua.browser.name,
  27. type: ua.device.type
  28. };
  29. }
  30. // Wait for new user connections
  31. bs.on('connection', function(client) {
  32. console.log('connection received!');
  33. client.deviceName = getDeviceName(client._socket.upgradeReq);
  34. // Incoming stream from browsers
  35. client.on('stream', function(stream, meta) {
  36. console.log('stream received!', meta);
  37. if (meta.handshake) {
  38. client.uuid = meta.handshake;
  39. return;
  40. }
  41. meta.from = client.uuid;
  42. // broadcast to all other clients
  43. for (var id in bs.clients) {
  44. if (bs.clients.hasOwnProperty(id)) {
  45. var otherClient = bs.clients[id];
  46. if (otherClient !== client && meta.toPeer === otherClient.uuid) {
  47. var send = otherClient.createStream(meta);
  48. stream.pipe(send, meta);
  49. }
  50. }
  51. }
  52. });
  53. });
  54. function forEachClient(fn) {
  55. for (var id in bs.clients) {
  56. if (bs.clients.hasOwnProperty(id)) {
  57. var client = bs.clients[id];
  58. fn(client);
  59. }
  60. }
  61. }
  62. function getIP(socket) {
  63. return socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress;
  64. }
  65. function notifyBuddies() {
  66. //TODO: This should be possible in linear time
  67. forEachClient(function(client1) {
  68. var buddies = [];
  69. var myIP = getIP(client1._socket);
  70. forEachClient(function(client2) {
  71. var otherIP = getIP(client2._socket);
  72. console.log(myIP, otherIP);
  73. if (client1 !== client2 && myIP === otherIP) {
  74. buddies.push({
  75. peerId: client2.uuid,
  76. name: client2.deviceName
  77. });
  78. }
  79. });
  80. var msg = {
  81. buddies: buddies,
  82. isSystemEvent: true,
  83. type: 'buddies'
  84. };
  85. client1.send(msg);
  86. });
  87. }
  88. setInterval(notifyBuddies, 4000);
  89. server.listen(9001);
  90. console.log('HTTP and BinaryJS server started on port 9001');