-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (72 loc) · 2.4 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
var peerClient;
var currentPeerConnection;
var localMediaStream;
var myselfId = document.getElementById('js-myself-id');
var peerId = document.getElementById('js-peer-id');
var partnerId = document.getElementById('js-partner-id');
var open = document.getElementById('js-open');
var connect = document.getElementById('js-connect');
var videoMyself = document.querySelector('#js-video-myself');
var videoPartner = document.querySelector('#js-video-partner');
navigator.mediaDevices.getUserMedia({video: true, audio: true})
.then(function(stream) {
videoMyself.srcObject = stream
videoMyself.play();
localMediaStream = stream;
})
open.onclick = function(e) {
// create peer object
peerClient = new Peer(myselfId.value);
// if peer connection is opened
peerClient.on('open', function() {
peerId.innerText = peerClient.id;
});
peerClient.on('call', function(call) {
// answer with my media stream
call.answer(localMediaStream);
// close current connection if exists
if (currentPeerConnection) {
currentPeerConnection.close();
}
// keep call as currentPeerConnection
currentPeerConnection = call;
// wait for partner's stream
call.on('stream', function(stream) {
videoPartner.srcObject = stream
videoPartner.play();
});
// if connection is closed
call.on('close', function() {
console.log('Connection is closed.');
});
});
// disable id input
myselfId.setAttribute('disabled', '');
// enable partner id input
partnerId.removeAttribute('disabled');
// enable connect button
connect.removeAttribute('disabled');
};
connect.onclick = function(e) {
// if peerClient is not initialized
if (!peerClient) {
return;
}
// connect to partner
var call = peerClient.call(partnerId.value, localMediaStream);
// close current connection if exists
if (currentPeerConnection) {
currentPeerConnection.close();
}
// keep call as currentPeerConnection
currentPeerConnection = call;
// wait for partner's stream
call.on('stream', function(stream) {
videoPartner.srcObject = stream
videoPartner.play();
});
// if connection is closed
call.on('close', function() {
console.log('Connection is closed.');
});
};