Skip to content

Commit

Permalink
bugfix: after closing the socket, the sending message action will stop
Browse files Browse the repository at this point in the history
  • Loading branch information
penn201500 committed Mar 4, 2024
1 parent 95a2bff commit af64faa
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const API_SERVER_WSS = 'wss://wss.woo.org/ws/stream';
let socket = null;
let keepAliveIntervalId = null;

function displayMessage(message, isFromServer) {
const messagesContainer = document.getElementById('websocket-messages');
Expand Down Expand Up @@ -32,10 +33,12 @@ function initWebSocket(applicationId) {
const pongMsg = JSON.stringify({event: 'pong'});

// Send a pong message every 10 seconds to keep the connection alive
setInterval(() => {
console.log('Sending pong to keep the connection alive.');
displayMessage(pongMsg, false)
socket.send(pongMsg);
keepAliveIntervalId = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) { // Check if the connection is open
console.log('Sending pong to keep the connection alive.');
displayMessage(pongMsg, false);
socket.send(pongMsg);
}
}, 10000);
};

Expand All @@ -59,13 +62,17 @@ function initWebSocket(applicationId) {
console.log('WebSocket is closed now.');
displayMessage('WebSocket connection closed.', false);
};
}


function stopWebSocket() {
if (socket) {
socket.close();
socket = null;
}
function stopWebSocket() {
if (socket) {
socket.close();
socket = null;
}
// Clear the interval to stop sending the pong message
if (keepAliveIntervalId) {
clearInterval(keepAliveIntervalId);
keepAliveIntervalId = null;
}
}

Expand Down

0 comments on commit af64faa

Please sign in to comment.