change indentation to 2 spaces
parent
9a06994998
commit
9147238b49
|
|
@ -1,20 +1,20 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
|
||||
<script src="webrtc.js"></script>
|
||||
</head>
|
||||
<head>
|
||||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
|
||||
<script src="webrtc.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<video id="localVideo" autoplay muted style="width:40%;"></video>
|
||||
<video id="remoteVideo" autoplay style="width:40%;"></video>
|
||||
<body>
|
||||
<video id="localVideo" autoplay muted style="width:40%;"></video>
|
||||
<video id="remoteVideo" autoplay style="width:40%;"></video>
|
||||
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<input type="button" id="start" onclick="start(true)" value="Start Video"></input>
|
||||
<input type="button" id="start" onclick="start(true)" value="Start Video"></input>
|
||||
|
||||
<script type="text/javascript">
|
||||
pageReady();
|
||||
</script>
|
||||
</body>
|
||||
<script type="text/javascript">
|
||||
pageReady();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
102
client/webrtc.js
102
client/webrtc.js
|
|
@ -6,90 +6,90 @@ var uuid;
|
|||
var serverConnection;
|
||||
|
||||
var peerConnectionConfig = {
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.services.mozilla.com'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
]
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.services.mozilla.com'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
]
|
||||
};
|
||||
|
||||
function pageReady() {
|
||||
uuid = createUUID();
|
||||
uuid = createUUID();
|
||||
|
||||
localVideo = document.getElementById('localVideo');
|
||||
remoteVideo = document.getElementById('remoteVideo');
|
||||
localVideo = document.getElementById('localVideo');
|
||||
remoteVideo = document.getElementById('remoteVideo');
|
||||
|
||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
||||
serverConnection.onmessage = gotMessageFromServer;
|
||||
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
|
||||
serverConnection.onmessage = gotMessageFromServer;
|
||||
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
};
|
||||
var constraints = {
|
||||
video: true,
|
||||
audio: true,
|
||||
};
|
||||
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
if(navigator.mediaDevices.getUserMedia) {
|
||||
navigator.mediaDevices.getUserMedia(constraints).then(getUserMediaSuccess).catch(errorHandler);
|
||||
} else {
|
||||
alert('Your browser does not support getUserMedia API');
|
||||
}
|
||||
}
|
||||
|
||||
function getUserMediaSuccess(stream) {
|
||||
localStream = stream;
|
||||
localVideo.srcObject = stream;
|
||||
localStream = stream;
|
||||
localVideo.srcObject = stream;
|
||||
}
|
||||
|
||||
function start(isCaller) {
|
||||
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||
peerConnection.onicecandidate = gotIceCandidate;
|
||||
peerConnection.onaddstream = gotRemoteStream;
|
||||
peerConnection.addStream(localStream);
|
||||
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
||||
peerConnection.onicecandidate = gotIceCandidate;
|
||||
peerConnection.onaddstream = gotRemoteStream;
|
||||
peerConnection.addStream(localStream);
|
||||
|
||||
if(isCaller) {
|
||||
peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
if(isCaller) {
|
||||
peerConnection.createOffer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotMessageFromServer(message) {
|
||||
if(!peerConnection) start(false);
|
||||
if(!peerConnection) start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == uuid) return;
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == uuid) return;
|
||||
|
||||
if(signal.sdp) {
|
||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
if(signal.sdp.type == 'offer') {
|
||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
}).catch(errorHandler);
|
||||
} else if(signal.ice) {
|
||||
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
||||
}
|
||||
if(signal.sdp) {
|
||||
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
if(signal.sdp.type == 'offer') {
|
||||
peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
|
||||
}
|
||||
}).catch(errorHandler);
|
||||
} else if(signal.ice) {
|
||||
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
|
||||
}
|
||||
}
|
||||
|
||||
function gotIceCandidate(event) {
|
||||
if(event.candidate != null) {
|
||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
||||
}
|
||||
if(event.candidate != null) {
|
||||
serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': uuid}));
|
||||
}
|
||||
}
|
||||
|
||||
function createdDescription(description) {
|
||||
console.log('got description');
|
||||
console.log('got description');
|
||||
|
||||
peerConnection.setLocalDescription(description).then(function() {
|
||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
||||
}).catch(errorHandler);
|
||||
peerConnection.setLocalDescription(description).then(function() {
|
||||
serverConnection.send(JSON.stringify({'sdp': peerConnection.localDescription, 'uuid': uuid}));
|
||||
}).catch(errorHandler);
|
||||
}
|
||||
|
||||
function gotRemoteStream(event) {
|
||||
console.log('got remote stream');
|
||||
remoteVideo.srcObject = event.stream;
|
||||
console.log('got remote stream');
|
||||
remoteVideo.srcObject = event.stream;
|
||||
}
|
||||
|
||||
function errorHandler(error) {
|
||||
console.log(error);
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// Taken from http://stackoverflow.com/a/105074/515584
|
||||
|
|
|
|||
|
|
@ -7,24 +7,24 @@ const WebSocketServer = WebSocket.Server;
|
|||
|
||||
// Yes, TLS is required
|
||||
const serverConfig = {
|
||||
key: fs.readFileSync('key.pem'),
|
||||
cert: fs.readFileSync('cert.pem'),
|
||||
key: fs.readFileSync('key.pem'),
|
||||
cert: fs.readFileSync('cert.pem'),
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
// 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);
|
||||
// Render the single client html file for any request the HTTP server receives
|
||||
console.log('request received: ' + request.url);
|
||||
|
||||
if(request.url === '/') {
|
||||
response.writeHead(200, {'Content-Type': 'text/html'});
|
||||
response.end(fs.readFileSync('client/index.html'));
|
||||
} else if(request.url === '/webrtc.js') {
|
||||
response.writeHead(200, {'Content-Type': 'application/javascript'});
|
||||
response.end(fs.readFileSync('client/webrtc.js'));
|
||||
}
|
||||
if(request.url === '/') {
|
||||
response.writeHead(200, {'Content-Type': 'text/html'});
|
||||
response.end(fs.readFileSync('client/index.html'));
|
||||
} else if(request.url === '/webrtc.js') {
|
||||
response.writeHead(200, {'Content-Type': 'application/javascript'});
|
||||
response.end(fs.readFileSync('client/webrtc.js'));
|
||||
}
|
||||
};
|
||||
|
||||
const httpsServer = https.createServer(serverConfig, handleRequest);
|
||||
|
|
@ -36,19 +36,19 @@ httpsServer.listen(HTTPS_PORT, '0.0.0.0');
|
|||
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);
|
||||
});
|
||||
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);
|
||||
}
|
||||
});
|
||||
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\
|
||||
|
|
|
|||
Loading…
Reference in New Issue