Guest User

Untitled

a guest
Jun 18th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. const WebSocket = require('ws');
  2.  
  3. // Initialize a new WebSocket server on port 5001
  4. const PORT = process.env.PORT || 5001;
  5. const wss = new WebSocket.Server({ port: PORT });
  6. // This object will store rooms and the clients within them
  7. const rooms = {};
  8.  
  9. console.log('Signaling server is running on port 5001');
  10.  
  11. wss.on('connection', ws => {
  12. console.log('Client connected');
  13.  
  14. // Handle messages from the client
  15. ws.on('message', message => {
  16. let data;
  17. try {
  18. data = JSON.parse(message);
  19. } catch (e) {
  20. console.error('Invalid JSON received:', message);
  21. return;
  22. }
  23.  
  24. const { type, roomId, ...payload } = data;
  25.  
  26. // Use a switch statement to handle different message types
  27. switch (type) {
  28. // When a user wants to join a room
  29. case "join_room":
  30. currentRoom = data.roomId;
  31.  
  32. if (!rooms[currentRoom]) {
  33. rooms[currentRoom] = [];
  34. }
  35.  
  36. rooms[currentRoom].push(ws);
  37.  
  38. console.log(`Client joined room: ${currentRoom}`);
  39.  
  40. // Notify the first peer that a second peer has joined
  41. if (rooms[currentRoom].length === 2) {
  42. rooms[currentRoom][0].send(JSON.stringify({ type: "peer_joined" }));
  43. }
  44. break;
  45.  
  46. // Forwarding offer, answer, and candidate messages to the other client in the room
  47. case 'offer':
  48. case 'answer':
  49. case 'candidate': {
  50. console.log(`Forwarding message of type '${type}' in room ${roomId}`);
  51. const room = rooms[roomId];
  52. if (room) {
  53. // Send the message to every other client in the room
  54. room.forEach(client => {
  55. if (client !== ws && client.readyState === WebSocket.OPEN) {
  56. client.send(JSON.stringify({ type, ...payload }));
  57. }
  58. });
  59. }
  60. break;
  61. }
  62.  
  63. default:
  64. console.warn(`Unknown message type received: ${type}`);
  65. }
  66. });
  67.  
  68. // Handle client disconnection
  69. ws.on('close', () => {
  70. console.log('Client disconnected');
  71. const { roomId } = ws; // Get the roomId that was stored on the connection
  72. if (roomId && rooms[roomId]) {
  73. // Notify the other peer that their partner has left
  74. rooms[roomId].forEach(client => {
  75. if (client !== ws && client.readyState === WebSocket.OPEN) {
  76. client.send(JSON.stringify({ type: 'peer_left' }));
  77. }
  78. });
  79.  
  80. // Remove the disconnected client from the room
  81. rooms[roomId] = rooms[roomId].filter(client => client !== ws);
  82.  
  83. // If the room is now empty, delete it
  84. if (rooms[roomId].length === 0) {
  85. console.log(`Room ${roomId} is empty and has been deleted.`);
  86. delete rooms[roomId];
  87. }
  88. }
  89. });
  90.  
  91. ws.on('error', (error) => {
  92. console.error('WebSocket error:', error);
  93. });
  94. });
  95.  
Advertisement
Add Comment
Please, Sign In to add comment