我想用我的本地服务器来运行一个游戏,并使用我的PC和平板电脑来玩它,但是它总是在游戏结束之前说,有些玩家离开了,但是我没有离开网站。如果我在PC上使用2个选项卡进行尝试,它将起作用。
我试图创建一个代码,该代码可以始终更改屏幕上的某些内容,并始终向服务器以及从服务器向播放器发送一些消息,因为我认为服务器可能会踢出不活跃的人。这是.ts代码:
import {
Room
} from "colyseus";
var spielerOnline = 2;
export class monopolyKartenspiel extends Room {
maxClients = 4;
player1 = null;
player2 = null;
player3 = null;
player4 = null;
onJoin(client) {
console.log(`${client.sessionId} joined.`);
let newPlayer = {
id: client.sessionId,
client: client,
};
if (!this.player1) this.player1 = newPlayer
else if (!this.player2) this.player2 = newPlayer
else if (!this.player3) {
this.player3 = newPlayer;
spielerOnline++;
} else if (!this.player4) {
this.player4 = newPlayer;
spielerOnline++;
}
if (this.player1 && this.player2) {
console.log("Mehr als 1 Spieler");
}
}
onLeave(client) {
if (client.sessionId == undefined == false) {
console.log(`${client.sessionId} left.`);
this.broadcast(`${client.sessionId} left`);
if (client.sessionId === this.player1.id) this.player1 = null;
else if (client.sessionId === this.player2.id) this.player2 = null;
else if (client.sessionId === this.player3.id) this.player3 = null;
else this.player4 = null;
}
}
}