Bob Ren
Bob Ren

Reputation: 2179

socket.io client not receiving packets

Client Side:

App.socket = io.connect('http://127.0.0.1:4000');
App.socket.on('draw', function(data) {
  console.log("drawing");   
  return App.draw(data.x, data.y, data.type);
});
...
$(document).keyup(function(e) {
  if (e.keyCode == 27) { 
    App.socket.emit('drawing');
  }   
});

Server Side:

(function() {
  var io;
  io = require('socket.io').listen(4000);
  io.sockets.on('connection', function(socket) {
    socket.on('drawing', function() {  
      socket.broadcast.emit('draw', {
      x: 1,
      y: 1,
      type: "test"
      });  
    });
  });
}).call(this);

So what the code does is detect for the "esc" key to be pressed. When the esc key is pressed, the client emits a packet called drawing to the server. This works, and the server does receive the packet. Furthemore, the server does broadcast a packet called "draw" after receiving the "drawing" packet, but for some reason on the client side, the draw packet is not received after the server broadcasts it.

I've been staring at this code for hours, it seems so simple, I'm hoping a fresh eye can catch the simple mistake.

Thanks

Upvotes: 1

Views: 816

Answers (1)

Bob Ren
Bob Ren

Reputation: 2179

Figured it out, socket.emit.broadcast sends packets to every socket connection besides the current socket

Upvotes: 1

Related Questions