summaryrefslogtreecommitdiff
path: root/client-web/source/protocol/mod.ts
blob: 4fbd6078274dccfdc36a08d22266d8657bb0be8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { log } from "../logger.ts"
import { crypto_seeded_key, crypt_hash } from "./crypto.ts"

export class SignalingConnection {
    room!: string
    websocket!: WebSocket
    signaling_id!: string
    key!: CryptoKey

    constructor() { }
    async connect(room: string): Promise<SignalingConnection> {
        this.key = await crypto_seeded_key(room)
        this.signaling_id = await crypt_hash(room)
        log("ws", "connecting…")
        const ws_url = new URL(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.host}/signaling/${encodeURIComponent(this.signaling_id)}`)
        this.websocket = new WebSocket(ws_url)
        await new Promise(r => this.websocket!.onopen = r)
        log("ws", "connection opened")
        return this
    }


}