开发者

Socket.io not sending a message to all connected sockets

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 );开发者_StackOverflow社区
  $('#count').html(data);
});
remaining.on('error', function(error){
  console.log(error);
});


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();

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜