mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
Merge pull request #172 from AlecM33/connection-corrections
correct connection handling to handle when socket is already connecte…
This commit is contained in:
@@ -20,6 +20,17 @@ import { Ended } from '../game_state/states/Ended.js';
|
||||
export const gameHandler = (socket, window, gameDOM) => {
|
||||
document.body.innerHTML = gameDOM + document.body.innerHTML;
|
||||
injectNavbar();
|
||||
const connectionHandler = () => {
|
||||
if (stateBucket.timerWorker) {
|
||||
stateBucket.timerWorker.terminate();
|
||||
stateBucket.timerWorker = null;
|
||||
}
|
||||
syncWithGame(
|
||||
socket,
|
||||
UserUtility.validateAnonUserSignature(stateBucket.environment),
|
||||
window
|
||||
);
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
window.fetch(
|
||||
'/api/games/environment',
|
||||
@@ -30,22 +41,17 @@ export const gameHandler = (socket, window, gameDOM) => {
|
||||
).catch(() => {
|
||||
reject(new Error('There was a problem connecting to the room.'));
|
||||
}).then((response) => {
|
||||
if (!response.ok) {
|
||||
reject(new Error('HTTP ' + response.status + ': Could not connect to the room'));
|
||||
if (!response.ok && !(response.status === 304)) {
|
||||
reject(new Error('Could not connect to the room: HTTP ' + response.status + ': ' + response.statusText));
|
||||
return;
|
||||
}
|
||||
response.text().then((text) => {
|
||||
stateBucket.environment = text;
|
||||
if (socket.connected) {
|
||||
connectionHandler();
|
||||
}
|
||||
socket.on('connect', () => {
|
||||
if (stateBucket.timerWorker) {
|
||||
stateBucket.timerWorker.terminate();
|
||||
stateBucket.timerWorker = null;
|
||||
}
|
||||
syncWithGame(
|
||||
socket,
|
||||
UserUtility.validateAnonUserSignature(stateBucket.environment),
|
||||
window
|
||||
);
|
||||
connectionHandler();
|
||||
});
|
||||
socket.on('connect_error', (err) => {
|
||||
toast(err, 'error', true, false);
|
||||
|
||||
@@ -31,7 +31,7 @@ function attemptToJoinGame (event) {
|
||||
mode: 'cors'
|
||||
}
|
||||
).then((res) => {
|
||||
if (!res.ok) {
|
||||
if (!res.ok && !(res.status === 304)) {
|
||||
switch (res.status) {
|
||||
case 404:
|
||||
toast('Game not found', 'error', true);
|
||||
|
||||
@@ -28,7 +28,7 @@ const joinHandler = (e) => {
|
||||
if (validateName(name)) {
|
||||
sendJoinRequest(e, name, accessCode)
|
||||
.then((res) => {
|
||||
if (!res.ok) {
|
||||
if (!res.ok && !(res.status === 304)) {
|
||||
switch (res.status) {
|
||||
case 404:
|
||||
toast('Game not found', 'error', true);
|
||||
|
||||
@@ -18,7 +18,7 @@ const template =
|
||||
<div></div>
|
||||
<div></div>
|
||||
</div>
|
||||
<p>Connecting to the Room...</p>
|
||||
<p>Waiting for Connection to Room...</p>
|
||||
</div>
|
||||
<div id="mobile-menu-background-overlay"></div>
|
||||
<div id="navbar"></div>
|
||||
|
||||
Reference in New Issue
Block a user