Reputation: 2179
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
Reputation: 2179
Figured it out, socket.emit.broadcast sends packets to every socket connection besides the current socket
Upvotes: 1