improved fetch in gameServer
This commit is contained in:
@@ -279,7 +279,8 @@ export class GameSession {
|
||||
gc.scoreRight = 0;
|
||||
}
|
||||
}
|
||||
await fetch(c.addressBackEnd + "/game/gameserver/updategame",
|
||||
|
||||
fetch(`${c.addressBackEnd}/game/gameserver/updategame`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -290,6 +291,14 @@ export class GameSession {
|
||||
playerOneUsernameResult: gc.scoreLeft,
|
||||
playerTwoUsernameResult: gc.scoreRight,
|
||||
})
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("catch /game/gameserver/updategame: ", error);
|
||||
});
|
||||
|
||||
setTimeout(this.destroy, 15000, this);
|
||||
|
||||
@@ -89,7 +89,8 @@ async function clientAnnounceListener(this: WebSocket, data: string)
|
||||
if (announce.privateMatch) {
|
||||
body.playerTwoUsername = announce.playerTwoUsername;
|
||||
}
|
||||
const response = await fetch(c.addressBackEnd + "/game/gameserver/validate",
|
||||
|
||||
fetch(`${c.addressBackEnd}/game/gameserver/validate`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -97,17 +98,17 @@ async function clientAnnounceListener(this: WebSocket, data: string)
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
})
|
||||
.catch(error => console.log("ERROR : " + error));
|
||||
if (!response || !response.ok)
|
||||
{
|
||||
let errMessage = "validate token error";
|
||||
if (response) {
|
||||
errMessage = (await response.json()).message;
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
this.send(JSON.stringify( new ev.EventError(errMessage) ));
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("catch /game/gameserver/validate: ", error);
|
||||
this.send(JSON.stringify( new ev.EventError("validate token error") ));
|
||||
clientTerminate(clientsMap.get(this.id));
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
player.matchOptions = announce.matchOptions;
|
||||
player.token = announce.token;
|
||||
player.username = announce.username;
|
||||
@@ -232,14 +233,26 @@ function privateMatchmaking(player: ClientPlayer)
|
||||
if (player.socket.OPEN) {
|
||||
player.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
|
||||
}
|
||||
const response = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{
|
||||
|
||||
fetch(`${c.addressBackEnd}/game/gameserver/destroysession`,
|
||||
{
|
||||
method: "POST",
|
||||
headers : {"Content-Type": "application/json"},
|
||||
body : JSON.stringify({
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token : player.token
|
||||
})
|
||||
})
|
||||
.catch(error => console.log("ERROR : " + error));
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("catch /game/gameserver/destroysession: ", error);
|
||||
});
|
||||
|
||||
clientTerminate(player);
|
||||
}
|
||||
}, 60000);
|
||||
@@ -315,24 +328,29 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string)
|
||||
playerOneUsernameResult : 0,
|
||||
playerTwoUsernameResult : 0
|
||||
};
|
||||
const response = await fetch(c.addressBackEnd + "/game/gameserver/creategame",
|
||||
|
||||
fetch(`${c.addressBackEnd}/game/gameserver/creategame`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
if (!response.ok)
|
||||
{
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("catch /game/gameserver/creategame: ", error);
|
||||
gameSessionsMap.delete(gameSession.id);
|
||||
gameSession.playersMap.forEach((client) => {
|
||||
client.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
|
||||
client.gameSession = null;
|
||||
clientTerminate(client);
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
gameSession.playersMap.forEach( (client) => {
|
||||
client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchStart) ));
|
||||
@@ -445,18 +463,22 @@ export async function clientTerminate(client: Client)
|
||||
if (client.role === en.ClientRole.player)
|
||||
{
|
||||
const player = client as ClientPlayer;
|
||||
console.log("/resetuserstatus " + player.username);
|
||||
const response = await fetch(c.addressBackEnd + "/game/gameserver/resetuserstatus",
|
||||
fetch(`${c.addressBackEnd}/game/gameserver/resetuserstatus`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({username: player.username})
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("catch /game/gameserver/resetuserstatus: ", error);
|
||||
});
|
||||
if (!response.ok) {
|
||||
console.log("/resetuserstatus " + player.username + " failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ export async function fetchAvatar(username: string)
|
||||
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar?username=${username}`)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("Avatar not retrieved");
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
return response.blob();
|
||||
})
|
||||
@@ -22,7 +22,7 @@ export async function fetchUser()
|
||||
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("User not retrieved");
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
@@ -40,7 +40,7 @@ export async function fetchAllUsers()
|
||||
return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/all`)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error("All Users not retrieved");
|
||||
throw new Error("HTTP " + response.status);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user