Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to socket.io twice

I have write a socket.io server, and I'm using mocha to test it. But the the I cannot connect to server at the second time.

Here is my code, it's written by coffee script.

Server:

server = require('http').Server()
io = require('socket.io').listen(server)

io.on 'connection', (socket) ->
  console.log socket.id

server.listen(3000)

Test:

chai = require 'chai'
io = require 'socket.io-client'

should = chai.should()

describe 'Socket', ()->

  url = 'http://0.0.0.0:3000'

  it 'should be connected', (done) ->
    client = io.connect(url)
    client.on 'connect', (data) ->
      console.log(data)
      client.socket.connected.should.equal(true)
      client.disconnect()
      done()


describe 'User', ()->

  url = 'http://0.0.0.0:3000'

  it 'should be connected', (done) ->
    client = io.connect(url)
    client.on 'connect', (data) ->
      console.log(data)
      client.socket.connected.should.equal(true)
      client.disconnect()
      done()

Server output:

   info  - socket.io started
   debug - client authorized
   info  - handshake authorized b_2h4dCr-YfD-7-Iw9Hl
   debug - setting request GET /socket.io/1/websocket/b_2h4dCr-YfD-7-Iw9Hl
   debug - set heartbeat interval for client b_2h4dCr-YfD-7-Iw9Hl
   debug - client authorized for 
   debug - websocket writing 1::
b_2h4dCr-YfD-7-Iw9Hl
   debug - got disconnection packet
   info  - transport end by forced client disconnection
   debug - websocket writing 0::
   info  - transport end (booted)
   debug - set close timeout for client b_2h4dCr-YfD-7-Iw9Hl
   debug - cleared close timeout for client b_2h4dCr-YfD-7-Iw9Hl
   debug - cleared heartbeat interval for client b_2h4dCr-YfD-7-Iw9Hl
   debug - discarding transport

Mocha output:

  Socket
    ◦ should be connected: undefined
    ✓ should be connected (75ms)

  User
    1) should be connected


  1 passing (2 seconds)
  1 failing

  1) User should be connected:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (/home/wangbin/webapp/chat_server/node_modules/mocha/lib/runnable.js:165:14)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
like image 751
Bin Wang Avatar asked Jan 12 '23 15:01

Bin Wang


1 Answers

When you want to create new session - set 'force new connection' option for .connect method

io.connect(socketAddress,{'force new connection': true});
like image 94
Vladimir Kurijov Avatar answered Jan 22 '23 02:01

Vladimir Kurijov