-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
179 lines (145 loc) · 5.15 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
let socket = null;
let dataChannels = {};
let peerConnections = {};
const SERVERS = null;
let localStream = null;
let localAudio = new Audio();
let remoteAudio = {};
const userMediaConstraints = {
audio: true,
video: false
};
const offerOptions = {
offerToReceiveVideo: 0,
offerToReceiveAudio: 1
};
function createRTC(socket, clientId) {
let peerConnection = new RTCPeerConnection(SERVERS);
peerConnection.addStream(localStream);
peerConnection.onicecandidate = (e) => {
if (e.candidate) {
console.log('send ice candidate to ' + clientId);
socket.emit('send ice candidate', {candidate: e.candidate, toClientId: clientId});
}
};
peerConnection.onaddstream = (e) => {
console.log("Remote stream was added!");
remoteAudio[clientId] = new Audio();
remoteAudio[clientId].srcObject = e.stream;
remoteAudio[clientId].play();
}
return peerConnection;
}
function initiateSignaling(socket, peerConnection, clientId) {
initiateDataChannel(peerConnection, clientId);
peerConnection.createOffer(offerOptions).then(
offer => {
peerConnection.setLocalDescription(offer);
console.log('send offer to ' + clientId);
socket.emit('send offer', {offer, toClientId: clientId});
},
err => {
if (err) throw err;
});
}
function initiateDataChannel(peerConnection, clientId) {
dataChannel = peerConnection.createDataChannel(
'messageChannel',
{ reliable: false }
);
dataChannel.onopen = () => {
console.log('open datachannel');
dataChannel.onmessage = (message) => {
console.log('ondatachannel onmessage (initiateDataChannel)');
const data = JSON.parse(message.data);
handleIncomingMessage(data.message)
}
};
dataChannels[clientId] = dataChannel;
}
function sendAnswer(socket, offer, peerConnection, toClientId) {
peerConnection.setRemoteDescription(offer);
peerConnection.createAnswer((answer) => {
peerConnection.setLocalDescription(answer);
console.log('send answer to ' + toClientId);
socket.emit('send answer', {answer, toClientId});
}, (err) => {
if (err) throw err;
});
}
$(document).ready(() => {
navigator.mediaDevices
.getUserMedia(userMediaConstraints)
.then(function(stream) {
localStream = stream;
socket.emit('send join');
console.log('join');
})
.catch(function(e) {
alert('getUserMedia() error: ' + e.name);
});
socket = io();
socket.on('receive client joined', function(clientId) {
console.log('new client joined ('+clientId+')');
peerConnection = createRTC(socket, clientId);
initiateSignaling(socket, peerConnection, clientId);
peerConnections[clientId] = peerConnection;
});
socket.on('receive offer', ({offer, fromClientId}) => {
console.log('receive offer from ' + fromClientId);
let peerConnection = createRTC(socket, fromClientId);
sendAnswer(socket, offer, peerConnection, fromClientId);
peerConnection.ondatachannel = (e) => {
dataChannel = e.channel;
dataChannel.onmessage = (message) => {
console.log('ondatachannel onmessage');
const data = JSON.parse(message.data);
handleIncomingMessage(data.message);
};
dataChannels[fromClientId] = dataChannel;
};
peerConnections[fromClientId] = peerConnection;
});
socket.on('receive answer', ({answer, fromClientId}) => {
console.log('receive answer from ' + fromClientId);
peerConnections[fromClientId].setRemoteDescription(answer);
});
socket.on('receive ice candidate', ({candidate, fromClientId}) => {
console.log('receive ice candidate from ' + fromClientId);
console.log(candidate);
peerConnections[fromClientId].addIceCandidate(candidate);
});
socket.on('receive client leave', function(clientId) {
console.log('client leave ('+clientId+')');
delete peerConnections[clientId];
delete dataChannels[clientId];
});
$('window').on('unload', () => {
socket.emit('leave page');
});
$('form').on('submit', (e) => {
e.preventDefault();
});
$('#sendMessage').on('click', function() {
const message = $(this).siblings()[0].value;
handleIncomingMessage(message);
$(this).siblings()[0].value = "";
const data = JSON.stringify({ type: 'message', message });
for(var k in dataChannels) {
dataChannels[k].send(data);
}
});
$('#listClients').on('click', function() {
for(var k in peerConnections) {
console.log(k+" "+peerConnections[k].iceConnectionState);
}
for(var k in dataChannels) {
console.log(k+" "+dataChannels);
}
});
});
function handleIncomingMessage(message) {
const messageElement = $('<p></p>', { class: 'message'});
messageElement.text(message);
$('#chat-window').append(messageElement);
}