-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsSocketRTC.js
More file actions
93 lines (75 loc) · 2.83 KB
/
wsSocketRTC.js
File metadata and controls
93 lines (75 loc) · 2.83 KB
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
// http://192.168.43.135:5500/
const socket = io('http://192.168.43.135:3000');
const boxUsers = document.getElementById('box-users');
socket.emit('new-user', name);
socket.on('joined-user', users => {
boxUsers.innerHTML = "";
for (key in users) {
renderUsersEL(users[key]);
}
});
socket.on('user-active', users => {
boxUsers.innerHTML = "";
for (key in users) {
renderUsersEL(users[key]);
}
});
function renderUsersEL(user) {
let nel = document.createElement('li');
if (user == name) user += " - active";
nel.innerHTML = user;
boxUsers.appendChild(nel);
}
const boxStream = document.getElementById('box-stream');
const streamBTN = document.getElementById('stream-btn');
const peerConnection = new RTCPeerConnection();
streamBTN.addEventListener('click', startStream);
// Browser `A` start stream
function startStream() {
let video = document.createElement('video');
video.muted = true;
let media = navigator.mediaDevices.getUserMedia({
'video': true,
'audio': true
});
media.then(async stream => {
video.srcObject = stream;
video.play();
// Browser `A` will add media stream into a peer
peerConnection.addStream(stream);
// Browser `A` will creating SDP That contained all information of stream media
await peerConnection.createOffer().then(sdp => peerConnection.setLocalDescription(sdp));
// And broadcasting into connected browser `B`
socket.emit('offer', JSON.stringify(peerConnection.localDescription));
boxStream.appendChild(video);
});
}
// in browser `B` wll accepting offer from browser `A`
socket.on('offer', message => {
// SDP that has received wlll be set into
// RemoteDescription inside PeerConnection in
// browser `B` and than peerConnection also
// procceding SDP to send back in browser `A`
console.log(JSON.parse(message));
peerConnection.setRemoteDescription(JSON.parse(message))
.then(() => peerConnection.createAnswer())
.then(sdp => peerConnection.setLocalDescription(sdp))
.then(() => socket.emit('answer', JSON.stringify(peerConnection.localDescription)));
// in process to create and sendback the SDP,
// peerConnection in browser `B` wll receive a
// stream that have broacasting by the browser `A`
peerConnection.onaddstream = e => {
const videoELement = document.createElement('video');
videoELement.muted = true;
videoELement.srcObject = event.stream;
videoELement.play();
boxStream.append(videoELement);
};
});
// After getting the answere from browser `B`
// peerConnection in browser `A` wll set
// remote description from browser `B` and
// establish comunication p2p
socket.on('answer', async message => {
peerConnection.setRemoteDescription(JSON.parse(message));
});