-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor server logics and introduces RoomManager, UUID based user sy…
…stem. Refactored server logic in the source index file to now utilize a brand new RoomManager class and UUID based identification system, rather than relying on a username header. RoomManager class allows the system to easily manage joins and leaves of a room, as well optimal handling and removal of empty rooms. UUID based user identification was chosen over the previous username system to ensure uniqueness of each user and to avoid potential collisions. This will greatly improve the consistency and performance of the system.
- Loading branch information
Showing
4 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export class SimplePacket { | ||
|
||
private readonly type: string; | ||
private readonly data: unknown; | ||
|
||
constructor(type: string, data: unknown) { | ||
this.type = type; | ||
this.data = data; | ||
} | ||
|
||
toString() : string { | ||
return JSON.stringify({ | ||
'type': this.type, | ||
'data': this.data | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
export default class RoomManager { | ||
|
||
private readonly maxClients: number; | ||
private rooms: Map<string, Room> = new Map; | ||
|
||
constructor(maxClients: number) { | ||
this.maxClients = maxClients; | ||
} | ||
|
||
joinRoom(room: string, client: string) : boolean { | ||
if ( ! this.rooms.has(room) ) { | ||
this.rooms.set(room, new Room(client)) | ||
} else { | ||
if(this.rooms.get(room)!.getClients() >= this.maxClients){ | ||
return false | ||
} | ||
this.rooms.get(room)?.join(client) | ||
} | ||
return true | ||
} | ||
|
||
leaveRoom(room: string, client: string) { | ||
if( this.rooms.has(room) ) { | ||
this.rooms.get(room)?.leave(client) | ||
if(this.rooms.get(room)!.getClients() <= 0) { | ||
this.rooms.delete(room) | ||
} | ||
} | ||
} | ||
|
||
getRoom(room: string) : string { | ||
if( this.rooms.has(room) ) { | ||
let roomData = this.rooms.get(room); | ||
return JSON.stringify({ | ||
'host': roomData?.getHost(), | ||
'clients': roomData?.getClientList() | ||
}); | ||
} | ||
return "" | ||
} | ||
} | ||
|
||
class Room { | ||
|
||
private clients: string[] = []; | ||
private host: string; | ||
|
||
constructor(host: string) { | ||
this.host = host | ||
this.clients.push(host) | ||
} | ||
|
||
getHost() : string { | ||
return this.host | ||
} | ||
|
||
getClientList() : string[] { | ||
return this.clients | ||
} | ||
|
||
getClients() : number { | ||
return this.clients.length | ||
} | ||
|
||
join(client: string) { | ||
if(!this.clients.includes(client)) | ||
this.clients.push(client) | ||
} | ||
|
||
leave(client: string) { | ||
if(this.clients.includes(client)) | ||
this.clients = this.clients.filter(c => c !== client) | ||
if(client === this.host && this.getClients() > 0) { | ||
this.host = this.clients[0]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface ClientData { | ||
roomId: string, | ||
username: string | ||
uuid: string | ||
} |