server.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * libdatachannel example web server
  3. * Copyright (C) 2020 Lara Mackey
  4. * Copyright (C) 2020 Paul-Louis Ageneau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. const fs = require('fs');
  20. const http = require('http');
  21. const websocket = require('websocket');
  22. const staticFiles = {
  23. '/index.html': 'text/html',
  24. '/style.css': 'text/css',
  25. '/script.js': 'text/javascript',
  26. };
  27. const clients = {};
  28. const httpServer = http.createServer((req, res) => {
  29. console.log(`${req.method.toUpperCase()} ${req.url}`);
  30. const respond = (code, data, contentType = 'text/plain') => {
  31. res.writeHead(code, {
  32. 'Content-Type': contentType,
  33. 'Access-Control-Allow-Origin': '*',
  34. });
  35. res.end(data);
  36. };
  37. if(req.method != 'GET') {
  38. respond(405, 'Method not allowed');
  39. return;
  40. }
  41. const url = req.url == '/' ? '/index.html' : req.url;
  42. const contentType = staticFiles[url];
  43. if(!contentType) {
  44. respond(404, 'Not found');
  45. return;
  46. }
  47. fs.readFile(__dirname + url, (err, data) => {
  48. if(err) {
  49. respond(500, 'Missing file');
  50. return;
  51. }
  52. respond(200, data, contentType);
  53. });
  54. });
  55. const wsServer = new websocket.server({ httpServer });
  56. wsServer.on('request', (req) => {
  57. console.log(`WS ${req.resource}`);
  58. const { path } = req.resourceURL;
  59. const splitted = path.split('/');
  60. splitted.shift();
  61. const id = splitted[0];
  62. const conn = req.accept(null, req.origin);
  63. conn.on('message', (data) => {
  64. if(data.type === 'utf8') {
  65. console.log(`Client ${id} << ${data.utf8Data}`);
  66. const message = JSON.parse(data.utf8Data);
  67. const destId = message.id;
  68. const dest = clients[destId];
  69. if(dest) {
  70. message.id = id;
  71. const data = JSON.stringify(message);
  72. console.log(`Client ${destId} >> ${data}`);
  73. dest.send(data);
  74. }
  75. else {
  76. console.error(`Client ${destId} not found`);
  77. }
  78. }
  79. });
  80. conn.on('close', () => {
  81. delete clients[id];
  82. console.error(`Client ${id} disconnected`);
  83. });
  84. clients[id] = conn;
  85. });
  86. httpServer.listen(8000);