Skip to content

Commit

Permalink
feat(unknown_secret): handle unknown_secret server err code
Browse files Browse the repository at this point in the history
  • Loading branch information
leorolland committed Jan 30, 2022
1 parent e5a7e81 commit 410ec9f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
19 changes: 13 additions & 6 deletions public/static/js/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const serverEl = document.getElementById('server')

let lastReceivedData;
/**
* Get the list of sockets URIs (as a Promise)
Expand Down Expand Up @@ -26,12 +28,12 @@ function joinGame(sockets, socketIndex=0) {
const targetPort = socketURI.split(':').at(-1)
socketURI = location.host.replace(/^[0-9]*/g, `wss://${targetPort}`)
}
console.log(`Connecting to ${socketURI} ...`)
serverEl.innerText = `Connecting to ${socketURI} ...`
// If using a Gitpod context, override the provided server address by Gitpod Ingress (and WSS)
const socket = new WebSocket(socketURI);

socket.onopen = function(e) {
console.log(`WebSocket connection established!`)
serverEl.innerText = `Websocket successfully connected to ${socketURI}`
if (secret != "" && nickname != "") {
console.log(`Found cookie, using nickname ${nickname} and secret ${secret}`)
socket.send(`${secret} get_state`)
Expand All @@ -50,12 +52,17 @@ function joinGame(sockets, socketIndex=0) {
};

socket.onmessage = function(event) {
console.log(`[message] Data received from server: ${event.data}`);

// Handle non-JSON messages (according to Specification_sockets_messages.md)
if (event.data.startsWith("error")) {
const errCode = event.data.split(" ")[1]
console.warn(`[error] server returned error code ${errCode}`)
switch (errCode) {
case 'unknown_secret':
clearCookies()
location.reload();
break;
default:
console.warn(`[error] server returned error code ${errCode}`)
}
return
} else if (event.data.startsWith("game_over")) {
let winnerName = event.data.split('game_over ')[1]
Expand Down Expand Up @@ -86,7 +93,7 @@ function joinGame(sockets, socketIndex=0) {
};

socket.onerror = function(event) {
console.warn(`Error occured with current websocket, trying to connect to the next WebSocket URI`);
serverEl.innerText = `Error occured with current websocket, trying to connect to the next WebSocket URI`
socket.close()
}
}
Expand Down
1 change: 1 addition & 0 deletions public/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<input id="nickname" type="text" placeholder="Nickname">
<button onclick="connect()">Join game</button>
<button onclick="clearCookies()">Clear cookies</button>
<p id="server"></p>
</div>
<svg id="cont" height="600" width="600" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>
<table id="scoreboard"></table>
Expand Down
1 change: 1 addition & 0 deletions src/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function manageIdentification(message: String, ws: WebSocketClient, gamew
}
} else {
log.warning("Unknown player, secret=" + raw[0]);
ws.send(`error unknown_secret`)
return undefined;
}
} else {
Expand Down

0 comments on commit 410ec9f

Please sign in to comment.