forked from haovanvo/educationsystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (117 loc) · 3.87 KB
/
main.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
const socket = io('https://haovosocketserver.herokuapp.com/');
$('#div-chat').hide();
socket.on('DANH_SACH_ONLINE', arrUserInfo => {
$('#div-chat').show();
$('#div-dang-ky').hide();
arrUserInfo.forEach(user => {
const { ten, peerId } = user;
$('#ulUser').append(`<li id="${peerId}">${ten}-${peerId}</li>`);
});
socket.on('CO_NGUOI_DUNG_MOI', user => {
const { ten, peerId } = user;
$('#ulUser').append(`<li id="${peerId}">${ten}-${peerId}</li>`);
});
socket.on('AI_DO_NGAT_KET_NOI', peerId => {
$(`#${peerId}`).remove();
});
});
socket.on('DANG_KY_THAT_BAT', () => alert('Vui long chon username khac!'));
function openStream() {
const config = { audio: true, video: true };
return navigator.mediaDevices.getUserMedia(config);
}
function playStream(idVideoTag, stream) {
const video = document.getElementById(idVideoTag);
video.srcObject = stream;
video.play();
}
// openStream()
// .then(stream => playStream('localStream', stream));
var clientId = guid.s4;
const peer = new Peer(clientId, {
host: 'haovopeerserver.herokuapp.com',
secure: true,
port: 443,
path: '/live',
iceServers: [
{ url: 'stun:stun01.sipphone.com' },
{ url: 'stun:stun.ekiga.net' },
{ url: 'stun:stun.fwdnet.net' },
{ url: 'stun:stun.ideasip.com' },
{ url: 'stun:stun.iptel.org' },
{ url: 'stun:stun.rixtelecom.se' },
{ url: 'stun:stun.schlund.de' },
{ url: 'stun:stun.l.google.com:19302' },
{ url: 'stun:stun1.l.google.com:19302' },
{ url: 'stun:stun2.l.google.com:19302' },
{ url: 'stun:stun3.l.google.com:19302' },
{ url: 'stun:stun4.l.google.com:19302' },
{ url: 'stun:stunserver.org' },
{ url: 'stun:stun.softjoys.com' },
{ url: 'stun:stun.voiparound.com' },
{ url: 'stun:stun.voipbuster.com' },
{ url: 'stun:stun.voipstunt.com' },
{ url: 'stun:stun.voxgratia.org' },
{ url: 'stun:stun.xten.com' },
{
url: 'turn:numb.viagenie.ca',
credential: 'muazkh',
username: 'webrtc@live.com'
},
{
url: 'turn:192.158.29.39:3478?transport=udp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
},
{
url: 'turn:192.158.29.39:3478?transport=tcp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
}
]
});
peer.on('open', id => {
$('#my-peer').append(id);
$('#btnSignUp').click(() => {
const username = $('#txtUsername').val();
socket.emit('NGUOI_DUNG_DANG_KY', { ten: username, peerId: id });
});
});
//Caller
$('#btnCall').click(() => {
const id = $('#remoteId').val();
openStream()
.then(stream => {
playStream('localStream', stream);
const call = peer.call(id, stream);
call.on('stream', remoteStream => playStream('remoteStream', remoteStream));
});
});
//Callee
peer.on('call', call => {
openStream()
.then(stream => {
call.answer(stream);
playStream('localStream', stream);
call.on('stream', remoteStream => playStream('remoteStream', remoteStream));
});
});
$('#ulUser').on('click', 'li', function () {
const id = $(this).attr('id');
console.log(id);
openStream()
.then(stream => {
playStream('localStream', stream);
const call = peer.call(id, stream);
call.on('stream', remoteStream => playStream('remoteStream', remoteStream));
});
});
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}