User Tools

Site Tools


njsetn:broadcasting_messages_with_websockets
// client.js
document.forms[0].onsubmit = function() {
  var input = document.getElementById('message');
  ws.send(input.value);
  input.value = ''; 
// server
 (..)
  wss.on("connection", function(ws) {
    ws.on("message", function(message) {
      if (message === 'exit') {
        ws.close();
      } else {
        ws.clients.forEach(function(client) {
          client.send(message);
        });
      }
    });
    ws.send("Welcome to the chat");
  });
njsetn/broadcasting_messages_with_websockets.txt · Last modified: 2016/03/28 12:29 by leo