-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
124 lines (111 loc) · 5.07 KB
/
client.js
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
(function() {
let nameInput = document.getElementById('name_input');
let passwordInput = document.getElementById('password_input');
let connectButton = document.getElementById('connect_button');
let closeButton = document.getElementById('close_button');
let clientList = document.getElementById('client_list');
let callAllButton = document.getElementById('call_all_button');
let hangUpAllButton = document.getElementById('hang_up_all_button');
let closeAllRoomPeerConnectionsButton = document.getElementById('close_all_room_peer_connections');
let idInput = document.getElementById('id_input');
let callOneButton = document.getElementById('call_one_button');
let textInput = document.getElementById('text_input');
let sendButton = document.getElementById('send_button');
let chatTextArea = document.getElementById('chat_text_area');
closeButton.disabled = true;
callAllButton.disabled = true;
hangUpAllButton.disabled = true;
closeAllRoomPeerConnectionsButton.disabled = true;
callOneButton.disabled = true;
sendButton.disabled = true;
chatTextArea.value = '';
let dataChannelClient = null;
function connectDataChannelClientEvents() {
dataChannelClient.onSignalingConnectionOpen = () => {
connectButton.disabled = true;
closeButton.disabled = false;
};
dataChannelClient.onSignalingConnectionClose = async () => {
connectButton.disabled = false;
closeButton.disabled = true;
callAllButton.disabled = true;
hangUpAllButton.disabled = true;
closeAllRoomPeerConnectionsButton.disabled = true;
callOneButton.disabled = true;
};
dataChannelClient.onSignalingConnectionError = message => {
alert(message);
}
dataChannelClient.onRoomClientsChange = clients => {
callAllButton.disabled = !(clients.length > 1 && hangUpAllButton.disabled);
callOneButton.disabled = callAllButton.disabled;
clientList.innerHTML = '';
clients.forEach(client => {
let li = document.createElement('li');
li.textContent = client.id + ' - ' + client.name;
li.style.color = client.isConnected ? 'green' : 'red';
clientList.appendChild(li);
});
};
dataChannelClient.callAcceptor = async (id, name, clientData) => {
return confirm('Do you accept the call from ' + name + '?');
};
dataChannelClient.onCallReject = (id, name, clientData) => {
alert('The call is rejected (' + name + ')');
};
dataChannelClient.onClientConnectionFail = (id, name, clientData) => {
console.log('The connect with the client ' + name + '(' + id + ') failed.');
}
dataChannelClient.onDataChannelOpen = (id, name, clientData) => {
sendButton.disabled = false;
callAllButton.disabled = true;
hangUpAllButton.disabled = false;
closeAllRoomPeerConnectionsButton.disabled = false;
callOneButton.disabled = true;
}
dataChannelClient.onDataChannelClose = (id, name, clientData) => {
sendButton.disabled = !dataChannelClient.isRtcConnected;
callAllButton.disabled = dataChannelClient.isRtcConnected;
hangUpAllButton.disabled = !dataChannelClient.isRtcConnected;
closeAllRoomPeerConnectionsButton.disabled = !dataChannelClient.isRtcConnected;
callOneButton.disabled = dataChannelClient.isRtcConnected;
};
dataChannelClient.onDataChannelMessage = (id, name, clientData, message) => {
chatTextArea.value += id + ' - ' + name + ': ';
chatTextArea.value += message;
chatTextArea.value += '\n';
};
}
connectButton.onclick = async () => {
const SignalingServerConfiguration = {
url: 'ws://localhost:8080/signaling',
name: nameInput.value,
data: {}, // Client custom data
room: 'chat',
password: passwordInput.value
};
const DataChannelConfiguration = {}; // See: https://developer.mozilla.org/fr/docs/Web/API/RTCPeerConnection/createDataChannel#RTCDataChannelInit_dictionary
const RtcConfiguration = { // See: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#RTCConfiguration_dictionary
iceServers: await window.openteraWebrtcWebClient.iceServers.fetchFromServer('http://localhost:8080/iceservers', passwordInput.value)
};
let logger = (...args) => console.log(...args);
dataChannelClient = new window.openteraWebrtcWebClient.DataChannelClient(SignalingServerConfiguration,
DataChannelConfiguration, RtcConfiguration, logger);
connectDataChannelClientEvents();
await dataChannelClient.connect();
};
closeButton.onclick = () => {
dataChannelClient.close();
clientList.innerHTML = '';
};
callAllButton.onclick = () => dataChannelClient.callAll();
hangUpAllButton.onclick = () => dataChannelClient.hangUpAll();
closeAllRoomPeerConnectionsButton.onclick = () => dataChannelClient.closeAllRoomPeerConnections();
callOneButton.onclick = () => dataChannelClient.callIds([idInput.value]);
sendButton.onclick = () => {
chatTextArea.value += 'Me: ';
chatTextArea.value += textInput.value;
chatTextArea.value += '\n';
dataChannelClient.sendToAll(textInput.value)
};
})();