socket.io 客户端没有收到来自服务器的消息 [英] socket.io client not receiving messages from server

查看:36
本文介绍了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 还是很陌生,所以我的一些机制对我来说仍然有点模糊,所以我提前为我的菜鸟道歉.:)

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 调用来做初始化(绑定事件等),而 socket 指的是当前套接字.当您收到 push 消息时,这将是客户端 1,因为处理程序函数绑定到该 socketpush 事件 - 您绑定了客户端 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天全站免登陆