2

如何正确关闭 socket.io / websocket-client?-- 相关,但已过时/不起作用

我正在编写一组测试来测试服务器端套接字。从github 上的 jamescarr获得代码。Carr 的代码基于 Liam Kaufman 的此处文字墙传入

这是测试的一面。

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   
4

1 回答 1

5

五天,没有答案,所以:

这是我学到的。

  1. 签名中带有 a 的任何测试(done)都必须调用done,否则它将失败(duh)。
  2. 设置你的回调,把done调用放到最后一个。
  3. 如果有两个连接并且它们交互,则将第二个连接的行为放在第一个的连接回调中。
  4. 最后,在测试结束时启动“种子”连接。
于 2013-02-27T00:02:45.557 回答