Advertisement
CSenshi

Untitled

Aug 19th, 2019
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. // http://127.0.0.1:9001
  2. // http://localhost:9001
  3.  
  4. var server = require('https'),
  5. url = require('url'),
  6. path = require('path'),
  7. fs = require('fs');
  8.  
  9. var options = {
  10. key: fs.readFileSync('/etc/letsencrypt/live/urprecious.org/privkey.pem'),
  11. cert: fs.readFileSync('/etc/letsencrypt/live/urprecious.org/fullchain.pem'),
  12. }
  13.  
  14.  
  15. function serverHandler(request, response) {
  16. var uri = url.parse(request.url).pathname,
  17. filename = path.join(process.cwd(), uri);
  18.  
  19. fs.exists(filename, function (exists) {
  20. if (!exists) {
  21. response.writeHead(404, {
  22. 'Content-Type': 'text/plain'
  23. });
  24. response.write('404 Not Found: ' + filename + '\n');
  25. response.end();
  26. return;
  27. }
  28.  
  29. if (filename.indexOf('favicon.ico') !== -1) {
  30. return;
  31. }
  32.  
  33. var isWin = !!process.platform.match(/^win/);
  34.  
  35. if (fs.statSync(filename).isDirectory() && !isWin) {
  36. filename += '/index.html';
  37. } else if (fs.statSync(filename).isDirectory() && !!isWin) {
  38. filename += '\\index.html';
  39. }
  40.  
  41. fs.readFile(filename, 'binary', function (err, file) {
  42. if (err) {
  43. response.writeHead(500, {
  44. 'Content-Type': 'text/plain'
  45. });
  46. response.write(err + '\n');
  47. response.end();
  48. return;
  49. }
  50.  
  51. var contentType;
  52.  
  53. if (filename.indexOf('.html') !== -1) {
  54. contentType = 'text/html';
  55. }
  56.  
  57. if (filename.indexOf('.js') !== -1) {
  58. contentType = 'application/javascript';
  59. }
  60.  
  61. if (contentType) {
  62. response.writeHead(200, {
  63. 'Content-Type': contentType
  64. });
  65. } else response.writeHead(200);
  66.  
  67. response.write(file, 'binary');
  68. response.end();
  69. });
  70. });
  71. }
  72.  
  73. // var config = {
  74. // "socketURL": "/",
  75. // "dirPath": "",
  76. // "homePage": "/",
  77. // "socketMessageEvent": "RTCMultiConnection-Message",
  78. // "socketCustomEvent": "RTCMultiConnection-Custom-Message",
  79. // "port": 9001,
  80. // "enableLogs": false,
  81. // "isUseHTTPs": false,
  82. // "enableAdmin": false
  83. // };
  84.  
  85. var config = {
  86. "socketURL": "/",
  87. "dirPath": "",
  88. "homePage": "/demos/index.html",
  89. "socketMessageEvent": "RTCMultiConnection-Message",
  90. "socketCustomEvent": "RTCMultiConnection-Custom-Message",
  91. "port": "9001",
  92. "enableLogs": "false",
  93. "autoRebootServerOnFailure": "false",
  94. "isUseHTTPs": "true",
  95. "sslKey": "/etc/letsencrypt/live/urprecious.org/privkey.pem",
  96. "sslCert": "/etc/letsencrypt/live/urprecious.org/cert.pem",
  97. "sslCabundle": "",
  98. "enableAdmin": "true",
  99. "adminUserName": "username",
  100. "adminPassword": "password"
  101. };
  102.  
  103. var RTCMultiConnectionServer = require('rtcmulticonnection-server');
  104. var ioServer = require('socket.io');
  105.  
  106. var app = server.createServer(options, serverHandler);
  107. RTCMultiConnectionServer.beforeHttpListen(app, config);
  108. app = app.listen(process.env.PORT || 9001, process.env.IP || "0.0.0.0", function () {
  109. RTCMultiConnectionServer.afterHttpListen(app, config);
  110. });
  111.  
  112. // --------------------------
  113. // socket.io codes goes below
  114.  
  115. ioServer(app).on('connection', function (socket) {
  116. RTCMultiConnectionServer.addSocket(socket, config);
  117.  
  118. // ----------------------
  119. // below code is optional
  120.  
  121. const params = socket.handshake.query;
  122.  
  123. if (!params.socketCustomEvent) {
  124. params.socketCustomEvent = 'custom-message';
  125. }
  126.  
  127. socket.on(params.socketCustomEvent, function (message) {
  128. socket.broadcast.emit(params.socketCustomEvent, message);
  129. });
  130. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement