Send less Server Data with "AFK"

Posted by Oliver Schöning on Game Development See other posts from Game Development or by Oliver Schöning
Published on 2012-10-26T15:47:22Z Indexed on 2012/10/26 17:21 UTC
Read the original article Hit count: 269

Filed under:

I am working on a 2D (Realtime) MultiPlayer Game.

With Construct2 and a Socket.IO JavaScript Server.

Right now the code does not include the Array for each Player.

var io = require("socket.io").listen(80); 
var x = 10; 
io.sockets.on("connection", function (socket) 
{ 
socket.on("message", function(data) 
{ 
x = x+1; 
}); 
}); 
setInterval(function() 
{ 
io.sockets.emit("message", 'Pos,' + x); 
},100);

I noticed a very annoying problem with my server today. It sends my X Coordinates every 100 milliseconds.

The Problem was, that when I went into another Browser Tab, the Browser stopped the Game from running. And when I went back, I think the Game had to run through all the packages. Because my Offline Debugging Button still worked immediately and the Online Button only responded after some seconds.

enter image description here

So then I changed my Code so that it would only send out an update when it received a player Input:

var io = require("socket.io").listen(80);

var x = 10;

io.sockets.on("connection", function (socket) {
socket.on("message", function(data)
{

x = x+1;
io.sockets.emit("message", 'Pos,' + x);
});
});

And it Updated Immediately, even when I had been inactive on the Browser Tab for a long time. Confirming my suspicion that it had to get through all the data. Confirm Please!

It would be insane to only send information on Client Input in a Real Time Game.

But how would I write a AFK function?

I would think it is easier to run a AFK Boolean Loop on the Server.

Here is what I need help for:

playerArray[Me]
if (   "Not Given any Input for X amount of Seconds"    )
{  
 "Don't send Data"
}
else 
{  
 "Send Data"
}

© Game Development or respective owner

Related posts about JavaScript