This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Call.ts
126 lines (116 loc) · 3.75 KB
/
Call.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { openClient } from "../js/socket-client.ts";
import { Component, computed, css, html, reactive } from "./deps.ts";
import { AButton } from "./button.ts";
import { globalStyles } from "./global_styles.ts";
// deno-lint-ignore no-undef
class CCall extends Component {
private client = new WebSocket("ws://127.0.0.1:1668");
#extensions = reactive([]);
#selectedFromExtension = reactive("");
#selectedToExtension = reactive("");
static styles = css`${globalStyles}` as never;
template = html`
<p>Remember to refresh the extensions to make a call!</p>
<div class="row flex">
<div class="col-6 text-align-c">
<p>Select an extension to call from</p>
<select id="extension-to-call-from" prop:value=${this.#selectedFromExtension}>
<option value="" selected=${this.#selectedFromExtension.value ===
""}>Select...</option>
${
computed(() => {
return html`
${
this.#extensions.value.map((extension: number) => {
if (extension === Number(this.#selectedFromExtension.value)) {
return html`
<option value=${extension} style="display: none;" selected="true">${extension}</option>
`;
}
if (extension === Number(this.#selectedToExtension)) {
return "";
}
return html`<option value=${extension}>${extension}</option>`;
})
}
`;
})
}
</select>
</div>
<div class="col-6 text-align-c">
<p>Select an extension to call to</p>
<select id="extension-to-call-to" prop:value=${this.#selectedToExtension}>
<option value="" selected=${this.#selectedToExtension
.value === ""}>Select...</option>
${
computed(() => {
return html`
${
this.#extensions.value.map((extension: number) => {
// If we selected it, hide it
if (extension === Number(this.#selectedToExtension.value)) {
return html
`<option value=${extension} style="display: none;" selected="true">${extension}</option>`;
}
// If other field selected it, hide it
if (extension === Number(this.#selectedFromExtension)) {
return "";
}
return html`<option value=${extension}>${extension}</option>`;
})
}
`;
})
}
</select>
</div>
</div>
<div class="row">
<${AButton} prop:id=${"initiate-call"} prop:text=${"Initiate Call"} on:click=${() =>
this.handleInitiateCallClick()}></${AButton}>
</div>
<div class="row">
<audio id="audio-remote" controls="true">
<p>Your browser doesn't support HTML5 audio.</p>
</audio>
</div>
`;
connectedCallback() {
this.initialiseSocketClient();
}
private handleInitiateCallClick() {
this.client!.send(JSON.stringify({
send_packet: {
to: "make-call",
message: {
to_extension: Number(this.#selectedToExtension.value),
from_extension: Number(this.#selectedFromExtension.value),
},
},
}));
}
private async initialiseSocketClient() {
await openClient(this.client);
this.client.send(JSON.stringify({
connect_to: ["get-extensions", "make-call"],
}));
this.client.onmessage = (msg) => {
if (msg.data.indexOf("Connected to") > -1) {
return;
}
const data = JSON.parse(msg.data); // { from, to, message }
data.message = JSON.parse(data.message);
if (data.to === "get-extensions") {
this.#extensions.value = data.message;
}
};
this.client.send(JSON.stringify({
send_packet: {
to: "get-extensions",
data: "",
},
}));
}
}
export { CCall };