socket.io / websocket-clientを適切にシャットダウンするにはどうすればよいですか? -- 関連しているが、時代遅れ/機能しなかった
サーバー側ソケットをテストするための一連のテストを作成しています。github で jamescarrからコードを取得しました。Carr のコードは、Liam Kaufman のhereに基づいています。 着信テキストの壁。
これはテスト側です。
ioServer = require("socket.io")
ioClient = require("socket.io-client")
chai.should()
chai.use(sinonChai)
expect = chai.expect
server = require('./testUtils/chat-server.coffee')
socketURL = 'http://localhost:5000'
options =
transports: ['websockets']
'force new connection':true
chatUser1 = name:'Tom'
chatUser2 = name:'Sally'
chatUser3 = name:'Dana'
describe "Chat Server", ->
before (done) ->
server.start 5000, done
after (done) ->
server.stop done
it "Should broadcast new user to all users", (done) ->
console.log "new user to all users"
client1 = ioClient.connect(socketURL)
client1.on "connect", (data) ->
client1.emit "connection name", chatUser1
client2 = ioClient.connect(socketURL, options)
client2.on "connect", (data) ->
client2.emit "connection name", chatUser2
client2.on "new user", (usersName) ->
usersName.should.equal chatUser2.name + " has joined."
client2.disconnect()
numUsers = 0
client1.on "new user", (usersName) ->
numUsers += 1
if numUsers is 2
usersName.should.equal chatUser2.name + " has joined."
client1.disconnect()
done()
これはサーバー側です。
io = null
clients = {}
module.exports =
start: (port, cb) ->
io = require("socket.io").listen port, cb
io.sockets.on "connection", (socket) ->
console.log "server on connection"
userName = ''
socket.on "connection name", (user) ->
console.log "server on connection name"
clients[user.name] = socket
userName = user.name
io.sockets.emit "new user", user.name + " has joined."
socket.on 'message', (msg) ->
console.log "server on message"
io.sockets.emit 'message', msg
socket.on 'private message', (msg) ->
console.log "server on private message"
fromMsg =
from: userName
txt: msg.txt
clients[msg.to].emit 'private message', fromMsg
stop: (cb) ->
io.server.close()
cb()
何らかの理由で、何をしても、接続でタイムアウトが発生します。
...... info - socket.io started
new user to all users
.15:55:06
.......................
× 1 of 45 tests failed:
1) Chat Server Should broadcast new user to all users:
Error: timeout of 2000ms exceeded
at Object.<anonymous> (c:\Users\jcollum\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:167:14)
at Timer.list.ontimeout (timers.js:101:19)
- watching debug - emitting heartbeat for client 9VqXYQIv39VLoLVZB-Gw
debug - websocket writing 2::
debug - set heartbeat timeout for client 9VqXYQIv39VLoLVZB-Gw
\ watching