Skip to content

Commit

Permalink
Merge pull request #105 from CS3219-AY2425S1/Aiden_back
Browse files Browse the repository at this point in the history
WebSocket ping ping ping
  • Loading branch information
smolegz authored Nov 13, 2024
2 parents 20df7c7 + 5fd9a71 commit 753afc0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions code-websocket/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ server.on('connection', (socket, request) => {
// Send the current code to the newly connected client
socket.send(JSON.stringify({ type: 'initialCode', content: sessionData[sessionID].code }));

const pingInterval = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) {
socket.ping();
console.log('ping ping ping');
}
}, 30000);

// Handle incoming messages
socket.on('message', (message) => {
const parsedMessage = JSON.parse(message);
Expand Down Expand Up @@ -91,6 +98,7 @@ server.on('connection', (socket, request) => {

// Handle client disconnection
socket.on('close', (code, reason) => {
clearInterval(pingInterval);
console.log(`User ${userID} disconnected from session ${sessionID}, code: ${code}, reason: ${reason}`);

// Remove the user from the active users list
Expand Down
2 changes: 1 addition & 1 deletion peer-prep-fe/src/collab-page/collab-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class CollabPageComponent implements OnInit, OnDestroy {
private route: ActivatedRoute,
private collabService: CollabService,
private editorWebSocketService: EditorWebSocketService, // Inject WebSocketService for code editor
private chatWebSocketService: ChatWebSocketService // Inject WebSocketService for chat
private chatWebSocketService: ChatWebSocketService, // Inject WebSocketService for chat
) {}

ngOnInit(): void {
Expand Down

0 comments on commit 753afc0

Please sign in to comment.