socket.io客户端没有从服务器接收消息 [英] socket.io client not receiving messages from server

查看:403
本文介绍了socket.io客户端没有从服务器接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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. :(

以下是所有这些代码。

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' });
    });
});

客户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>

客户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>

我无法理解客户2没有收到pop消息的原因,
一般来说我对socket.io和node.js都很陌生,所以对我来说一些技巧仍然有点模糊,所以我提前为我的noobness道歉。 :)

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. :)

欢呼

-k-

推荐答案

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

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).

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

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

这篇关于socket.io客户端没有从服务器接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆