我将尝试在多人游戏中复制一个简单的游戏(最多两个玩家)。我使用JavaScript、Node.js、Socket.io和Express来完成这个任务。一切都很好,但是当我在服务器和客户端激活主循环后,游戏开始非常流畅,但在2或3秒后,延迟和冻结呈指数增长。我知道这是由于我在无限循环中调用的事件监听器的无限性导致的......我该如何避免这种情况?
以下是服务器的循环:
以下是服务器的循环:
function updateGameArea(){
frameNo+=1;
if (players.length < 2){
if (players[0]!=socket.id){
console.log('Logged',socket.id);
players.push(socket.id);
}
}
for (i=0;i<players.length;i++){
if (socket.id==players[i]){
var c=i;
}
}
if (!id1 || !id0){
socket.emit('id',{'id':c});
}
socket.on('info',(data)=>{
if (data.c==0){
myGamePiece=data.x;
info0=true;
}
else {
yourGamePiece=data.x;
info1=true;
}
})
socket.on('id0',()=>{
id0=true;
})
socket.on('id1',()=>{
id1=true;
})
if (id0 & id1 & info0 & info1){
socket.emit('go');
socket.emit('frame',{'f':frameNo});
socket.on('newpos',(data)=>{
console.log('Wroking');
if (data.c==0){
myGamePiece.newPos();
socket.emit('info0',{'o':myGamePiece.gravitySpeed,'x':myGamePiece.x,'y':myGamePiece.y,'sy':myGamePiece.speedY});
}
else if(data.c==1){
yourGamePiece.newPos();
socket.emit('info1',{'o':yourGamePiece.gravitySpeed,'x':yourGamePiece.x,'y':yourGamePiece.y,'sy':yourGamePiece.speedY});
}
})
socket.on('numob',(data)=>{
myObstacles[myObstacles.length-1]=data.i;
myObstacles[myObstacles.length-1]=data.l;
})
if (frameNo ==1 || everyinterval(150)){
socket.emit('createob');
}
for (i=0;i<myObstacles.length;i+=1){
myObstacles[i].x += -1 ;
socket.emit('agobj',{'i':i});
}
for (i=0;i<myObstacles.length; i+=1){
if (myGamePiece.crashWith(myObstacles[i])){
socket.emit('dead0');
}
else if(yourGamePiece.crashWith(myObstacles[i])){
socket.emit('dead1');
}
}
socket.on('push',(data)=>{
var l=speed(data.id);
if (l=0){
var id=0;
y=-3.5
socket.emit('pushinf',{'y':y,'id':id})
}
else{
var id=1;
y=-3.5;
socket.emit('pushinf',{'y':y,'id':id})
}
})
socket.on('disconnect',() => {
console.log('id attivo',socket.id);
console.log('disconnected!',socket.id);
for (i=0;i<players.length;i++){
if (players[i]==socket.id){
players.splice(i,1);
}
}
});
}
以下是客户端的生命周期:
function updateGameArea(){
socket.on('connect', function() {
console.log("connected from the client side");
});
socket.on('id',function(data){
console.log('server sent info id to me')
console.log(data.id)
var id=data.id;
console.log(id);
if (id==0){
socket.emit('id0');
}
else {
socket.emit('id1');
}
socket.emit('info',{'x':myGamePiece,'c':id});
});
socket.on('go',function(){
go=true;
})
if (go){
socket.on('frame',function(data){
frameNo=data.f;
})
socket.on('createob',function(){
x=myGameArea.canvas.width;
minHeight=60;
maxHeight=140;
height=Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap=60;
maxGap=90;
var gap=Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10,height,"green",x,0));
myObstacles.push(new component(10,x-height-gap,"green",x,height + gap));
socket.emit('numob',{'l':myObstacles[myObstacles.length],'i':myObstacles[myObstacles.length-1]})
});
socket.emit('newpos',{'c':id});
socket.on('info0',function(data){
if (id==0){
myGamePiece.gravitySpeed=data.o
myGamePiece.x=data.x;
myGamePiece.y=data.y;
myGamePiece.speedY=data.sy;
}
else{
yourGamePiece.gravitySpeed=data.o;
yourGamePiece.x=data.x;
yourGamePiece.y=data.y;
yourGamePiece.speedY=data.sy;
}
})
socket.on('info1',function(data){
if (id==1){
myGamePiece.gravitySpeed=data.o;
myGamePiece.x=data.x;
myGamePiece.y=data.y;
myGamePiece.speedY=data.sy;
}
else{
yourGamePiece.gravitySpeed=data.o;
yourGamePiece.x=data.x;
yourGamePiece.y=data.y;
yourGamePiece.speedY=data.sy;
}
})
socket.on('dead0',function(){
if (id==0){
myGameArea.stop();
mySound.play();
GameO=true;
}
else{
mySound.play();
yourGamePiece.gravitySpeed==3.5;
}
})
socket.on('dead1',function(){
if (id==1){
myGameArea.stop();
mySound.play();
GameO=true;
}
else{
mySound.play();
yourGamePiece.gravitySpeed==3.5;
}
})
socket.on('agobj',function(data){
myObstacles[data.i].x+=1;
})
myGameArea.canvas.addEventListener('click',function(){
socket.emit('push',{'id':id});
if (mSoundw.sound.pausedy) {
mySoundw.sound.play();
}
else{
mySoundw.sound.currentTime = 0
}
socket.on('pushinf',function(data){
if (id==data.c){
myGamePiece.gravitySpeed=y;
myGamePiece.image.src="angry.png";
}
else{
yourGamePiece.gravitySpeed=y;
yourGamePiece.image.src="angryb.png";
}
})
},false)
}
}