I am using NodeJS + Socket.IO for a simple web game. It is working. Why?
Here is my server.js
var app = require('express').createServer(); var io = require('socket.io').listen(app); io.sockets.on('connection', function (socket) { socket.on('set nickname' , function (nickname) { socket.nickname = nickname; console.log(nickname + ' just connected!'); }); socket.on('msg' , function (msg) { socket.msg = msg; io.sockets.emit('response', msg); }); socket.on('updatePlayer' , function (updatePlayer) { console.log("Someone just moved on the map!"); }); }); app.listen(8080);
The reason I know works because when I load the page and this script executes:
// SERVER STUFF socket.on('connect', function () { console.log('We are connected!'); name = $(".name").val(); this.emit('set nickname', name, function (success) {}); msg = name + "|" + currX + "|" + currY + "|" + currentMap; this.emit('msg', msg, function (success) {}); socket.on('response', function (data) { var theMsg = data.split("|"); sayDialog("New player! " + theMsg[0] + " connected on map " + theMsg[3] + " on coordinates (" + theMsg[1] + "," + theMsg[2] + ")"); }); });
I get in my dialog box ("CSS div") something like New player! weka connected on map 4 on coordinates (10,5)
New player! weka connected on map 4 on coordinates (10,5)
OK, cool, that's how it works! However, when I "move", I try to send a message to the server using this
socket.on('updatePlayer', function () { console.log("Testing..."); });
I donβt even see console
say Testing. I do not know why.
source share