socket.io - 第一次工作,第二次以后不再工作

发布于 2024-12-05 20:36:30 字数 1254 浏览 1 评论 0原文

当我启动 Node.js 服务器并且客户端连接时,我可以从客户端 (socket.emit) 发送请求并获得响应 (socket.on('rentsAround'....))。但是当我第二次连接时,客户端能够发送,但服务器无法发送或发出。所以我必须再次重新启动服务器。我知道它正在按预期工作,但不知何故我的理解在某些地方是错误的......请有人指出。

客户端: ========

    var socket = new io.Socket();
    socket = io.connect();

    socket.on('rentsAround', function(data){
        registration.handleRentsAround(data);
    });


    socket.on('locationDetailsRes', function(data){
        registration.handleRentsAround(data);
    });

    socket.on('connect', function(data){
        alert('inside connect on client side');
    });

socket.on('disconnect', function(){ 
    // do something, if you want to. 
    });
    .............
    socket.emit("searchRent",  {"lat":lat, "lng":lng});

服务器端: ========

socket.sockets.on('connection', function(client){ 

            client.on('searchRent', function(msg){
        console.log('inside on connection');
                // do something and reply back
        client.emit('rentsAround',{"totalRents":docs.length, "rents":docs});
            });

   client.on('disconnect', function(){ 
        sys.puts("client disconnect"); 
        mongoose.disconnect();
        }); 

When I start my node.js server and client gets connected, I am able to send a request from client (socket.emit) and get a response (socket.on('rentsAround'....)). But when I connect 2nd time onwards, the client is able to send, but server can not send or emit. So I have to restart the server again. I understand that it is working as expected, but somehow my understanding is wrong somewhere... Would someone please point out.

client side:
========

    var socket = new io.Socket();
    socket = io.connect();

    socket.on('rentsAround', function(data){
        registration.handleRentsAround(data);
    });


    socket.on('locationDetailsRes', function(data){
        registration.handleRentsAround(data);
    });

    socket.on('connect', function(data){
        alert('inside connect on client side');
    });

socket.on('disconnect', function(){ 
    // do something, if you want to. 
    });
    .............
    socket.emit("searchRent",  {"lat":lat, "lng":lng});

server side:
========

socket.sockets.on('connection', function(client){ 

            client.on('searchRent', function(msg){
        console.log('inside on connection');
                // do something and reply back
        client.emit('rentsAround',{"totalRents":docs.length, "rents":docs});
            });

   client.on('disconnect', function(){ 
        sys.puts("client disconnect"); 
        mongoose.disconnect();
        }); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-12-12 20:36:30

Socket.io 0.7 及以上版本将尝试重用与同一主机的连接。根据我的经验,我发现这种行为可能有点不稳定。

我无法从您提供的小代码示例中看出,但我怀疑问题是对 connect() 的第二次调用试图重用第一个(已关闭)联系。

解决方法是在调用 connect() 时传递'force new connection' 选项。例如:

<代码>
io.connect("http://localhost", {'强制新连接': true});

Socket.io 0.7 onwards will try and reuse connections to the same host. In my experience I've found this behaviour can be a little flakey.

I can't tell from the small code sample you provided, but I suspect the problem is that the second call to connect() is trying to reuse the first (closed) connection.

The workaround is to pass the 'force new connection' option when you call connect(). Eg:


io.connect("http://localhost", {'force new connection': true});

遗弃M 2024-12-12 20:36:30

第二行丢弃在第一行中创建的对象。只需这样做应该可行:

var socket = io.connect();

第一次发送和第二次失败的问题可能是由于浏览器/协议造成的。我在 Internet Explorer 和 XHR 传输以及使用 JSONP 的 Opera 中看到过这种行为。

如果您使用 IE,请尝试切换到 JSONP,它应该可以正常工作。这可以在服务器端通过向配置提供传输列表来完成。只需确保 JSONP 位于 XHR 之前即可。像这样的东西:

sio.set('transports', [
    'websocket'
    , 'flashsocket'
    , 'jsonp-polling'
    , 'xhr-polling'
    , 'htmlfile'
]);

Your second line discards the object created in the first line. Simply doing this should work:

var socket = io.connect();

The problem with first send and second fail could be due to browser/protocol. I have seen such behaviour with Internet Explorer and XHR transport, and also with Opera using JSONP.

If you are using IE, try switching to JSONP and it should work properly. This can be done on the server side, by supplying the transport list to configuration. Just make sure JSONP comes before XHR. Something like this:

sio.set('transports', [
    'websocket'
    , 'flashsocket'
    , 'jsonp-polling'
    , 'xhr-polling'
    , 'htmlfile'
]);
愁以何悠 2024-12-12 20:36:30

从 socket.io 1.0 开始,有两个设置控制此行为:

  • “强制新连接”:true,在客户端 connect() 调用上。

  • "cookie":false,在服务器 Server() 构造函数上。

显然,两者产生完全相同的行为。

截至目前,第二种方法尚未记录在案。但是,查看源代码,您可以看到传递给 socket.io Server() 的所有选项都传递给内部 Engine.io 库 Server() 构造函数,这使您可以更改其中的任何选项。这些选项记录在此处:

https://github.com/LearnBoost/engine.io

As of socket.io 1.0, two settings control this behaviour:

  • "force new connection":true, on the client connect() call.

  • "cookie":false, on the server Server() constructor.

Apparently, both produce the exact same behaviour.

The second method is, as of today, undocumented. However, looking at the source code you can see that all options passed to socket.io Server() are passed to the internal Engine.io library Server() constructor, which lets you change any of the options there. These options are documented here:

https://github.com/LearnBoost/engine.io

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文