【发布时间】:2015-02-07 04:51:10
【问题描述】:
对于我的 Node 应用程序,我有一个在 Debian 上运行的服务器 (app.js),它使用 socket.io 为我的客户端 (index.html) 提供 html 和 websocket 数据。我正在尝试制作一款基于回合制的 HTML5 多人游戏。
在使用 socket.emit()/io.emit() 和 socket.on() 执行多次成功的数据传输后,我的服务器在 socket.emit() 调用时崩溃并出现错误
“事件.js:72
投掷者; //未处理的“错误”事件
RangeError:超出最大调用堆栈大小”。
我有很多 socket.on() 事件侦听器,每个侦听器都处理游戏中的不同功能(例如 roll_dice、end_turn、ready_to_play 等)。
我试图研究这个问题(发现很多关于异步循环的讨论),但无法找到如何将解决方案应用于我自己的代码。我在这里附上了相关来源。你也可以在我的 github 上查看所有源代码: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));
});
});
请轻点,我大约一周前才开始使用 Node.js。谢谢!
【问题讨论】:
-
你能发布完整的错误信息吗?
-
编辑问题以包含它,它是:“events.js:72 throw er; //Unhandled 'error' event RangeError: Maximum call stack size exceeded”。
标签: javascript node.js socket.io