mirror of
https://github.com/ianramzy/decentralized-video-chat.git
synced 2024-11-23 18:49:21 +08:00
better auto-connect - just refresh to fix, + favicon
This commit is contained in:
parent
839539b5fa
commit
c4fe332552
5
index.js
5
index.js
@ -16,17 +16,16 @@ io.on('connection', function(socket){
|
|||||||
// When a client tries to join a room, only allow them if they are first or
|
// When a client tries to join a room, only allow them if they are first or
|
||||||
// second in the room. Otherwise it is full.
|
// second in the room. Otherwise it is full.
|
||||||
socket.on('join', function(room){
|
socket.on('join', function(room){
|
||||||
|
console.log('A client joined room:' + room);
|
||||||
console.log('A client joined')
|
|
||||||
var clients = io.sockets.adapter.rooms[room];
|
var clients = io.sockets.adapter.rooms[room];
|
||||||
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
|
var numClients = typeof clients !== 'undefined' ? clients.length : 0;
|
||||||
if(numClients === 0){
|
if(numClients === 0){
|
||||||
socket.join(room);
|
socket.join(room);
|
||||||
socket.emit('firstin', room);
|
|
||||||
}else if(numClients === 1){
|
}else if(numClients === 1){
|
||||||
socket.join(room);
|
socket.join(room);
|
||||||
// When the client is second to join the room, both clients are ready.
|
// When the client is second to join the room, both clients are ready.
|
||||||
console.log('Broadcasting ready message')
|
console.log('Broadcasting ready message')
|
||||||
|
socket.broadcast.emit('willInitiateCall', room);
|
||||||
socket.emit('ready', room);
|
socket.emit('ready', room);
|
||||||
socket.broadcast.emit('ready', room);
|
socket.broadcast.emit('ready', room);
|
||||||
}else{
|
}else{
|
||||||
|
@ -10,9 +10,7 @@ if (!location.hash) {
|
|||||||
const roomHash = location.hash.substring(1);
|
const roomHash = location.hash.substring(1);
|
||||||
|
|
||||||
function logIt(message, error) {
|
function logIt(message, error) {
|
||||||
// Print on console
|
// console.log(message);
|
||||||
console.log(message);
|
|
||||||
|
|
||||||
// Add to logs on page
|
// Add to logs on page
|
||||||
// let logs = document.getElementById('logs');
|
// let logs = document.getElementById('logs');
|
||||||
// let tmp = document.createElement('P');
|
// let tmp = document.createElement('P');
|
||||||
@ -26,7 +24,7 @@ function logIt(message, error) {
|
|||||||
// Create an object to save various objects to without polluting the global namespace.
|
// Create an object to save various objects to without polluting the global namespace.
|
||||||
var VideoChat = {
|
var VideoChat = {
|
||||||
connected: false,
|
connected: false,
|
||||||
firstInChat : false,
|
willInitiateCall : false,
|
||||||
localICECandidates: [],
|
localICECandidates: [],
|
||||||
// Initialise our connection to the WebSocket.
|
// Initialise our connection to the WebSocket.
|
||||||
socket: io(),
|
socket: io(),
|
||||||
@ -36,14 +34,12 @@ var VideoChat = {
|
|||||||
// screenButton: document.getElementById('get-screen'),
|
// screenButton: document.getElementById('get-screen'),
|
||||||
// callButton: document.getElementById('call'),
|
// callButton: document.getElementById('call'),
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Call to getUserMedia (provided by adapter.js for cross browser compatibility)
|
// Call to getUserMedia (provided by adapter.js for cross browser compatibility)
|
||||||
// asking for access to both the video and audio streams. If the request is
|
// asking for access to both the video and audio streams. If the request is
|
||||||
// accepted callback to the onMediaStream function, otherwise callback to the
|
// accepted callback to the onMediaStream function, otherwise callback to the
|
||||||
// noMediaStream function.
|
// noMediaStream function.
|
||||||
requestMediaStream: function (event) {
|
requestMediaStream: function (event) {
|
||||||
|
console.log("requestMediaStream");
|
||||||
navigator.mediaDevices
|
navigator.mediaDevices
|
||||||
.getUserMedia({video: true, audio: true})
|
.getUserMedia({video: true, audio: true})
|
||||||
.then(stream => {
|
.then(stream => {
|
||||||
@ -67,17 +63,19 @@ var VideoChat = {
|
|||||||
|
|
||||||
// The onMediaStream function receives the media stream as an argument.
|
// The onMediaStream function receives the media stream as an argument.
|
||||||
onMediaStream: function (stream) {
|
onMediaStream: function (stream) {
|
||||||
VideoChat.localVideo.volume = 0; // Turn the volume down to 0 to avoid echoes.
|
console.log("onMediaStream");
|
||||||
|
// VideoChat.localVideo.volume = 0; // Turn the volume down to 0 to avoid echoes.
|
||||||
VideoChat.localStream = stream;
|
VideoChat.localStream = stream;
|
||||||
// VideoChat.videoButton.setAttribute('disabled', 'disabled');
|
// VideoChat.videoButton.setAttribute('disabled', 'disabled');
|
||||||
// VideoChat.screenButton.setAttribute('disabled', 'disabled');
|
// VideoChat.screenButton.setAttribute('disabled', 'disabled');
|
||||||
// Add the stream as video's srcObject.
|
// Add the stream as video's srcObject.
|
||||||
VideoChat.localVideo.srcObject = stream;
|
VideoChat.localVideo.srcObject = stream;
|
||||||
// Now we're ready to join the chat room.
|
// Now we're ready to join the chat room.
|
||||||
VideoChat.socket.emit('join', 'test');
|
VideoChat.socket.emit('join', roomHash);
|
||||||
|
// VideoChat.socket.emit('join', 'test'); default
|
||||||
VideoChat.socket.on('offer', VideoChat.onOffer);
|
VideoChat.socket.on('offer', VideoChat.onOffer);
|
||||||
VideoChat.socket.on('ready', VideoChat.readyToCall);
|
VideoChat.socket.on('ready', VideoChat.readyToCall);
|
||||||
VideoChat.socket.on('firstin', () => VideoChat.firstInChat = true);
|
VideoChat.socket.on('willInitiateCall', () => VideoChat.willInitiateCall = true);
|
||||||
},
|
},
|
||||||
|
|
||||||
// There's not much to do in this demo if there is no media stream. So let's just stop.
|
// There's not much to do in this demo if there is no media stream. So let's just stop.
|
||||||
@ -87,16 +85,16 @@ var VideoChat = {
|
|||||||
|
|
||||||
// When we are ready to call, enable the Call button.
|
// When we are ready to call, enable the Call button.
|
||||||
readyToCall: function (event) {
|
readyToCall: function (event) {
|
||||||
|
console.log("readyToCall");
|
||||||
// VideoChat.callButton.removeAttribute('disabled');
|
// VideoChat.callButton.removeAttribute('disabled');
|
||||||
if (VideoChat.firstInChat) {
|
if (VideoChat.willInitiateCall) {
|
||||||
// alert("firstin is calling")
|
|
||||||
VideoChat.startCall()
|
VideoChat.startCall()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Set up a callback to run when we have the ephemeral token to use Twilio's
|
// Set up a callback to run when we have the ephemeral token to use Twilio's TURN server.
|
||||||
// TURN server.
|
|
||||||
startCall: function (event) {
|
startCall: function (event) {
|
||||||
|
console.log("startCall");
|
||||||
logIt('>>> Sending token request...');
|
logIt('>>> Sending token request...');
|
||||||
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createOffer));
|
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createOffer));
|
||||||
VideoChat.socket.emit('token');
|
VideoChat.socket.emit('token');
|
||||||
@ -105,6 +103,7 @@ var VideoChat = {
|
|||||||
|
|
||||||
// When we receive the ephemeral token back from the server.
|
// When we receive the ephemeral token back from the server.
|
||||||
onToken: function (callback) {
|
onToken: function (callback) {
|
||||||
|
console.log("onToken");
|
||||||
return function (token) {
|
return function (token) {
|
||||||
logIt('<<< Received token');
|
logIt('<<< Received token');
|
||||||
// Set up a new RTCPeerConnection using the token's iceServers.
|
// Set up a new RTCPeerConnection using the token's iceServers.
|
||||||
@ -126,6 +125,7 @@ var VideoChat = {
|
|||||||
// When the peerConnection generates an ice candidate, send it over the socket
|
// When the peerConnection generates an ice candidate, send it over the socket
|
||||||
// to the peer.
|
// to the peer.
|
||||||
onIceCandidate: function (event) {
|
onIceCandidate: function (event) {
|
||||||
|
console.log("onIceCandidate");
|
||||||
if (event.candidate) {
|
if (event.candidate) {
|
||||||
logIt(`<<< Received local ICE candidate from STUN/TURN server (${event.candidate.address})`);
|
logIt(`<<< Received local ICE candidate from STUN/TURN server (${event.candidate.address})`);
|
||||||
if (VideoChat.connected) {
|
if (VideoChat.connected) {
|
||||||
@ -144,6 +144,7 @@ var VideoChat = {
|
|||||||
// When receiving a candidate over the socket, turn it back into a real
|
// When receiving a candidate over the socket, turn it back into a real
|
||||||
// RTCIceCandidate and add it to the peerConnection.
|
// RTCIceCandidate and add it to the peerConnection.
|
||||||
onCandidate: function (candidate) {
|
onCandidate: function (candidate) {
|
||||||
|
console.log("onCandidate");
|
||||||
rtcCandidate = new RTCIceCandidate(JSON.parse(candidate));
|
rtcCandidate = new RTCIceCandidate(JSON.parse(candidate));
|
||||||
logIt(`<<< Received remote ICE candidate (${rtcCandidate.address} - ${rtcCandidate.relatedAddress})`);
|
logIt(`<<< Received remote ICE candidate (${rtcCandidate.address} - ${rtcCandidate.relatedAddress})`);
|
||||||
VideoChat.peerConnection.addIceCandidate(rtcCandidate);
|
VideoChat.peerConnection.addIceCandidate(rtcCandidate);
|
||||||
@ -151,6 +152,7 @@ var VideoChat = {
|
|||||||
|
|
||||||
// Create an offer that contains the media capabilities of the browser.
|
// Create an offer that contains the media capabilities of the browser.
|
||||||
createOffer: function () {
|
createOffer: function () {
|
||||||
|
console.log("createOffer");
|
||||||
logIt('>>> Creating offer...');
|
logIt('>>> Creating offer...');
|
||||||
VideoChat.peerConnection.createOffer(
|
VideoChat.peerConnection.createOffer(
|
||||||
function (offer) {
|
function (offer) {
|
||||||
@ -173,6 +175,7 @@ var VideoChat = {
|
|||||||
// description to the peerConnection object. Then the answer is created in the
|
// description to the peerConnection object. Then the answer is created in the
|
||||||
// same manner as the offer and sent over the socket.
|
// same manner as the offer and sent over the socket.
|
||||||
createAnswer: function (offer) {
|
createAnswer: function (offer) {
|
||||||
|
console.log("createAnswer");
|
||||||
return function () {
|
return function () {
|
||||||
logIt('>>> Creating answer...');
|
logIt('>>> Creating answer...');
|
||||||
VideoChat.connected = true;
|
VideoChat.connected = true;
|
||||||
@ -194,6 +197,7 @@ var VideoChat = {
|
|||||||
// When a browser receives an offer, set up a callback to be run when the
|
// When a browser receives an offer, set up a callback to be run when the
|
||||||
// ephemeral token is returned from Twilio.
|
// ephemeral token is returned from Twilio.
|
||||||
onOffer: function (offer) {
|
onOffer: function (offer) {
|
||||||
|
console.log("onOffer");
|
||||||
logIt('<<< Received offer');
|
logIt('<<< Received offer');
|
||||||
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createAnswer(offer)));
|
VideoChat.socket.on('token', VideoChat.onToken(VideoChat.createAnswer(offer)));
|
||||||
VideoChat.socket.emit('token');
|
VideoChat.socket.emit('token');
|
||||||
@ -202,6 +206,7 @@ var VideoChat = {
|
|||||||
// When an answer is received, add it to the peerConnection as the remote
|
// When an answer is received, add it to the peerConnection as the remote
|
||||||
// description.
|
// description.
|
||||||
onAnswer: function (answer) {
|
onAnswer: function (answer) {
|
||||||
|
console.log("onAnswer");
|
||||||
logIt('<<< Received answer');
|
logIt('<<< Received answer');
|
||||||
var rtcAnswer = new RTCSessionDescription(JSON.parse(answer));
|
var rtcAnswer = new RTCSessionDescription(JSON.parse(answer));
|
||||||
VideoChat.peerConnection.setRemoteDescription(rtcAnswer);
|
VideoChat.peerConnection.setRemoteDescription(rtcAnswer);
|
||||||
@ -219,6 +224,7 @@ var VideoChat = {
|
|||||||
// When the peerConnection receives the actual media stream from the other
|
// When the peerConnection receives the actual media stream from the other
|
||||||
// browser, add it to the other video element on the page.
|
// browser, add it to the other video element on the page.
|
||||||
onAddStream: function (event) {
|
onAddStream: function (event) {
|
||||||
|
console.log("onAddStream");
|
||||||
logIt('<<< Received new stream from remote. Adding it...');
|
logIt('<<< Received new stream from remote. Adding it...');
|
||||||
VideoChat.remoteVideo.srcObject = event.stream;
|
VideoChat.remoteVideo.srcObject = event.stream;
|
||||||
}
|
}
|
||||||
@ -229,5 +235,5 @@ var VideoChat = {
|
|||||||
|
|
||||||
// auto get media
|
// auto get media
|
||||||
// VideoChat.requestScreenStream();
|
// VideoChat.requestScreenStream();
|
||||||
VideoChat.requestMediaStream()
|
VideoChat.requestMediaStream();
|
||||||
|
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<title>Video Chat</title>
|
<title>Decentralized Video Chat</title>
|
||||||
|
<link rel="shortcut icon" href="https://img.icons8.com/pastel-glyph/2x/worldwide-location.png">
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
background: #ececec;
|
background: #ececec;
|
||||||
|
Loading…
Reference in New Issue
Block a user