29 lines
		
	
	
		
			711 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			29 lines
		
	
	
		
			711 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/*
 | 
						|
 *  cmd_websocket_chat.cpp
 | 
						|
 *  Author: Benjamin Sergeant
 | 
						|
 *  Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
 | 
						|
 */
 | 
						|
const WebSocket = require('ws');
 | 
						|
 | 
						|
const wss = new WebSocket.Server({ port: 8080 });
 | 
						|
 | 
						|
// Broadcast to all.
 | 
						|
wss.broadcast = function broadcast(data) {
 | 
						|
  wss.clients.forEach(function each(client) {
 | 
						|
    if (client.readyState === WebSocket.OPEN) {
 | 
						|
      client.send(data);
 | 
						|
    }
 | 
						|
  });
 | 
						|
};
 | 
						|
 | 
						|
wss.on('connection', function connection(ws) {
 | 
						|
  ws.on('message', function incoming(data) {
 | 
						|
    // Broadcast to everyone else.
 | 
						|
    wss.clients.forEach(function each(client) {
 | 
						|
      if (client !== ws && client.readyState === WebSocket.OPEN) {
 | 
						|
        client.send(data);
 | 
						|
      }
 | 
						|
    });
 | 
						|
  });
 | 
						|
});
 |