Skip to content

Commit

Permalink
Merge pull request #223 from fleizean/spa
Browse files Browse the repository at this point in the history
socket open close solved
  • Loading branch information
fyurtsev committed Apr 18, 2024
2 parents fd91880 + 2f11030 commit 2428c63
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion indianpong/pong/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ <h3>{{context.baseInfoSubHeaderText3}}</h3>
var animationId;
var localTournamentAnimationId;
var localGameAnimationId;
var websocket;
var matchsocket;
var rpsSocketActivity = false;
var pongSocketActivity = false;

function setLanguage(language) {
function setLanguage(language) {
document.cookie = "selectedLanguage=" + language;

swapApp(window.location.pathname);
Expand Down
6 changes: 2 additions & 4 deletions indianpong/static/js/game/sockPong.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const gameId = pathArray[3]; // Assuming game_id is the fourth segment of the UR


// Connect to the WebSocket server using the extracted game_id and game_type
const matchsocket = new WebSocket(`wss://${window.location.host}/ws/remote-game/${gameType}/${gameId}/`); //? Maybe we need to pass game type and game id here
pongSocketActivity = true;
matchsocket = new WebSocket(`wss://${window.location.host}/ws/remote-game/${gameType}/${gameId}/`); //? Maybe we need to pass game type and game id here


const canvas = document.getElementById('pongCanvas');
Expand Down Expand Up @@ -215,17 +216,14 @@ function scoreUpdate(player1_score, player2_score) {

matchsocket.onopen = function (e) {
// Show some greeting message
console.log('WebSocket connection established');
}

matchsocket.onclose = function (e) {
//clearInterval(BallRequest);
stopGame();
console.error('WebSocket connection closed');
}

matchsocket.onerror = function (e) {
console.error('Error: ' + e.data);
//clearInterval(BallRequest);
stopGame();
}
Expand Down
4 changes: 4 additions & 0 deletions indianpong/static/js/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ window.onpopstate = function(event) {
function pageHandler(path) {
const pathParts = path.split('/');
getCookie();
if (rpsSocketActivity)
websocket.close();
if (pongSocketActivity)
matchsocket.close();
if (animationId)
cancelAnimationFrame(animationId);
if (localTournamentAnimationId)
Expand Down
6 changes: 2 additions & 4 deletions indianpong/static/js/sockRps.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ function showToast(content, status, iconClass) {
}, 8000);
}

rpsSocketActivity = true;
const wsEndpoint = 'wss://' + window.location.host + '/ws/rps/';
const websocket = new WebSocket(wsEndpoint);
websocket = new WebSocket(wsEndpoint);

const cookie = document.cookie.split('; ').find(row => row.startsWith('selectedLanguage='));
const selectedLanguage = cookie ? cookie.split('=')[1] : 'en';
Expand Down Expand Up @@ -79,16 +80,13 @@ function scoreUpdate(player1_score, player2_score) {

websocket.onopen = function (e) {
// Show some greeting message
console.log('WebSocket connection established');
}

websocket.onclose = function (e) {
// stop the game
console.error('WebSocket connection closed');
}

websocket.onerror = function (e) {
console.error('Error: ' + e.data.error);
// stop the game
}

Expand Down

0 comments on commit 2428c63

Please sign in to comment.