I am making a game with NODE JS and socket iO
In my main file (index.js) when a person connects and logs in I add them into my player pool as an object. Within the player object I store their socket so when they are in the game I can emit events to them real time
var index ={};
index.playerPool = [];
adding player
socket.on("user_information",function(info){
//when the user sends me their user information lets create a new player object.
checkIfPlayerExistsInMap(info.user_id);
var player = new playa.playa();
player.name = info.name;
player.picture = info.picture;
player.email = info.email;
player.user_id = info.user_id;
player.socket = socket;
adding player to game
var ngame = new game.game();
var id = getNewGameId();
p.current_game_id = id;
ngame.created = id;
index.currentGames[String(id)] = ngame;
var ng = index.currentGames[String(id)];
ng.addPlayer(p);
This works fine through out my game (for about 4 min). All of a sudden, when I emit a certain event, my reference to the player.socket will no longer work.
I tried to log the object I get the following
connected: false,
disconnected: true,
Is there a reason that my socket IO client is getting disconnected? I keep sending my clients data randomly (IE I have a lobby countdown timer and a notification when the game starts which works great), how ever when a player looses I can no longer emit an event to any of my clients and I find this very strange?
versions:
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "~4.8.7",
"socket.io": "~1.0.6"
}
}
I originally thought that it had to do with the fact that I had an interval function running on my clients... but when I took them out the problem still persisted
I also tried a call back function to the socket who emitted the "player lost event"... Then and only then did my socket emit an event. I also tried an io. emit just to make sure that my clients were connected at all.. which worked.
So I am figuring that the reference doesn't get held in my object correctly?
Has anyone had any experience with this?
this is how I try to emit my event in the game class
var p = index.index.playerPool[String(my_id)];
p.socket.emit("player_left",player_id_leaving);
which again works for the lobby count down and notifying players that other players have left etc before the game starts.