Skip to content

Fix event handling bugs and cleanup #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function FindAddStack(room: string, currentStack): Datastructure.TopperStack {
}

io.on('connection', function(client) {
// Send current user list to current user only
client.emit('update_toppers', totalToppers);
// Send current user count to the connecting client only
client.emit('total_toppers', totalToppers);

client.on('addUser', function(data: Datastructure.ITopper) {
data.id = client.id;
Expand Down
9 changes: 7 additions & 2 deletions src/app/chat/chat.component.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
AfterViewChecked,
ElementRef,
ViewChild,
OnInit
OnInit,
OnDestroy
} from '@angular/core';

import { SocketService } from '../socket.service';
Expand All @@ -29,7 +30,7 @@ import { Chat } from '../../../Datastructure/Message';
'b { color: #2e69c9; }'
]
})
export class ChatComponent implements OnInit, AfterViewChecked {
export class ChatComponent implements OnInit, AfterViewChecked, OnDestroy {
@ViewChild('chatwindow') private myScrollContainer: ElementRef;

private previousChatHeight: number = 0;
Expand All @@ -52,6 +53,10 @@ export class ChatComponent implements OnInit, AfterViewChecked {
});
}

ngOnDestroy() {
this.socketService.getSocketConnection().off('message');
}

sendMessage(event) {
let message: Chat.Message = {
message: event.target.value,
Expand Down
25 changes: 18 additions & 7 deletions src/app/register/register.component.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { SocketService } from '../socket.service';
import { Datastructure } from '../../../Datastructure/TopperStack';
import { Router } from '@angular/router';
Expand Down Expand Up @@ -26,7 +26,7 @@ import { Router } from '@angular/router';
</div>
<router-outlet></router-outlet>`,
})
export class RegisterComponent implements OnInit {
export class RegisterComponent implements OnInit, OnDestroy {
currentTopper: Datastructure.ITopper;
usernameInvalid: boolean = false;
onlineCount: number = 0;
Expand All @@ -38,17 +38,21 @@ export class RegisterComponent implements OnInit {
}

addUser() {
if (!this.currentTopper.name) {
this.usernameInvalid = true;
return;
}
this.socketService
.getSocketConnection()
.emit('addUser', this.currentTopper);
}

ngOnInit() {
this.socketService
.getSocketConnection()
.on('update_toppers', totalTopppers => {
this.onlineCount = totalTopppers;
});
this.socketService
.getSocketConnection()
.on('total_toppers', total => {
this.onlineCount = total;
});

this.socketService.getSocketConnection().on('update_me', data => {
this.currentTopper.id = data.id;
Expand All @@ -60,4 +64,11 @@ export class RegisterComponent implements OnInit {
this.usernameInvalid = true;
});
}

ngOnDestroy() {
const socket = this.socketService.getSocketConnection();
socket.off('total_toppers');
socket.off('update_me');
socket.off('register_failed');
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're unsubscribing from 'register_failed', but there’s no matching .on('register_failed', ...) listener in this component. Remove this off call or add the corresponding subscription.

Copilot uses AI. Check for mistakes.

}
}
8 changes: 6 additions & 2 deletions src/app/top-main/top-main.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Datastructure } from '../../../Datastructure/TopperStack';
import { SocketService } from '../socket.service';
import { Router } from '@angular/router';
Expand Down Expand Up @@ -48,7 +48,7 @@ import { Router } from '@angular/router';
<router-outlet></router-outlet>`,
styles: ['#topperList { padding:0px; margin-top:0px; }'],
})
export class TopMainComponent implements OnInit {
export class TopMainComponent implements OnInit, OnDestroy {
socketService: SocketService;
currentTopper: Datastructure.ITopper;
topperList: Datastructure.TopperStack = new Datastructure.TopperStack([]);
Expand All @@ -68,6 +68,10 @@ export class TopMainComponent implements OnInit {
});
}

ngOnDestroy() {
this.socketService.getSocketConnection().off('update_toppers');
}

topMe() {
this.socketService.getSocketConnection().emit('top_me', this.currentTopper);
}
Expand Down