-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (128 loc) · 4.6 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
console.clear();
window.onload = async () => {
const
chat = document.querySelector("#chat"),
video = document.querySelector("#video"),
status = document.querySelector("#status"),
peerId = document.querySelector("#peerId"),
callBtn = document.querySelector("#callBtn"),
chatInput = document.querySelector("#chatInput"),
connection = document.querySelector("#connection");
const audio = await navigator.mediaDevices.getUserMedia({video: false, audio: true})
const response = await fetch("https://fulldroper.metered.live/api/v1/turn/credentials?apiKey=20b057434f2dba67cce42dbf43a66658ba5d");
const servers = await response.json()
const peer = new Peer({
config: {'iceServers': servers },
timeout: 120000
});
peer.on('open', id => {
status.innerHTML = "Online";
peerId.innerHTML = id;
let call;
const chatHandler = function(conn) {
status.innerHTML = "Connected to remote";
connection.style.display = "none";
chatInput.style.visibility = "visible";
callBtn.style.display = "block";
const endCall = function(e) {
status.innerHTML = "Connected to remote";
callBtn.innerHTML = "Voice Call";
console.log(e)
callBtn.onclick = makeCall;
call?.close();
}
const makeCall = function({target}) {
target.innerHTML = "End call";
call = peer.call(conn.peer, audio);
call.on('stream', function(stream) {
status.innerHTML = "Connected to voice";
video.srcObject = stream;
video.play();
callBtn.onclick = endCall;
call.on('close', endCall);
call.on('error', endCall);
})
}
callBtn.onclick = makeCall;
conn.on('data', function(data) {
const msg = document.createElement("li");
msg.innerHTML = 'Remote: ' + data;
chat.appendChild(msg);
chat.scrollTop = chat.scrollHeight;
});
conn.on('close', function() {
status.innerHTML = "Disconnected from remote";
connection.style.display = "block";
chatInput.style.visibility = "collapse";
callBtn.style.display = "none";
})
conn.on('error', function(e) {
status.innerHTML = "Disconnected from remote with error";
connection.style.display = "block";
chatInput.style.visibility = "collapse";
callBtn.style.display = "none";
console.log(e);
})
chatInput.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
if(!chatInput.value) return;
conn.send(chatInput.value)
const msg = document.createElement("li");
msg.innerHTML = 'You: ' + chatInput.value;
chat.appendChild(msg);
chatInput.value = "";
chat.scrollTop = chat.scrollHeight;
}
});
}
connection.addEventListener("keypress", function(e) {
if (e.key === "Enter") {
e.preventDefault();
if(!connection.value) return;
const c = peer.connect(connection.value.trim())
c.on('open', () => chatHandler(c))
connection.value = "";
connection.style.visibility = "collapsed";
}
});
peer.on('connection', c => c.on('open', () => chatHandler(c)))
peer.on('close', () => {
status.innerHTML = "Offline";
})
peer.on('error', (e) => {
status.innerHTML = "Error " + e.type;
})
peer.on('call', call => {
call.answer(audio);
call.on('stream', function(stream) {
status.innerHTML = "Connected to voice";
callBtn.innerHTML = "End call";
video.srcObject = stream;
video.play();
callBtn.onclick = endCall;
call.on('close', endCall);
call.on('error', endCall);
})
const endCall = function(e) {
status.innerHTML = "Connected to remote";
callBtn.innerHTML = "Voice Call";
callBtn.onclick = makeCall;
console.log(e)
call?.close();
}
const makeCall = function({target}) {
status.innerHTML = "Connected to voice";
target.innerHTML = "End call";
call.on('stream', function(stream) {
video.srcObject = stream;
video.play();
callBtn.onclick = endCall;
call.on('close', endCall);
call.on('error', endCall);
})
}
callBtn.onclick = endCall;
});
});
}