gpt4 book ai didi

javascript - 无法连接到 socket.io 两次

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:43 25 4
gpt4 key购买 nike

我写了一个socket.io 服务器,我正在使用mocha 来测试它。但是我无法第二次连接到服务器。

这是我的代码,它是用 CoffeeScript 写的。

服务器:

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

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

server.listen(3000)

测试:

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()

服务器输出:

   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 输出:

  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)

最佳答案

当您想创建新 session 时 - 为 .connect 方法设置“强制新连接”选项

io.connect(socketAddress,{'force new connection': true});

关于javascript - 无法连接到 socket.io 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com