-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
||
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}}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (type === 'p2p') extra.p2p = true; | ||
|
||
N.API.createRoom(name, function (roomID) { | ||
theRoom = roomID._id; | ||
callback(theRoom); | ||
}, function(){}, {data: {basicExampleRoom:true}}); | ||
}, function(){}, extra); | ||
}); | ||
}; | ||
|
||
|
@@ -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 () { | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use something like:
or even:
😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to check that req.body.room is an integer ( |
||
|
||
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'); | ||
}); | ||
}); | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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) { | ||
|
@@ -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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer |
||
|
||
createToken(room_data, function (response) { | ||
var token = response; | ||
console.log(token); | ||
room = Erizo.Room({token: token}); | ||
|
There was a problem hiding this comment.
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?