Fixing connection closing

This commit is contained in:
David Négrier 2020-10-01 17:16:49 +02:00
parent e1193ad95a
commit 1061c80f1f
2 changed files with 6 additions and 1 deletions

View File

@ -43,6 +43,7 @@ export class RoomConnection implements RoomConnection {
private userId: number|null = null; private userId: number|null = null;
private listeners: Map<string, Function[]> = new Map<string, Function[]>(); private listeners: Map<string, Function[]> = new Map<string, Function[]>();
private static websocketFactory: null|((url: string)=>any) = null; // eslint-disable-line @typescript-eslint/no-explicit-any private static websocketFactory: null|((url: string)=>any) = null; // eslint-disable-line @typescript-eslint/no-explicit-any
private closed: boolean = false;
public static setWebsocketFactory(websocketFactory: (url: string)=>any): void { // eslint-disable-line @typescript-eslint/no-explicit-any public static setWebsocketFactory(websocketFactory: (url: string)=>any): void { // eslint-disable-line @typescript-eslint/no-explicit-any
RoomConnection.websocketFactory = websocketFactory; RoomConnection.websocketFactory = websocketFactory;
@ -157,6 +158,7 @@ export class RoomConnection implements RoomConnection {
public closeConnection(): void { public closeConnection(): void {
this.socket?.close(); this.socket?.close();
this.closed = true;
} }
private resolveJoinRoom!: (value?: (RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface> | undefined)) => void; private resolveJoinRoom!: (value?: (RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface> | undefined)) => void;
@ -389,6 +391,9 @@ export class RoomConnection implements RoomConnection {
public onServerDisconnected(callback: (event: CloseEvent) => void): void { public onServerDisconnected(callback: (event: CloseEvent) => void): void {
this.socket.addEventListener('close', (event) => { this.socket.addEventListener('close', (event) => {
if (this.closed === true) {
return;
}
console.log('Socket closed with code '+event.code+". Reason: "+event.reason); console.log('Socket closed with code '+event.code+". Reason: "+event.reason);
if (event.code === 1000) { if (event.code === 1000) {
// Normal closure case // Normal closure case

View File

@ -951,7 +951,7 @@ export class GameScene extends Phaser.Scene implements CenterListener {
}); });
const nextSceneKey = this.checkToExit(); const nextSceneKey = this.checkToExit();
if(nextSceneKey){ if (nextSceneKey) {
// We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map. // We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map.
this.connection.closeConnection(); this.connection.closeConnection();
this.simplePeer.unregister(); this.simplePeer.unregister();