socket.io客户端没有从服务器接收消息

时间:2022-11-18 09:52:14

I'm trying to implement a system with two clients one of them sends a message and the other one shall receive it. The figure below will explain it in a more visual way:

我正在尝试实现一个有两个客户端的系统,其中一个客户端发送一条消息,另一个客户端接收它。下图将以更直观的方式解释它:

socket.io客户端没有从服务器接收消息

So, the client 1 send the message to the server (and this works), the server receives a "push" message and emits a "pop" message that should be picked up by Client 2. The problem here is that Client 2 never receives the "pop" message. :(

因此,客户端1将消息发送到服务器(并且这有效),服务器接收“推送”消息并发出应该由客户端2接收的“弹出”消息。这里的问题是客户端2从未收到“pop”消息。 :(

Here's the code for all of them.

这是所有这些的代码。

SERVER.JS

SERVER.JS

var app = require('express').createServer()
  , io = require('socket.io').listen(app);

app.listen(999);

app.get('/webclient', function (req, res) {
    res.sendfile(__dirname + '/web.html');
});

app.get('/mobile', function (req, res) {
    res.sendfile(__dirname + '/mobile.html');
});

io.sockets.on('connection', function (socket) {
//      socket.emit('pop', { hello: 'world' });
    socket.on('push', function (data) {
        console.log('push received, emitting a pop');
        socket.emit('pop', { hello: 'world' });
    });
});

CLIENT 1 ( aka mobile.html )

客户端1(又名mobile.html)

<html>
    <head>
        <title>
            Mobile
        </title>
        <script src="/socket.io/socket.io.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js" type="text/javascript"></script>
        <script>
          var socket = io.connect('http://localhost:999');
        </script>
    </head>
    <body>
        <input type="button" name="act" id="push" value="message" />
        <script type="text/javascript" charset="utf-8">
            window.addEvent('domready', function() {
                $('push').addEvent('click', function() { 
                    socket.emit('push', { hello: 'world' });
                });
            });
        </script>
    </body>
</html>

CLIENT 2 (aka web.html)

客户端2(又名web.html)

<script src  = "/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:999');
  socket.on('pop', function (data) {
    console.log(data);
  });
</script>

I just cannot understand the reason why Client 2 does not receive the "pop" message, I'm quite new to socket.io and node.js in general so some mechanics to me are still a bit obscure, so I apologize in advance for my noobness. :)

我只是无法理解客户端2没有收到“pop”消息的原因,我对socket.io和node.js一般都很新,所以对我来说一些机制仍然有点模糊,所以我提前道歉我的noobness。 :)

cheers

干杯

-k-

-K-

2 个解决方案

#1


15  

The function passed to .on is called for each socket to do the initialization (binding events etc), and socket refers to that current socket. This will be client 1 when you receive a push message, because the handler function is bound to the push event of that socket - you bound that function when client 1 connected (where socket refers to client 1).

传递给.on的函数被调用,每个套接字执行初始化(绑定事件等),socket引用当前套接字。当您收到推送消息时,这将是客户端1,因为处理程序函数绑定到该套接字的推送事件 - 您在客户端1连接时绑定该函数(其中套接字引用客户端1)。

io.sockets refers to all sockets connected, so including client 2 in your case.

io.sockets指的是连接的所有套接字,因此在您的情况下包括客户端2。

#2


12  

Another aspect which you could take into consideration is the use of :

您可以考虑的另一个方面是使用:

socket.broadcast.emit('push', { hello: 'world' });

socket.broadcast.emit('push',{hello:'world'});

Which would essentially send the message to all the connected clients except the one that originated the message. Removing the task of filtering clients/reducing unnecessary traffic to the originating socket.

这本质上会将消息发送到除发起消息的客户端之外的所有连接客户端。删除过滤客户端的任务/减少对原始套接字的不必要流量。

Socket.IO on GitHub - Under broadcasting.

GitHub上的Socket.IO - 在广播下。

#1


15  

The function passed to .on is called for each socket to do the initialization (binding events etc), and socket refers to that current socket. This will be client 1 when you receive a push message, because the handler function is bound to the push event of that socket - you bound that function when client 1 connected (where socket refers to client 1).

传递给.on的函数被调用,每个套接字执行初始化(绑定事件等),socket引用当前套接字。当您收到推送消息时,这将是客户端1,因为处理程序函数绑定到该套接字的推送事件 - 您在客户端1连接时绑定该函数(其中套接字引用客户端1)。

io.sockets refers to all sockets connected, so including client 2 in your case.

io.sockets指的是连接的所有套接字,因此在您的情况下包括客户端2。

#2


12  

Another aspect which you could take into consideration is the use of :

您可以考虑的另一个方面是使用:

socket.broadcast.emit('push', { hello: 'world' });

socket.broadcast.emit('push',{hello:'world'});

Which would essentially send the message to all the connected clients except the one that originated the message. Removing the task of filtering clients/reducing unnecessary traffic to the originating socket.

这本质上会将消息发送到除发起消息的客户端之外的所有连接客户端。删除过滤客户端的任务/减少对原始套接字的不必要流量。

Socket.IO on GitHub - Under broadcasting.

GitHub上的Socket.IO - 在广播下。