Socket.io 未向所有连接的套接字发送消息

发布于 2024-12-08 03:28:12 字数 1314 浏览 0 评论 0原文

我正在尝试node.js 和socket.io。我不想使用删除 ping 功能,我必须从服务器获取更新。这是我正在做的事情的示例代码:

var app = require('http').createServer(),
    io  = require('socket.io').listen(app),
    cp  = require('child_process');

app.listen(8080);

//I check a global value for all the connected users from the php command line
var t = setInterval(function(){
  cp.exec('/usr/bin/php /Users/crear/Projects/MandaFree/symfony api:getRemainingMessages',
    function(err, stdout){
      if (err) {
        io.sockets.emit('error', 'An error ocurred while running child process.');
      } else {
        io.sockets.emit('change', stdout);
      }
      console.log('Remaining messages: ' + stdout);
    });
  }, 3000);

var remaining =  io.of('/getRemainingMessages')
  .on('connection', function(socket){
    socket.on('disconnect', function(){});
  });

这里的问题是,当我调用 io.sockets.emit() 时,调试控制台告诉我它正在做某事,但看起来它没有到达客户端。因为他们什么也没做。

我曾经为每个连接的客户端设置一个间隔,当我使用 socket.emit() 时,它确实起作用了。但这不是最佳解决方案。

更新: 这是我的客户端代码。

var remaining = io.connect('http://127.0.0.1:8080/getRemainingMessages');
remaining.on('change', function(data){
  console.log('Remaining messages: ' + data );
  $('#count').html(data);
});
remaining.on('error', function(error){
  console.log(error);
});

I'm trying out node.js and socket.io. I wan't to use to remove a ping function I have to get updates from my server. Here is an example code of what I'm doing:

var app = require('http').createServer(),
    io  = require('socket.io').listen(app),
    cp  = require('child_process');

app.listen(8080);

//I check a global value for all the connected users from the php command line
var t = setInterval(function(){
  cp.exec('/usr/bin/php /Users/crear/Projects/MandaFree/symfony api:getRemainingMessages',
    function(err, stdout){
      if (err) {
        io.sockets.emit('error', 'An error ocurred while running child process.');
      } else {
        io.sockets.emit('change', stdout);
      }
      console.log('Remaining messages: ' + stdout);
    });
  }, 3000);

var remaining =  io.of('/getRemainingMessages')
  .on('connection', function(socket){
    socket.on('disconnect', function(){});
  });

The Issue here, is that when I call io.sockets.emit() the debug console tells me it is doing something, but it looks like it is not getting to the clients. Because they are doing nothing.

I use to have one interval for every connected client, and when I used socket.emit() it did worked. But it is not the optimal solution.

UPDATE:
Here is my client side code.

var remaining = io.connect('http://127.0.0.1:8080/getRemainingMessages');
remaining.on('change', function(data){
  console.log('Remaining messages: ' + data );
  $('#count').html(data);
});
remaining.on('error', function(error){
  console.log(error);
});

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

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

发布评论

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

评论(1

苦笑流年记忆 2024-12-15 03:28:12

几天前遇到了一个非常相似的问题,看起来 socket.io 对 API 做了一些更改。我从未使用过 symfony,希望问题是一样的。

我有一个 socket.io 发送和接收消息的工作演示 - 上传到 https://github。 com/parj/node-websocket-demo 作为参考

本质上有两个更改

  1. 在服务器端 - 将 socket.on 更改为 socket.sockets.on

    var socket = io.listen(server);
    socket.sockets.on('连接', 函数(客户端)
    
  2. 在客户端 - 不需要 URL 和端口是自动检测到的。

    var socket = io.connect();
    

这已经使用 Express 2.5.2 和 Socket.io 0.8.7 进行了测试,

我已将您的服务器代码与我的服务器代码合并,您可以在服务器和我的 客户端 JavaScript客户端 html 只是为了看看它是否正常工作?

var socket = io.listen(server);

socket.sockets.on('connection', function(client){
  var connected = true;

  client.on('message', function(m){
    sys.log('Message received: '+m);
  });

  client.on('disconnect', function(){
    connected = false;
  });

  var t = setInterval(function(){
  if (!connected) {
      return;
  }

  cp.exec('/usr/bin/php /Users/crear/Projects/MandaFree/symfony api:getRemainingMessages',
    function(err, stdout){
      if (err) {
        client.send('error : An error ocurred while running child process.');
      } else {
        client.send('change : ' + stdout);
      }
      console.log('Remaining messages: ' + stdout);
    });
  }, 3000);

  t();

});

Had a very similar issue couple of days back and looks like socket.io had some changes in the API. I have never worked with symfony and am hoping the issues are the same.

I have a working demo of socket.io sending and receiving a message - uploaded to https://github.com/parj/node-websocket-demo as a reference

Essentially two changes

  1. On Server side - changed socket.on to socket.sockets.on

    var socket = io.listen(server);
    socket.sockets.on('connection', function(client)
    
  2. On Client side - URL and port not required as it is autodetected.

    var socket = io.connect();
    

This has been tested using Express 2.5.2 and Socket.io 0.8.7

I have amalgamated your server code with mine, would you be able to try this on the server and my client javascript and client html just to see if it is working?

var socket = io.listen(server);

socket.sockets.on('connection', function(client){
  var connected = true;

  client.on('message', function(m){
    sys.log('Message received: '+m);
  });

  client.on('disconnect', function(){
    connected = false;
  });

  var t = setInterval(function(){
  if (!connected) {
      return;
  }

  cp.exec('/usr/bin/php /Users/crear/Projects/MandaFree/symfony api:getRemainingMessages',
    function(err, stdout){
      if (err) {
        client.send('error : An error ocurred while running child process.');
      } else {
        client.send('change : ' + stdout);
      }
      console.log('Remaining messages: ' + stdout);
    });
  }, 3000);

  t();

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