Advertisement
MrZep

Untitled

Mar 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const   WebSocketServer = new require('ws');
  2. const   wSS = new WebSocketServer.Server( { port: 8081 } );
  3.  
  4. wSS.broadcast = function(data, clientValidator = () => true) {            
  5.     this.clients.forEach(client => {
  6.             client.send(data);
  7.     });
  8. };
  9.  
  10. wSS.on("connection", ws => {
  11.     // событие будет вызвано, когда клиент отправит сообщение
  12.     ws.on('message', message => {
  13.         //  отправляем сообщение всем, кроме автора
  14.         wSS.broadcast(message, client => client !== ws);
  15.     });
  16. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement