Advertisement
MrZep

WSChat

Mar 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const   WebSocketServer = new require('ws');
  2. let clients = {};
  3. const   wSS = new WebSocketServer.Server( { port: 8081 } );
  4.  
  5. wSS.on('connection', function( ws ) {                              
  6.     let id = Math.floor(Math.random() * 100000 + 1);
  7.     clients.id = ws;
  8.     console.log("новое соединение " + id);
  9.  
  10.     ws.on('message', function(message) {
  11.         message = JSON.parse(message);
  12.         console.log('От '+ message.login + ' получено сообщение: ' + message.message);
  13.  
  14.         for (let key of clients) { clients[key].send(message.message); }
  15.     });
  16.  
  17.     ws.on('close', function() {
  18.         console.log('соединение закрыто ' + id);
  19.         delete clients[id];
  20.     });
  21.  
  22. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement