Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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"
}
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You could check for the blur event in the window, when that happen you send a message to the server saying "this player is afk", the server won't send messages to afk players, when your player comes back you send another message saying "not afk anymore" and start sending messages again.

It should be something like this:

window.onblur = function() {
    server.send(PLAYER_AFK, connection);
}

window.onfocus = function() {
    server.send(PLAYER_NOT_AFK, connection);
}

onblur is called whenever an object loses focus, in this case when the user changes the tab (or the browser stop being active.) onfocus is called when the window receives the focus again.

share|improve this answer
    
Interesting. I though the entire JavaScript gets "Frozen" and can't send any message to the server. I will read up on this Mysterious "blur" :D –  Oliver Schöning Oct 26 '12 at 18:11
    
I will not close the Question just yet for several Reasons. A. Construct2 (the Clientside) most likely needs me to write a plugin to implement blur. B. I am developing for Several Platforms, and currently I am not sure if Blur works on all of them. –  Oliver Schöning Oct 26 '12 at 18:19
    
I added a little explanation to see if I could help with the "mysterious" part of the blur. –  Luke B. Oct 26 '12 at 18:20
    
Thank you for all the help :) –  Oliver Schöning Oct 26 '12 at 18:24
    
You should tag your question with "Construct2" if you're looking for a specific answer. –  Luke B. Oct 26 '12 at 18:33
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.