Compare commits
1 commit
develop
...
refactorco
Author | SHA1 | Date | |
---|---|---|---|
30a4d504e4 |
|
@ -23,6 +23,7 @@ enum EventMessage{
|
||||||
GROUP_DELETE = "group-delete",
|
GROUP_DELETE = "group-delete",
|
||||||
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
||||||
|
|
||||||
|
CONNECT = "connect",
|
||||||
CONNECT_ERROR = "connect_error",
|
CONNECT_ERROR = "connect_error",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,13 +110,17 @@ export class Connection implements Connection {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createConnection(name: string, characterSelected: string): Promise<Connection> {
|
public static async createConnection(name: string, characterSelected: string): Promise<Connection> {
|
||||||
return Axios.post(`${API_URL}/login`, {name: name})
|
try {
|
||||||
.then((res) => {
|
const res = await Axios.post(`${API_URL}/login`, {name: name})
|
||||||
|
|
||||||
return new Promise<Connection>((resolve, reject) => {
|
return new Promise<Connection>((resolve, reject) => {
|
||||||
const connection = new Connection(res.data.token);
|
const connection = new Connection(res.data.token);
|
||||||
|
|
||||||
|
connection.onConnect(() => {
|
||||||
|
resolve(connection);
|
||||||
|
});
|
||||||
|
|
||||||
connection.onConnectError((error: object) => {
|
connection.onConnectError((error: object) => {
|
||||||
console.log('An error occurred while connecting to socket server. Retrying');
|
console.log('An error occurred while connecting to socket server. Retrying');
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -127,11 +132,8 @@ export class Connection implements Connection {
|
||||||
} as SetPlayerDetailsMessage, (id: string) => {
|
} as SetPlayerDetailsMessage, (id: string) => {
|
||||||
connection.userId = id;
|
connection.userId = id;
|
||||||
});
|
});
|
||||||
|
|
||||||
resolve(connection);
|
|
||||||
});
|
});
|
||||||
})
|
} catch (err) {
|
||||||
.catch((err) => {
|
|
||||||
// Let's retry in 4-6 seconds
|
// Let's retry in 4-6 seconds
|
||||||
return new Promise<Connection>((resolve, reject) => {
|
return new Promise<Connection>((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -139,7 +141,7 @@ export class Connection implements Connection {
|
||||||
.catch((error) => reject(error));
|
.catch((error) => reject(error));
|
||||||
}, 4000 + Math.floor(Math.random() * 2000) );
|
}, 4000 + Math.floor(Math.random() * 2000) );
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public closeConnection(): void {
|
public closeConnection(): void {
|
||||||
|
@ -184,6 +186,10 @@ export class Connection implements Connection {
|
||||||
this.socket.on(EventMessage.GROUP_DELETE, callback)
|
this.socket.on(EventMessage.GROUP_DELETE, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public onConnect(callback: () => void): void {
|
||||||
|
this.socket.on(EventMessage.CONNECT, callback)
|
||||||
|
}
|
||||||
|
|
||||||
public onConnectError(callback: (error: object) => void): void {
|
public onConnectError(callback: (error: object) => void): void {
|
||||||
this.socket.on(EventMessage.CONNECT_ERROR, callback)
|
this.socket.on(EventMessage.CONNECT_ERROR, callback)
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,6 +213,10 @@ export class GameScene extends Phaser.Scene {
|
||||||
this.scene.sleep(ReconnectingSceneName);
|
this.scene.sleep(ReconnectingSceneName);
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
|
}).catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
// TODO: go to error screen / reconnect
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue