update syntax to es6
parent
93d28dd9c0
commit
cd7177d2d6
|
|
@ -1,11 +1,11 @@
|
||||||
var localVideo;
|
let localStream;
|
||||||
var localStream;
|
let localVideo;
|
||||||
var remoteVideo;
|
let peerConnection;
|
||||||
var peerConnection;
|
let remoteVideo;
|
||||||
var uuid;
|
let serverConnection;
|
||||||
var serverConnection;
|
let uuid;
|
||||||
|
|
||||||
var peerConnectionConfig = {
|
const peerConnectionConfig = {
|
||||||
'iceServers': [
|
'iceServers': [
|
||||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||||
{'urls': 'stun:stun.l.google.com:19302'},
|
{'urls': 'stun:stun.l.google.com:19302'},
|
||||||
|
|
@ -18,10 +18,10 @@ function pageReady() {
|
||||||
localVideo = document.getElementById('localVideo');
|
localVideo = document.getElementById('localVideo');
|
||||||
remoteVideo = document.getElementById('remoteVideo');
|
remoteVideo = document.getElementById('remoteVideo');
|
||||||
|
|
||||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
serverConnection = new WebSocket(`wss://${window.location.hostname}:8443`);
|
||||||
serverConnection.onmessage = gotMessageFromServer;
|
serverConnection.onmessage = gotMessageFromServer;
|
||||||
|
|
||||||
var constraints = {
|
const constraints = {
|
||||||
video: true,
|
video: true,
|
||||||
audio: true,
|
audio: true,
|
||||||
};
|
};
|
||||||
|
|
@ -52,13 +52,13 @@ function start(isCaller) {
|
||||||
function gotMessageFromServer(message) {
|
function gotMessageFromServer(message) {
|
||||||
if(!peerConnection) start(false);
|
if(!peerConnection) start(false);
|
||||||
|
|
||||||
var signal = JSON.parse(message.data);
|
const signal = JSON.parse(message.data);
|
||||||
|
|
||||||
// Ignore messages from ourself
|
// Ignore messages from ourself
|
||||||
if(signal.uuid == uuid) return;
|
if(signal.uuid == uuid) return;
|
||||||
|
|
||||||
if(signal.sdp) {
|
if(signal.sdp) {
|
||||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(() => {
|
||||||
// Only create answers in response to offers
|
// Only create answers in response to offers
|
||||||
if(signal.sdp.type == 'offer') {
|
if(signal.sdp.type == 'offer') {
|
||||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||||
|
|
@ -78,7 +78,7 @@ function gotIceCandidate(event) {
|
||||||
function createdDescription(description) {
|
function createdDescription(description) {
|
||||||
console.log('got description');
|
console.log('got description');
|
||||||
|
|
||||||
peerConnection.setLocalDescription(description).then(function() {
|
peerConnection.setLocalDescription(description).then(() => {
|
||||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
||||||
}).catch(errorHandler);
|
}).catch(errorHandler);
|
||||||
}
|
}
|
||||||
|
|
@ -99,5 +99,5 @@ function createUUID() {
|
||||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4() + s4() + s4()}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,55 +5,65 @@ const https = require('https');
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const WebSocketServer = WebSocket.Server;
|
const WebSocketServer = WebSocket.Server;
|
||||||
|
|
||||||
// Yes, TLS is required
|
// Yes, TLS is required for WebRTC
|
||||||
const serverConfig = {
|
const serverConfig = {
|
||||||
key: fs.readFileSync('key.pem'),
|
key: fs.readFileSync('key.pem'),
|
||||||
cert: fs.readFileSync('cert.pem'),
|
cert: fs.readFileSync('cert.pem'),
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------
|
function main() {
|
||||||
|
const httpsServer = startHttpsServer(serverConfig);
|
||||||
|
startWebSocketServer(httpsServer);
|
||||||
|
printHelp();
|
||||||
|
}
|
||||||
|
|
||||||
// Create a server for the client html page
|
function startHttpsServer(serverConfig) {
|
||||||
const handleRequest = function(request, response) {
|
// Handle incoming requests from the client
|
||||||
// Render the single client html file for any request the HTTP server receives
|
const handleRequest = (request, response) => {
|
||||||
console.log('request received: ' + request.url);
|
console.log(`request received: ${request.url}`);
|
||||||
|
|
||||||
if(request.url === '/') {
|
// This server only serves two files: The HTML page and the client JS file
|
||||||
response.writeHead(200, {'Content-Type': 'text/html'});
|
if(request.url === '/') {
|
||||||
response.end(fs.readFileSync('client/index.html'));
|
response.writeHead(200, {'Content-Type': 'text/html'});
|
||||||
} else if(request.url === '/webrtc.js') {
|
response.end(fs.readFileSync('client/index.html'));
|
||||||
response.writeHead(200, {'Content-Type': 'application/javascript'});
|
} else if(request.url === '/webrtc.js') {
|
||||||
response.end(fs.readFileSync('client/webrtc.js'));
|
response.writeHead(200, {'Content-Type': 'application/javascript'});
|
||||||
}
|
response.end(fs.readFileSync('client/webrtc.js'));
|
||||||
};
|
|
||||||
|
|
||||||
const httpsServer = https.createServer(serverConfig, handleRequest);
|
|
||||||
httpsServer.listen(HTTPS_PORT, '0.0.0.0');
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Create a server for handling websocket calls
|
|
||||||
const wss = new WebSocketServer({server: httpsServer});
|
|
||||||
|
|
||||||
wss.on('connection', function(ws) {
|
|
||||||
ws.on('message', function(message) {
|
|
||||||
// Broadcast any received message to all clients
|
|
||||||
console.log('received: %s', message);
|
|
||||||
wss.broadcast(message);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
wss.broadcast = function(data) {
|
|
||||||
this.clients.forEach(function(client) {
|
|
||||||
if(client.readyState === WebSocket.OPEN) {
|
|
||||||
client.send(data);
|
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
};
|
|
||||||
|
|
||||||
console.log('Server running. Visit https://localhost:' + HTTPS_PORT + ' in Firefox/Chrome.\n\n\
|
const httpsServer = https.createServer(serverConfig, handleRequest);
|
||||||
Some important notes:\n\
|
httpsServer.listen(HTTPS_PORT, '0.0.0.0');
|
||||||
* Note the HTTPS; there is no HTTP -> HTTPS redirect.\n\
|
return httpsServer;
|
||||||
* You\'ll also need to accept the invalid TLS certificate.\n\
|
}
|
||||||
* Some browsers or OSs may not allow the webcam to be used by multiple pages at once. You may need to use two different browsers or machines.\n'
|
|
||||||
);
|
function startWebSocketServer(httpsServer) {
|
||||||
|
// Create a server for handling websocket calls
|
||||||
|
const wss = new WebSocketServer({server: httpsServer});
|
||||||
|
|
||||||
|
wss.on('connection', (ws) => {
|
||||||
|
ws.on('message', (message) => {
|
||||||
|
// Broadcast any received message to all clients
|
||||||
|
console.log(`received: ${message}`);
|
||||||
|
wss.broadcast(message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
wss.broadcast = function(data) {
|
||||||
|
this.clients.forEach((client) => {
|
||||||
|
if(client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(data, {binary: false});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function printHelp() {
|
||||||
|
console.log(`Server running. Visit https://localhost:${HTTPS_PORT} in Firefox/Chrome/Safari.\n`);
|
||||||
|
console.log('Please note the following:');
|
||||||
|
console.log(' * Note the HTTPS in the URL; there is no HTTP -> HTTPS redirect.');
|
||||||
|
console.log(' * You\'ll need to accept the invalid TLS certificate as it is self-signed.');
|
||||||
|
console.log(' * Some browsers or OSs may not allow the webcam to be used by multiple pages at once. You may need to use two different browsers or machines.');
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue