【问题标题】:how do I close a server in socket.io 1.0?如何在 socket.io 1.0 中关闭服务器?
【发布时间】:2014-07-20 12:32:11
【问题描述】:

有这样的骗局,但它们是针对库的先前版本的。好像代码变了。

我正在像这样实例化一个套接字:

this.socketLib || (this.socketLib = require('socket.io'));
this.socket = this.socketLib.listen(this.port);

在对象的其他地方,我有一个关闭函数。我尝试了以下方法来关闭套接字:

this.socket.close() # TypeError: Object #<Server> has no method 'close'
this.socket.server.close() #  TypeError: Object #<Server> has no method 'close'
this.socket.server.disconnect() #  TypeError: Cannot call method 'disconnect' of undefined
this.socket.disconnect() # TypeError: Object #<Server> has no method 'disconnect'
this.socket.io.engine.close() # TypeError: Cannot read property 'engine' of undefined

代码库中出现了disconnect,但它们似乎是针对客户端的:(link)。 close 基本上也是一样的。或者我没看到。

我已经调试并在对象中四处寻找,但没有发现任何关闭服务器的东西。

如何关闭新代码库中的套接字服务器?我需要关闭它以进行集成测试(第一次测试运行通过,第二次运行得到EADDRINUSE 错误)。我正在尝试编写一个可以在 mocha 中的 after 函数中运行的关闭函数。如果我在没有--watch 的情况下运行它们,我的测试就会通过。只是多次连续运行失败。

【问题讨论】:

  • 看起来 1.0 仍然缺少以前版本的许多功能。
  • @dystroy 哎哟,不知道为什么?认为有更好的选择吗?
  • 我不知道。现在我在等着看它在接下来的几天或几周内会如何变化。我冻结了我的应用程序中的迁移,直到它更清楚为止。
  • 我刚刚降级到 0.9。我会为此提交一个问题。
  • 作为问题提交(这应该由文档回答):github.com/Automattic/socket.io/issues/1559

标签: node.js websocket socket.io socket.io-1.0


【解决方案1】:

希望对你有帮助:-)

socket.conn.close ();

如果你这样做,你会得到无限数量的"a user connected"

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){
    console.log('a user connected');

    socket.conn.close ();

        ....
});

【讨论】:

  • 我以后看看。目前,我已经降级到 Socket IO 0.9。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2011-11-21
  • 2014-12-15
  • 2013-05-03
  • 1970-01-01
  • 2013-03-22
相关资源
最近更新 更多