update syntax to es6
parent
93d28dd9c0
commit
cd7177d2d6
|
|
@ -1,11 +1,11 @@
|
|||
var localVideo;
|
||||
var localStream;
|
||||
var remoteVideo;
|
||||
var peerConnection;
|
||||
var uuid;
|
||||
var serverConnection;
|
||||
let localStream;
|
||||
let localVideo;
|
||||
let peerConnection;
|
||||
let remoteVideo;
|
||||
let serverConnection;
|
||||
let uuid;
|
||||
|
||||
var peerConnectionConfig = {
|
||||
const peerConnectionConfig = {
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
|
|
@ -18,10 +18,10 @@ function pageReady() {
|
|||
localVideo = document.getElementById('localVideo');
|
||||
remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
||||
serverConnection = new WebSocket(`wss://${window.location.hostname}:8443`);
|
||||
serverConnection.onmessage = gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
const constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
};
|
||||
|
|
@ -52,13 +52,13 @@ function start(isCaller) {
|
|||
function gotMessageFromServer(message) {
|
||||
if(!peerConnection) start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
const signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == uuid) return;
|
||||
|
||||
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
|
||||
if(signal.sdp.type == 'offer') {
|
||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||
|
|
@ -78,7 +78,7 @@ function gotIceCandidate(event) {
|
|||
function createdDescription(description) {
|
||||
console.log('got description');
|
||||
|
||||
peerConnection.setLocalDescription(description).then(function() {
|
||||
peerConnection.setLocalDescription(description).then(() => {
|
||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
||||
}).catch(errorHandler);
|
||||
}
|
||||
|
|
@ -99,5 +99,5 @@ function createUUID() {
|
|||
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,19 +5,24 @@ const https = require('https');
|
|||
const WebSocket = require('ws');
|
||||
const WebSocketServer = WebSocket.Server;
|
||||
|
||||
// Yes, TLS is required
|
||||
// Yes, TLS is required for WebRTC
|
||||
const serverConfig = {
|
||||
key: fs.readFileSync('key.pem'),
|
||||
cert: fs.readFileSync('cert.pem'),
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
function main() {
|
||||
const httpsServer = startHttpsServer(serverConfig);
|
||||
startWebSocketServer(httpsServer);
|
||||
printHelp();
|
||||
}
|
||||
|
||||
// Create a server for the client html page
|
||||
const handleRequest = function(request, response) {
|
||||
// Render the single client html file for any request the HTTP server receives
|
||||
console.log('request received: ' + request.url);
|
||||
function startHttpsServer(serverConfig) {
|
||||
// Handle incoming requests from the client
|
||||
const handleRequest = (request, response) => {
|
||||
console.log(`request received: ${request.url}`);
|
||||
|
||||
// This server only serves two files: The HTML page and the client JS file
|
||||
if(request.url === '/') {
|
||||
response.writeHead(200, {'Content-Type': 'text/html'});
|
||||
response.end(fs.readFileSync('client/index.html'));
|
||||
|
|
@ -25,35 +30,40 @@ const handleRequest = function(request, response) {
|
|||
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');
|
||||
const httpsServer = https.createServer(serverConfig, handleRequest);
|
||||
httpsServer.listen(HTTPS_PORT, '0.0.0.0');
|
||||
return httpsServer;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
function startWebSocketServer(httpsServer) {
|
||||
// Create a server for handling websocket calls
|
||||
const wss = new WebSocketServer({server: httpsServer});
|
||||
|
||||
// Create a server for handling websocket calls
|
||||
const wss = new WebSocketServer({server: httpsServer});
|
||||
|
||||
wss.on('connection', function(ws) {
|
||||
ws.on('message', function(message) {
|
||||
wss.on('connection', (ws) => {
|
||||
ws.on('message', (message) => {
|
||||
// Broadcast any received message to all clients
|
||||
console.log('received: %s', message);
|
||||
console.log(`received: ${message}`);
|
||||
wss.broadcast(message);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
wss.broadcast = function(data) {
|
||||
this.clients.forEach(function(client) {
|
||||
wss.broadcast = function(data) {
|
||||
this.clients.forEach((client) => {
|
||||
if(client.readyState === WebSocket.OPEN) {
|
||||
client.send(data);
|
||||
client.send(data, {binary: false});
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
console.log('Server running. Visit https://localhost:' + HTTPS_PORT + ' in Firefox/Chrome.\n\n\
|
||||
Some important notes:\n\
|
||||
* Note the HTTPS; there is no HTTP -> HTTPS redirect.\n\
|
||||
* 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 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