mock-socket icon indicating copy to clipboard operation
mock-socket copied to clipboard

Multiple sockets share connection

Open Ghirigoro opened this issue 9 years ago • 5 comments

If you create multiple sockets pointing to the same server they will all share a single connection (i.e. when one is connected they all are, when a message is sent to one they all receive it, etc.)

NOTE: I ran into this running mocha tests in Node.

Ghirigoro avatar Mar 12 '15 15:03 Ghirigoro

@Ghirigoro thanks for finding this. let me look into it and send an update.

thoov avatar Mar 13 '15 05:03 thoov

window.WebSocket = MockSocket;

var wss = new MockServer('ws://localhost:8281');

wss.on('connection', function(ws) {
  ws.send("msgSSS"); //This seems to broadcast to all clients, when I would assume it would only send to the current client.
});

var sock1 = new WebSocket('ws://localhost:8281');


var sock1MessageCount = 0;

sock1.onmessage = function(e) {
    sock1MessageCount ++;
    console.log("sock1 onmessageCount:" + sock1MessageCount)
};


var sock2 = new WebSocket('ws://localhost:8281');

var sock2MessageCount = 0;

sock2.onmessage = function(e) {
  sock2MessageCount ++;
  console.log("sock2 onmessageCount:" + sock2MessageCount)
};

console output

sock1 onmessageCount:1
sock2 onmessageCount:1
sock1 onmessageCount:2
sock2 onmessageCount:2

nicholasjstock avatar Apr 30 '15 11:04 nicholasjstock

@nicholasjstock I find console output in your example quite correct. All clients connected to the same server should receive the message that server sends into connection. You didn't specify the client you want to send you message to, so all of them receive it.

vlizard avatar May 13 '15 08:05 vlizard

@vlizard The code that was posted is not broadcasting, so the output is not correct. Those messages are being sent to a specific socket (i.e. ws.send("msgSSS") is sending a message through ws, not to all clients).

Ghirigoro avatar May 13 '15 13:05 Ghirigoro

Here's a failing test around the broadcast example at https://github.com/websockets/ws#broadcast-example. It throws:

  1) Sockets broadcast:
     Uncaught TypeError: server.clients.forEach is not a function
      at Server.server.broadcast (test/test.js:22:20)
      at Server.<anonymous> (test/test.js:33:14)
      at node_modules/mock-socket/dist/webpack:/src/event-target.js:64:18
      at Array.forEach (native)
      at Server.dispatchEvent (node_modules/mock-socket/dist/webpack:/src/event-target.js:62:15)
      at WebSocket.delayCallback (node_modules/mock-socket/dist/webpack:/src/websocket.js:113:18)
      at Timeout._onTimeout (node_modules/mock-socket/dist/webpack:/src/helpers/delay.js:10:41)

import assert from 'assert'
import { WebSocket, Server } from 'mock-socket'

function Client () {
  const chatSocket = new WebSocket('ws://localhost:8080')
  this.messages = []

  chatSocket.onmessage = (event) => {
    this.messages.push(event.data)
  }
}

let server, client1, client2

beforeEach(() => {
  server = new Server('ws://localhost:8080')
  client1 = new Client()
  client2 = new Client()

  server.broadcast = (data) => {
    server.clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(data)
      }
    })
  }
})

describe('Sockets', () => {
  it('broadcast', (done) => {
    server.on('connection', server => {
      server.broadcast('test message 1')
      server.broadcast('test message 2')
    })

    setTimeout(() => {
      assert.equal(client1.messages.length, 2, '2 messages passed')
      assert.equal(client2.messages.length, 2, '2 messages passed')
      server.stop(done)
    }, 100)
  })
})

colwilson avatar May 16 '17 06:05 colwilson