Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility of dynamically creating p2p rooms in basic example #807

Merged
merged 2 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions extras/basic_example/basicServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,36 @@ app.use(function(req, res, next) {
N.API.init(config.nuve.superserviceID, config.nuve.superserviceKey, 'http://localhost:3000/');

var defaultRoom;
var defaultRoomName = 'basicExampleRoom';
const defaultRoomName = 'basicExampleRoom';

var getOrCreateRoom = function (roomName, callback) {
var getOrCreateRoom = function (name, type = 'erizo', callback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not callback = function(){} too?


if (roomName === defaultRoomName && defaultRoom) {
if (name === defaultRoomName && defaultRoom) {
callback(defaultRoom);
return;
}

N.API.getRooms(function (roomlist){
var theRoom = '';
var rooms = JSON.parse(roomlist);
for (var room in rooms) {
if (rooms[room].name === roomName &&
rooms[room].data &&
rooms[room].data.basicExampleRoom){
for (var room of rooms) {
if (room.name === name &&
room.data &&
room.data.basicExampleRoom){

theRoom = rooms[room]._id;
theRoom = room._id;
callback(theRoom);
return;
}
}
N.API.createRoom(roomName, function (roomID) {

let extra = {data: {basicExampleRoom:true}};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basicExampleRoom: true

if (type === 'p2p') extra.p2p = true;

N.API.createRoom(name, function (roomID) {
theRoom = roomID._id;
callback(theRoom);
}, function(){}, {data: {basicExampleRoom:true}});
}, function(){}, extra);
});
};

Expand Down Expand Up @@ -109,15 +113,16 @@ var deleteRoomsIfEmpty = function (theRooms, callback) {
};

var cleanExampleRooms = function (callback) {
console.log('Cleaning basic example rooms');
N.API.getRooms(function (roomlist) {
var rooms = JSON.parse(roomlist);
var roomsToCheck = [];
for (var room in rooms){
if (rooms[room].data &&
rooms[room].data.basicExampleRoom &&
rooms[room].name !== defaultRoomName){
for (var room of rooms){
if (room.data &&
room.data.basicExampleRoom &&
room.name !== defaultRoomName){

roomsToCheck.push(rooms[room]);
roomsToCheck.push(room);
}
}
deleteRoomsIfEmpty (roomsToCheck, function () {
Expand All @@ -142,21 +147,22 @@ app.get('/getUsers/:room', function(req, res) {


app.post('/createToken/', function(req, res) {
console.log(req.body);
var room = defaultRoomName;
if (req.body.room && !isNaN(req.body.room)) {
room = req.body.room;
}
console.log('Creating token. Request body: ',req.body);

let username = req.body.username;
let role = req.body.role;

let room = defaultRoomName, type;

var username = req.body.username,
role = req.body.role;
if (req.body.room && !isNaN(req.body.room)) room = req.body.room;
if (req.body.type) type = req.body.type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use something like:

let room = req.body.room || defaultRoomName;
let type = req.body.type;

or even:

let {body: {room = defaultRoomName, type}} = req;

😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to check that req.body.room is an integer (!isNaN(req.body.room)). So I'm not sure if just a line is recommended (because probably is possible :D)


getOrCreateRoom(room, function (roomId) {
getOrCreateRoom(room, type, function (roomId) {
N.API.createToken(roomId, username, role, function(token) {
console.log(token);
console.log('Token created', token);
res.send(token);
}, function(error) {
console.log(error);
console.log('Error creating token', error);
res.status(401).send('No Erizo Controller found');
});
});
Expand All @@ -175,7 +181,7 @@ app.use(function(req, res, next) {
});

cleanExampleRooms(function() {
getOrCreateRoom(defaultRoomName, function (roomId) {
getOrCreateRoom(defaultRoomName, undefined, function (roomId) {
defaultRoom = roomId;
app.listen(3001);
var server = https.createServer(options, app);
Expand Down
12 changes: 7 additions & 5 deletions extras/basic_example/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ window.onload = function () {
recording = false;
var screen = getParameterByName('screen');
var roomName = getParameterByName('room') || 'basicExampleRoom';
console.log('Selected Room', room);
var roomType = getParameterByName('type') || 'erizo';
console.log('Selected Room', roomName, 'of type', roomType);
var config = {audio: true,
video: true,
data: true,
Expand All @@ -64,11 +65,10 @@ window.onload = function () {
config.extensionId = 'okeephmleflklcdebijnponpabbmmgeo';
}
localStream = Erizo.Stream(config);
var createToken = function(userName, role, roomName, callback) {
var createToken = function(room_data, callback) {

var req = new XMLHttpRequest();
var url = serverUrl + 'createToken/';
var body = {username: userName, role: role, room:roomName};

req.onreadystatechange = function () {
if (req.readyState === 4) {
Expand All @@ -78,10 +78,12 @@ window.onload = function () {

req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(body));
req.send(JSON.stringify(room_data));
};

createToken('user', 'presenter', roomName, function (response) {
var room_data = {username: 'user', role: 'presenter', room: roomName, type: roomType};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer roomData


createToken(room_data, function (response) {
var token = response;
console.log(token);
room = Erizo.Room({token: token});
Expand Down