javascript - Testing a Socket.io app, in which two connections interact -
i'm new unit testing nodejs, right want test socket.io server using mocha, app involves interaction of 2 sockets, spec following
var expect = require('expect.js'); var io = require('socket.io-client'); describe("server", function() { var options = { transports: ['websocket'], 'force new connection': true }; var socketone; var sockettwo; it("should stuff", function(done) { socketone = io.connect("http://localhost:3000"); socketone.on("connect", function(data) { sockettwo = io.connect("http://localhost:3000"); sockettwo.on("connect", function(data) { console.log("hellow world"); }); }); socketone.on("connection", function(x) { console.log(x); done(); }); }); });
by now, want test create 2 socket connections, when run it, seems "socketone" connecting server, sockettwo.on("connection") doesn't called. know doing wrong ?
the socket.io client recycle (multiplex) existing connection, why second 1 isn't firing connect event. prevent this, can disable multiplex specifying {multiplex: false}
in connect options. this:
socketone = io.connect("http://localhost:3000"); socketone.on("connect", function(data) { sockettwo = io.connect("http://localhost:3000", {multiplex: false}); sockettwo.on("connect", function(data) { console.log("hellow world"); }); });
Comments
Post a Comment