aboutsummaryrefslogtreecommitdiff
path: root/client-web/source/user
diff options
context:
space:
mode:
Diffstat (limited to 'client-web/source/user')
-rw-r--r--client-web/source/user/local.ts11
-rw-r--r--client-web/source/user/mod.ts21
-rw-r--r--client-web/source/user/remote.ts23
3 files changed, 41 insertions, 14 deletions
diff --git a/client-web/source/user/local.ts b/client-web/source/user/local.ts
index a741726..2ea1ea6 100644
--- a/client-web/source/user/local.ts
+++ b/client-web/source/user/local.ts
@@ -7,7 +7,7 @@ import { get_rnnoise_node } from "../rnnoise.ts";
import { Room } from "../room.ts";
import { TrackHandle } from "../track_handle.ts";
import { User } from "./mod.ts";
-import { BOTTOM_CONTAINER } from "../index.ts";
+import { BOTTOM_CONTAINER, ROOM_CONTAINER } from "../index.ts";
export class LocalUser extends User {
mic_gain?: GainNode
@@ -17,10 +17,16 @@ export class LocalUser extends User {
super(room, id)
this.el.classList.add("local")
this.local = true
+ this.name = PREFS.username
this.create_controls()
this.add_initial_tracks()
log("usermodel", `added local user: ${this.display_name}`)
}
+ leave() { // we might never need this but ok
+ this.room.local_user = undefined as unknown as LocalUser
+ super.leave()
+ ROOM_CONTAINER.removeChild(this.el)
+ }
async add_initial_tracks() {
if (PREFS.microphone_enabled) this.publish_track(await this.create_mic_track())
@@ -43,6 +49,9 @@ export class LocalUser extends User {
add_initial_to_remote(u: RemoteUser) {
this.tracks.forEach(t => u.peer.addTrack(t.track))
}
+ identify(recipient?: number) {
+ if (this.name) this.room.signaling.send_relay({ identify: { username: this.name } }, recipient)
+ }
create_controls() {
const mic_toggle = document.createElement("input")
diff --git a/client-web/source/user/mod.ts b/client-web/source/user/mod.ts
index c0aa6be..cbe9468 100644
--- a/client-web/source/user/mod.ts
+++ b/client-web/source/user/mod.ts
@@ -9,15 +9,25 @@ import { TrackHandle } from "../track_handle.ts";
export abstract class User {
protected el: HTMLElement
public local = false
- public name?: string
protected tracks: Set<TrackHandle> = new Set()
+ private name_el = document.createElement("span")
+ private _name?: string
+ get name() { return this._name }
+ set name(n: string | undefined) { this._name = n; this.name_el.textContent = this.display_name }
+ get display_name() { return this.name ?? `guest (${this.id})` }
+
constructor(public room: Room, public id: number) {
+ room.users.set(this.id, this)
+
this.el = document.createElement("div")
this.el.classList.add("user")
ROOM_CONTAINER.append(this.el)
this.setup_view()
}
+ leave() {
+ this.room.users.delete(this.id)
+ }
add_track(t: TrackHandle) {
this.tracks.add(t)
@@ -34,15 +44,12 @@ export abstract class User {
})
}
- get display_name() { return this.name ?? `guest (${this.id})` }
-
setup_view() {
const info_el = document.createElement("div")
info_el.classList.add("info")
- const name_el = document.createElement("span")
- name_el.textContent = this.display_name
- name_el.classList.add("name")
- info_el.append(name_el)
+ this.name_el.textContent = this.display_name
+ this.name_el.classList.add("name")
+ info_el.append(this.name_el)
this.el.append(info_el)
}
diff --git a/client-web/source/user/remote.ts b/client-web/source/user/remote.ts
index ced8482..dbae3db 100644
--- a/client-web/source/user/remote.ts
+++ b/client-web/source/user/remote.ts
@@ -1,5 +1,6 @@
/// <reference lib="dom" />
+import { RelayMessage } from "../../../common/packets.d.ts";
import { ROOM_CONTAINER, RTC_CONFIG } from "../index.ts"
import { log } from "../logger.ts"
import { Room } from "../room.ts"
@@ -12,6 +13,8 @@ export class RemoteUser extends User {
constructor(room: Room, id: number) {
super(room, id)
+ room.remote_users.set(this.id, this)
+
log("usermodel", `added remote user: ${id}`)
this.peer = new RTCPeerConnection(RTC_CONFIG)
this.peer.onicecandidate = ev => {
@@ -31,6 +34,20 @@ export class RemoteUser extends User {
this.offer()
}
}
+ leave() {
+ log("usermodel", `remove remote user: ${this.display_name}`)
+ this.peer.close()
+ this.room.remote_users.delete(this.id)
+ super.leave()
+ ROOM_CONTAINER.removeChild(this.el)
+ }
+
+ on_relay(message: RelayMessage) {
+ if (message.ice_candidate) this.add_ice_candidate(message.ice_candidate)
+ if (message.offer) this.on_offer(message.offer)
+ if (message.answer) this.on_answer(message.answer)
+ if (message.identify) this.name = message.identify.username
+ }
async offer() {
this.negotiation_busy = true
@@ -65,10 +82,4 @@ export class RemoteUser extends User {
add_ice_candidate(candidate: RTCIceCandidateInit) {
this.peer.addIceCandidate(new RTCIceCandidate(candidate))
}
-
- leave() {
- log("usermodel", `remove remote user: ${this.display_name}`)
- this.peer.close()
- ROOM_CONTAINER.removeChild(this.el)
- }
} \ No newline at end of file