问题描述
我如何使用Node.js和socket.io建立一个游戏。
我有code如下。游戏很简单。它开始只要它连接到服务器做一些动画和它保持动画。
我要确保游戏正常失败,如果互联网连接中断或服务器断开连接。而本场比赛回来,一旦连接备份。
动画的功能从服务器的内容。因此,如果没有连接,将无法正常工作。我想只有当连接得到执行的动画的功能。
所以,我引入了一个名为连接的变量,它应该保持连接状态。如果conncted =假的,我停止动画。
但现在的问题是socket.io自行尝试重新连接了几次。如何设置连接到虚假的,在重新连接的过程?
Game.js
VAR连接= FALSE;$(文件)。就绪(函数(){//连接到socket服务器插座= io.connect('/游戏服务器');socket.on('连接',函数(){
连接= TRUE;
动画();
});socket.on('脱节',函数(){
连接= FALSE;
socket.connect();});});
功能动画(){....
......
........如果((连接)及及(.....))动画();
}
您可以使用下面的内置功能,要知道连接的状态,并相应地定义动画的状态:
socket.on('脱节',函数(){}); //等待重新连接,抱动画
socket.on(重新连接,函数(){}); //连接恢复后,重新启动动画
socket.on(重新连接,功能(nextRetry){}); //尝试重新连接,抱动画
socket.on('reconnect_failed',函数(){消息(重新连接失败);}); //动画结束
nextRetry规定之前,将尝试连接,如果在状态更新用户帮助的时间。
此外从服务器断开连接,你不必调用socket.connect()。更新的状态将可从socket.on('重新')。因此,只要有电话来这里动画。
这是仅当socket.disconnect已从客户端调用,连接将不会自动恢复,因此socket.connect()将要被再次调用。
I am using node.js and socket.io for building a game .
I have the code as below . The game is simple . It starts doing some animation as soon as it connects to the server and it keeps animating.
I want to make sure that the game fails gracefully if the internet connection goes down or if the server gets disconnected .And the game gets back once the connection is back up .
The animate function fetches content from the server . Hence it will not work properly if there is no connection . I want the animate function to get executed only when there is connection.
So I introduced a variable called connected , which is supposed to hold the status of the connection . if conncted=false , I stop the animation .
But now the problem is socket.io by itself tries to reconnect a few times . How do I set connected to false , during the reconnection process ?
Game.js
var connected = false;
$(document).ready(function(){
//connect to socket server
socket = io.connect('/gameserver');
socket.on('connect', function() {
connected = true;
animate();
});
socket.on('disconnect', function() {
connected = false;
socket.connect();
});
});
function animate()
{
....
......
........
if (( connected ) && (..... )) animate();
}
You could use the following in-built functions to know the status of the connection and accordingly define the status of animation:
socket.on('disconnect', function(){}); // wait for reconnect, hold animation
socket.on('reconnect', function(){}); // connection restored, restart animation
socket.on('reconnecting', function( nextRetry ){}); //trying to reconnect, hold animation
socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });// end animation
nextRetry specifies the time before it will try to connect, if that helps in updating the user with the status.
Moreover on a disconnect from the server, you need not call socket.connect(). The updated status will be available from socket.on('reconnect'). So just include the call to animation here.
It's only if the socket.disconnect has been called from the client, the connection will not be restored automatically and therefore socket.connect() will have to be called again.
这篇关于Socket.io:如何同时断开后重新连接执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!