Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Socket.io Maximum call stack size exceeded

Tags:

For my Node application, I have a server (app.js) running on Debian serving both html and websocket data using socket.io to my client (index.html). I am attempting to make a turn-based HTML5 multiplayer game.

After performing a number of successful data transmissions using socket.emit()/io.emit() and socket.on(), my server crashes on an socket.emit() call with the error
"events.js:72
throw er; //Unhandled 'error' event
RangeError: Maximum call stack size exceeded".
I have quite a few socket.on() event listeners, with each one handling a different function in the game (e.g. roll_dice, end_turn, ready_to_play, etc.).

I attempted researching the problem (found a lot of discussion on async loops), but was unable to find how to apply the solutions to my own code. I attached relevant source here. You can also view all the source on my github at: https://github.com/sjmoon0/gameofdeath

index.html

var socket = io.connect('http://131.178.15.173',{'forceNew':true});
 
                    ...

  //----------------Initialization and Menu functions-----------
  socket.on('load', function (data) {
    console.log(data);
    clientID=data;
    socket.emit('check_game_started', { un: clientID });
    socket.on('last_client_loaded', function(hasStarted){
    	console.log("Has game started? :"+hasStarted);
    	if(hasStarted==true){
    		$('#choosecharacter').show();
    	}
    });
  });

  socket.on('client_disconnect', function (data) {
    console.log(data);
  });

  socket.on('client_counter', function (data) {
    if(data<5){
	    console.log(data);
	    incrementLoadBar(data);	
    	allowedInGame=true;
    }
    if(!allowedInGame){
    	...
    }
  });

  socket.on('game_started', function (data) {
    console.log(data);
    $('#welcome').hide();
    $('#choosecharacter').show();
  });

  socket.on('set_user', function(characterName){
  	chosenCharacter=characterName;
  });

  socket.on('disable_player_choice', function(data){
  	var id=data.stuff[0].chara;
  	incrementLoadBar(data.stuff[0].numChar);
  	console.log(id +" was chosen");
  	$('#'+id).hide();
  });


//-------------------Gameplay functions
  socket.on('start_gameplay',function(nonsense){
  	showChanges(nonsense);
	$('#wait').hide();
	$('#gamespace').show();
	draw_c();
	socket.emit('ready_to_play',chosenCharacter);
  });

  socket.on('take_turn',function(updatedBoard){
  	showChanges(updatedBoard);
  	if(updatedBoard.currPlayer==chosenCharacter){
  		promptUser(updatedBoard);
  	}
  });

  socket.on('roll_result',function(rollResult){
  	promptUser(rollResult);
  });

                  ...
                  

	$('#rollDiceButton').click(function(){
		socket.emit('roll_dice',chosenCharacter);
	});

	$('#okCloseButton').click(function(){
		socket.emit('end_turn',chosenCharacter);
	});

	$('.thumbnail').click(function(something){
		socket.emit('player_chosen', something.target.id);
		            ...
	});

app.js

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var url = require('url');

...

app.listen(8001);

function handler (req, res) {
...
}
console.log("~Server Running~");


io.on('connection', function (socket) {
  console.log("A Client connected");
  ...
  socket.emit('load', { user: uID });
  io.emit('client_counter',numClients);

  if(numClients==4){
      gameStarted=true;
      console.log("Game started!");
      io.emit('game_started',"The Game has begun!");
    }
    else if(numClients>4){
      numClients--;
      delete allClients[allClients.indexOf(socket)];
    }

  socket.on('check_game_started', function (data) {
    socket.emit('last_client_loaded', gameStarted);
    console.log(data);
    if(gameStarted){
      console.log("Last Player Loaded!");
    }
  });

  socket.on('player_chosen', function(cp){
    ...
    socket.emit('set_user', cp);
    ...
    io.emit('disable_player_choice',{'stuff':[{'chara':cp,'numChar':numCharChosen}]});
    if(numCharChosen==4){
      io.emit('start_gameplay', boardUpdate);
    }
  });

  socket.on('disconnect',function(){
    console.log("A client disconnected");
    numClients--;
    delete allClients[allClients.indexOf(socket)];
    io.emit('client_disconnect',"We've lost another comrade!");
  });

  socket.on('ready_to_play',function(characterThatIsReadyToPlay){
    io.emit('take_turn',boardUpdate);
  });

  socket.on('roll_dice', function(characterThatRolledDice){
    var temp=generateRollResult(characterThatRolledDice)
    socket.emit('roll_result',temp);
  });

  socket.on('end_turn',function(characterThatEndedTurn){
    io.emit('take_turn',nextUpdate(characterThatEndedTurn));
  });
});

Please be gentle, I just started using Node.js about a week ago. Thanks!

like image 268
noden00b Avatar asked Dec 09 '14 10:12

noden00b


1 Answers

Found my issue.

The object (temp) I was trying to send over the network (in socket.emit('roll_result',temp);) was a self-referencing array. It was the recursive property of the array that caused the stack to exceed the max size.

like image 196
noden00b Avatar answered Oct 02 '22 05:10

noden00b