working emit/broadcase to just room

This commit is contained in:
ian ramzy 2020-03-22 12:47:35 -04:00
parent c4fe332552
commit 72e8c0a865
2 changed files with 71 additions and 57 deletions

115
index.js
View File

@ -2,8 +2,8 @@ require('dotenv').config();
// Twilio init // Twilio init
var twilio = require('twilio')( var twilio = require('twilio')(
process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN process.env.TWILIO_AUTH_TOKEN
); );
var express = require('express'); var express = require('express');
var app = express(); var app = express();
@ -11,63 +11,70 @@ var http = require('http').createServer(app);
var io = require('socket.io')(http); var io = require('socket.io')(http);
app.use(express.static('public')); app.use(express.static('public'));
function log(msg, room){
console.log(room + " - " + msg)
}
// When a socket connects, set up the specific listeners we will use. // When a socket connects, set up the specific listeners we will use.
io.on('connection', function(socket){ 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); log('A client joined the room', room);
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);
}else if(numClients === 1){ // socket.emit("roomtest", room).to(room)
socket.join(room); } else if (numClients === 1) {
// When the client is second to join the room, both clients are ready. socket.join(room);
console.log('Broadcasting ready message') // socket.emit("roomtest", room).to(room)
socket.broadcast.emit('willInitiateCall', room); // When the client is second to join the room, both clients are ready.
socket.emit('ready', room); log('Broadcasting ready message', room);
socket.broadcast.emit('ready', room); socket.broadcast.to(room).emit('willInitiateCall', room);
}else{ socket.emit('ready', room).to(room);
console.log("room full"); socket.broadcast.to(room).emit('ready', room);
socket.emit('full', room); } else {
} log("room already full", room);
}); socket.emit('full', room);
}
// When receiving the token message, use the Twilio REST API to request an
// token to get ephemeral credentials to use the TURN server.
socket.on('token', function(){
console.log('Received token request');
twilio.tokens.create(function(err, response){
if(err){
console.log(err);
}else{
// Return the token to the browser.
console.log('Token generated. Returning it to the client');
socket.emit('token', response);
}
}); });
});
// Relay candidate messages // When receiving the token message, use the Twilio REST API to request an
socket.on('candidate', function(candidate){ // token to get ephemeral credentials to use the TURN server.
console.log('Received candidate. Broadcasting...'); socket.on('token', function () {
socket.broadcast.emit('candidate', candidate); console.log('Received token request');
}); twilio.tokens.create(function (err, response) {
if (err) {
console.log(err);
} else {
// Return the token to the browser.
console.log('Token generated. Returning it to the client');
socket.emit('token', response);
}
});
});
// Relay offers // Relay candidate messages
socket.on('offer', function(offer){ socket.on('candidate', function (candidate) {
console.log('Received offer. Broadcasting...'); console.log('Received candidate. Broadcasting...');
socket.broadcast.emit('offer', offer); socket.broadcast.emit('candidate', candidate);
}); });
// Relay answers // Relay offers
socket.on('answer', function(answer){ socket.on('offer', function (offer) {
console.log('Received answer. Broadcasting...'); console.log('Received offer. Broadcasting...');
socket.broadcast.emit('answer', answer); socket.broadcast.emit('offer', offer);
}); });
// Relay answers
socket.on('answer', function (answer) {
console.log('Received answer. Broadcasting...');
socket.broadcast.emit('answer', answer);
});
}); });
http.listen(3000, function() { http.listen(3000, function () {
console.log("http://localhost:3000"); console.log("http://localhost:3000");
}); });

View File

@ -4,8 +4,7 @@ if (!location.hash) {
var nouns = ["dog", "bat", "wrench", "apple", "pear", "ghost", "cat", "wolf", "squid", "goat", "snail", "hat", "sock", "plum", "bear", "snake", "turtle", "horse","spoon","fork","spider","tree","chair","table"] var nouns = ["dog", "bat", "wrench", "apple", "pear", "ghost", "cat", "wolf", "squid", "goat", "snail", "hat", "sock", "plum", "bear", "snake", "turtle", "horse","spoon","fork","spider","tree","chair","table"]
var adjective = adjectives[Math.floor(Math.random() * adjectives.length)] var adjective = adjectives[Math.floor(Math.random() * adjectives.length)]
var noun = nouns[Math.floor(Math.random() * nouns.length)] var noun = nouns[Math.floor(Math.random() * nouns.length)]
// var num = Math.floor(Math.ran1dom() * 100) location.hash = adjective + noun
location.hash = adjective + "-" + noun
} }
const roomHash = location.hash.substring(1); const roomHash = location.hash.substring(1);
@ -72,7 +71,9 @@ var VideoChat = {
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', roomHash); VideoChat.socket.emit('join', roomHash);
// VideoChat.socket.emit('join', 'test'); default // VideoChat.socket.on('roomtest', (passedRoom) => alert("youre in room: " + passedRoom));
VideoChat.socket.on('temp', () => alert("temp called"));
VideoChat.socket.on('full', VideoChat.chatRoomFull);
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('willInitiateCall', () => VideoChat.willInitiateCall = true); VideoChat.socket.on('willInitiateCall', () => VideoChat.willInitiateCall = true);
@ -83,6 +84,12 @@ var VideoChat = {
logIt('No media stream for us.'); logIt('No media stream for us.');
}, },
chatRoomFull: function(){
alert("Chat room is full. Check to make sure you don't have multiple open tabs");
// VideoChat.socket.disconnect()
// todo handle this better
},
// 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"); console.log("readyToCall");