aboutsummaryrefslogtreecommitdiff
path: root/client-web/source/room.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client-web/source/room.ts')
-rw-r--r--client-web/source/room.ts38
1 files changed, 23 insertions, 15 deletions
diff --git a/client-web/source/room.ts b/client-web/source/room.ts
index af85044..c22a956 100644
--- a/client-web/source/room.ts
+++ b/client-web/source/room.ts
@@ -13,6 +13,7 @@ export class Room {
users: Map<number, User> = new Map()
remote_users: Map<number, RemoteUser> = new Map()
local_user!: LocalUser
+ my_id!: number
websocket: WebSocket
constructor(name: string) {
@@ -34,17 +35,21 @@ export class Room {
websocket_message(packet: ClientboundPacket) {
log("ws", `<- ${packet.message?.sender ?? "control packet"}: `, packet);
if (packet.init) {
- this.local_user = new LocalUser(this, packet.init.your_id, "...");
- }
- if (packet.client_join) {
+ this.my_id = packet.init.your_id
+ // no need to check compat for now because this is hosted in the same place
+ log("*", `server: ${packet.init.version}`)
+ } else if (packet.client_join) {
const p = packet.client_join
log("*", `${this.name} ${p.id} joined`);
- const ru = new RemoteUser(this, p.id, p.name)
- this.local_user.add_initial_to_remote(ru)
- ru.offer()
- this.users.set(p.id, ru)
- this.remote_users.set(p.id, ru)
- return
+ if (p.id == this.my_id) {
+ this.local_user = new LocalUser(this, p.id, p.name);
+ } else {
+ const ru = new RemoteUser(this, p.id, p.name)
+ this.local_user.add_initial_to_remote(ru)
+ ru.offer()
+ this.users.set(p.id, ru)
+ this.remote_users.set(p.id, ru)
+ }
} else if (packet.client_leave) {
const p = packet.client_leave;
log("*", `${this.name} ${p.id} left`);
@@ -52,13 +57,16 @@ export class Room {
this.users.delete(p.id)
this.remote_users.delete(p.id)
return
- }
- if (packet.message) {
+ } else if (packet.message) {
const p = packet.message;
- const sender = this.remote_users.get(p.sender)!
- if (p.message.ice_candidate) sender.add_ice_candidate(p.message.ice_candidate)
- if (p.message.offer) sender.on_offer(p.message.offer)
- if (p.message.answer) sender.on_answer(p.message.answer)
+ const sender = this.users.get(p.sender)
+ if (sender instanceof RemoteUser) {
+ if (p.message.ice_candidate) sender.add_ice_candidate(p.message.ice_candidate)
+ if (p.message.offer) sender.on_offer(p.message.offer)
+ if (p.message.answer) sender.on_answer(p.message.answer)
+ } else {
+ console.log("!", p, sender);
+ }
}
}
websocket_close() {