aboutsummaryrefslogtreecommitdiff
path: root/source/client/room.ts
blob: 100f6cf6faa146149c9681539812de3917c6df2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { CSPacket, SCPacket} from "./types";
import { User } from "./user";


export class Room {
    el: HTMLElement
    name: string
    users: Map<string, User> = new Map()
    websocket: WebSocket
    local_user: User

    constructor(name: string) {
        this.name = name
        this.el = document.createElement("div")

        this.websocket = new WebSocket(`ws://${window.location.host}/room/${encodeURIComponent(name)}`)
        this.websocket.onclose = () => this.websocket_close()
        this.websocket.onopen = () => this.websocket_open()
        this.websocket.onmessage = (ev) => {
            this.websocket_message(JSON.parse(ev.data))
        }
        // const name =  prompt() ?? "nameless user"
        const uname = Math.random().toString()
        this.local_user = new User(this, uname, true)
    }

    websocket_send(data: CSPacket) {
        this.websocket.send(JSON.stringify(data))
    }
    websocket_message(packet: SCPacket) {
        console.log("websocket message", packet);
        if (packet.join) {
            this.users.set(packet.sender, new User(this, packet.sender))
            return
        }
        const sender = this.users.get(packet.sender)
        if (!sender) return console.warn(`unknown sender ${packet.sender}`)
        if (packet.leave) {
            sender.leave()
            this.users.delete(packet.sender)
            return
        }

        if (packet.data.ice_candiate) sender.add_ice_candidate(packet.data.ice_candiate)
    }
    websocket_close() {
        console.log("websocket closed");
    }
    websocket_open() {
        console.log("websocket opened");
        this.websocket.send(this.local_user.name)
    }
}