server.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const path = require('path');
  2. const express = require('express');
  3. const WebSocket = require('ws');
  4. const app = express();
  5. const path1 = require('path');
  6. const express1 = require('express');
  7. const WebSocket1 = require('ws');
  8. const path2 = require('path');
  9. const express2 = require('express');
  10. const WebSocket2 = require('ws');
  11. const path3 = require('path');
  12. const express3 = require('express');
  13. const WebSocket3 = require('ws');
  14. const path4 = require('path');
  15. const express4 = require('express');
  16. const WebSocket4 = require('ws');
  17. const path5 = require('path');
  18. const express5 = require('express');
  19. const WebSocket5 = require('ws');
  20. const WS_PORT = 8088;
  21. const HTTP_PORT = 8000;
  22. const WS_PORT1 = 8089;
  23. const WS_PORT2 = 8090;
  24. const WS_PORT3 = 8091;
  25. const WS_PORT4 = 8092;
  26. const WS_PORT5 = 8093;
  27. const wsServer = new WebSocket.Server({port: WS_PORT}, ()=> console.log(`WS Server is listening at ${WS_PORT}`));
  28. let connectedClients = [];
  29. wsServer.on('connection', (ws, req)=>{
  30. console.log('Connected');
  31. connectedClients.push(ws);
  32. ws.on('message', data => {
  33. connectedClients.forEach((ws,i)=>{
  34. if(ws.readyState === ws.OPEN){
  35. ws.send(data);
  36. }else{
  37. connectedClients.splice(i ,1);
  38. }
  39. })
  40. });
  41. });
  42. const wsServer1 = new WebSocket.Server({port: WS_PORT1}, ()=> console.log(`WS Server is listening at ${WS_PORT1}`));
  43. let connectedClients1 = [];
  44. wsServer1.on('connection', (ws1, req)=>{
  45. console.log('Connected');
  46. connectedClients1.push(ws1);
  47. ws1.on('message', data1 => {
  48. connectedClients1.forEach((ws1,i)=>{
  49. if(ws1.readyState === ws1.OPEN){
  50. ws1.send(data1);
  51. }else{
  52. connectedClients1.splice(i ,1);
  53. }
  54. })
  55. });
  56. });
  57. const wsServer2 = new WebSocket.Server({port: WS_PORT2}, ()=> console.log(`WS Server is listening at ${WS_PORT2}`));
  58. let connectedClients2 = [];
  59. wsServer2.on('connection', (ws2, req)=>{
  60. console.log('Connected');
  61. connectedClients2.push(ws2);
  62. ws2.on('message', data2 => {
  63. connectedClients2.forEach((ws2,i)=>{
  64. if(ws2.readyState === ws2.OPEN){
  65. ws2.send(data2);
  66. }else{
  67. connectedClients2.splice(i ,1);
  68. }
  69. })
  70. });
  71. });
  72. const wsServer3 = new WebSocket.Server({port: WS_PORT3}, ()=> console.log(`WS Server is listening at ${WS_PORT3}`));
  73. let connectedClients3 = [];
  74. wsServer3.on('connection', (ws3, req)=>{
  75. console.log('Connected');
  76. connectedClients3.push(ws3);
  77. ws3.on('message', data3 => {
  78. connectedClients3.forEach((ws3,i)=>{
  79. if(ws3.readyState === ws3.OPEN){
  80. ws3.send(data3);
  81. }else{
  82. connectedClients3.splice(i ,1);
  83. }
  84. })
  85. });
  86. });
  87. const wsServer4 = new WebSocket.Server({port: WS_PORT4}, ()=> console.log(`WS Server is listening at ${WS_PORT4}`));
  88. let connectedClients4 = [];
  89. wsServer4.on('connection', (ws4, req)=>{
  90. console.log('Connected');
  91. connectedClients4.push(ws4);
  92. ws4.on('message', data4 => {
  93. connectedClients4.forEach((ws4,i)=>{
  94. if(ws4.readyState === ws4.OPEN){
  95. ws4.send(data4);
  96. }else{
  97. connectedClients4.splice(i ,1);
  98. }
  99. })
  100. });
  101. });
  102. const wsServer5 = new WebSocket.Server({port: WS_PORT5}, ()=> console.log(`WS Server is listening at ${WS_PORT5}`));
  103. let connectedClients5 = [];
  104. wsServer5.on('connection', (ws5, req)=>{
  105. console.log('Connected');
  106. connectedClients5.push(ws5);
  107. ws5.on('message', data5 => {
  108. connectedClients5.forEach((ws5,i)=>{
  109. if(ws5.readyState === ws5.OPEN){
  110. ws5.send(data5);
  111. }else{
  112. connectedClients5.splice(i ,1);
  113. }
  114. })
  115. });
  116. });
  117. app.get('/client',(req,res)=>res.sendFile(path.resolve(__dirname, '/home/gs1801/CoffeeProject/app/templates/video.html')));
  118. app.listen(HTTP_PORT, ()=> console.log(`HTTP server listening at ${HTTP_PORT}`));