openvidu-recording-node: update to openvidu-node-client 2.3.0

This commit is contained in:
pabloFuente 2018-07-22 22:28:09 +02:00
parent ac52d0c3cb
commit ec8be625e6
4 changed files with 293 additions and 48 deletions

View File

@ -41,6 +41,18 @@ function joinSession() {
});
});
session.on('sessionDisconnected', (event) => {
if (event.reason !== 'disconnect') {
removeUser();
}
if (event.reason !== 'sessionClosedByServer') {
session = null;
numVideos = 0;
$('#join').show();
$('#session').hide();
}
});
// --- 4) Connect to the session passing the retrieved token and some more data from
// the client (in this case a JSON with the nickname chosen by the user) ---
@ -97,13 +109,8 @@ function joinSession() {
function leaveSession() {
// --- 9) Leave the session by calling 'disconnect' method over the Session object ---
session.disconnect();
session = null;
numVideos = 0;
$('#join').show();
$('#session').hide();
}
/* OPENVIDU METHODS */
@ -118,7 +125,7 @@ function getToken(callback) {
httpRequest(
'POST',
'api/get-token',
{sessionName: sessionName},
{ sessionName: sessionName },
'Request of TOKEN gone WRONG:',
(response) => {
token = response[0]; // Get token from response
@ -132,14 +139,76 @@ function removeUser() {
httpRequest(
'POST',
'api/remove-user',
{sessionName: sessionName, token: token},
'User couldn\'t be removed from session',
{ sessionName: sessionName, token: token },
'User couldn\'t be removed from session',
(response) => {
console.warn("You have been removed from session " + sessionName);
}
);
}
function closeSession() {
httpRequest(
'DELETE',
'api/close-session',
{ sessionName: sessionName },
'Session couldn\'t be closed',
(response) => {
console.warn("Session " + sessionName + " has been closed");
}
);
}
function fetchInfo() {
httpRequest(
'POST',
'api/fetch-info',
{ sessionName: sessionName },
'Session couldn\'t be fetched',
(response) => {
console.warn("Session info has been fetched");
$('#text-area').text(JSON.stringify(response, null, "\t"));
}
);
}
function fetchAll() {
httpRequest(
'GET',
'api/fetch-all',
{},
'All session info couldn\'t be fetched',
(response) => {
console.warn("All session info has been fetched");
$('#text-area').text(JSON.stringify(response, null, "\t"));
}
);
}
function forceDisconnect() {
httpRequest(
'DELETE',
'api/force-disconnect',
{ sessionName: sessionName, connectionId: document.getElementById('forceValue').value },
'Connection couldn\'t be closed',
(response) => {
console.warn("Connection has been closed");
}
);
}
function forceUnpublish() {
httpRequest(
'DELETE',
'api/force-unpublish',
{ sessionName: sessionName, streamId: document.getElementById('forceValue').value },
'Stream couldn\'t be closed',
(response) => {
console.warn("Stream has been closed");
}
);
}
function httpRequest(method, url, body, errorMsg, callback) {
$('#text-area').text('');
var http = new XMLHttpRequest();
@ -154,7 +223,7 @@ function httpRequest(method, url, body, errorMsg, callback) {
try {
callback(JSON.parse(http.responseText));
} catch (e) {
callback();
callback(e);
}
} else {
console.warn(errorMsg + ' (' + http.status + ')');
@ -165,28 +234,28 @@ function httpRequest(method, url, body, errorMsg, callback) {
}
}
var recordingId;
function startRecording() {
httpRequest(
'POST',
'api/recording/start',
{session: session.sessionId},
'Start recording WRONG',
{ session: session.sessionId },
'Start recording WRONG',
(response) => {
console.log(response);
recordingId = response.id;
document.getElementById('forceRecordingId').value = response.id;
checkBtnsRecordings();
$('#text-area').text(JSON.stringify(response, null, "\t"));
}
);
}
function stopRecording() {
var forceRecordingId = document.getElementById('forceRecordingId').value;
httpRequest(
'POST',
'api/recording/stop',
{recording: recordingId},
'Stop recording WRONG',
{ recording: forceRecordingId },
'Stop recording WRONG',
(response) => {
console.log(response);
$('#text-area').text(JSON.stringify(response, null, "\t"));
@ -195,10 +264,11 @@ function stopRecording() {
}
function deleteRecording() {
var forceRecordingId = document.getElementById('forceRecordingId').value;
httpRequest(
'DELETE',
'api/recording/delete',
{recording: recordingId},
{ recording: forceRecordingId },
'Delete recording WRONG',
() => {
console.log("DELETE ok");
@ -208,9 +278,10 @@ function deleteRecording() {
}
function getRecording() {
var forceRecordingId = document.getElementById('forceRecordingId').value;
httpRequest(
'GET',
'api/recording/get/' + recordingId,
'api/recording/get/' + forceRecordingId,
{},
'Get recording WRONG',
(response) => {
@ -265,4 +336,26 @@ function updateNumVideos(i) {
}
}
function checkBtnsForce() {
if (document.getElementById("forceValue").value === "") {
document.getElementById('buttonForceUnpublish').disabled = true;
document.getElementById('buttonForceDisconnect').disabled = true;
} else {
document.getElementById('buttonForceUnpublish').disabled = false;
document.getElementById('buttonForceDisconnect').disabled = false;
}
}
function checkBtnsRecordings() {
if (document.getElementById("forceRecordingId").value === "") {
document.getElementById('buttonGetRecording').disabled = true;
document.getElementById('buttonStopRecording').disabled = true;
document.getElementById('buttonDeleteRecording').disabled = true;
} else {
document.getElementById('buttonGetRecording').disabled = false;
document.getElementById('buttonStopRecording').disabled = false;
document.getElementById('buttonDeleteRecording').disabled = false;
}
}
/* APPLICATION BROWSER METHODS */

View File

@ -8,11 +8,11 @@
<!-- Bootstrap -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
crossorigin="anonymous"></script>
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Bootstrap -->
@ -36,7 +36,7 @@
<a class="navbar-brand" href="/">
<img class="demo-logo" src="images/openvidu_vert_white_bg_trans_cropped.png" /> Recording Node</a>
<a class="navbar-brand nav-icon" href="https://github.com/OpenVidu/openvidu-tutorials/tree/master/openvidu-recording-node"
title="GitHub Repository" target="_blank">
title="GitHub Repository" target="_blank">
<i class="fa fa-github" aria-hidden="true"></i>
</a>
<a class="navbar-brand nav-icon" href="http://www.openvidu.io/docs/tutorials/openvidu-js-node/" title="Documentation" target="_blank">
@ -69,16 +69,25 @@
<div id="session" style="display: none">
<div id="session-header">
<h1 id="session-title"></h1>
<input class="btn btn-large btn-danger" type="button" id="buttonLeaveSession" onmouseup="removeUser(); leaveSession()" value="Leave session">
<input class="btn btn-sm btn-danger" type="button" id="buttonCloseSession" onmouseup="closeSession()" value="Close session">
<input class="btn btn-sm btn-danger" type="button" id="buttonLeaveSession" onmouseup="removeUser(); leaveSession()" value="Leave session">
<input class="btn btn-sm" type="button" id="buttonForceUnpublish" onmouseup="forceUnpublish()" value="Force unpublish" disabled>
<input class="btn btn-sm" type="button" id="buttonForceDisconnect" onmouseup="forceDisconnect()" value="Force disconnect"
disabled>
<input class="form-control" id="forceValue" type="text" onkeyup="checkBtnsForce()">
<input class="btn btn-sm" type="button" id="buttonFetchInfo" onmouseup="fetchInfo()" value="Fetch info">
<input class="btn btn-sm" type="button" id="buttonFetchAll" onmouseup="fetchAll()" value="Fetch all">
</div>
<div id="video-container" class="col-md-12"></div>
<div id="recording-btns">
<div id="btns">
<input class="btn btn-large" type="button" onmouseup="startRecording()" value="Start recording">
<input class="btn btn-large" type="button" onmouseup="stopRecording()" value="Stop recording">
<input class="btn btn-large" type="button" onmouseup="deleteRecording()" value="Delete recording">
<input class="btn btn-large" type="button" onmouseup="getRecording()" value="Get recording">
<input class="btn btn-large" type="button" onmouseup="listRecordings()" value="List recordings">
<input class="btn btn-md" type="button" id="buttonStartRecording" onmouseup="startRecording()" value="Start recording">
<input class="btn btn-md" type="button" id="buttonListRecording" onmouseup="listRecordings()" value="List recordings">
<input class="form-control" id="forceRecordingId" type="text" onkeyup="checkBtnsRecordings()">
<input class="btn btn-md" type="button" id="buttonGetRecording" onmouseup="getRecording()" value="Get recording" disabled>
<input class="btn btn-md" type="button" id="buttonStopRecording" onmouseup="stopRecording()" value="Stop recording" disabled>
<input class="btn btn-md" type="button" id="buttonDeleteRecording" onmouseup="deleteRecording()" value="Delete recording"
disabled>
</div>
<textarea id="text-area" readonly="true" class="form-control" name="comment">HTTP responses...</textarea>
</div>

View File

@ -46,7 +46,7 @@ nav i.fa:hover {
#main-container {
padding-bottom: 80px;
height: 100%;
margin-top: -70px;
margin-top: -70px;
}
.vertical-center {
@ -225,20 +225,27 @@ a:hover .demo-logo {
#session-header {
margin-bottom: 20px;
height: 8%;
margin-top: 70px;
margin-top: 70px;
}
#session-header form {
display: inline;
}
#session-header input.btn {
float: right;
margin-top: 20px;
margin-left: 5px;
}
#session-title {
display: inline-block;
}
#buttonLeaveSession {
#session-header .form-control {
width: initial;
float: right;
margin-top: 20px;
margin: 18px 0px 0px 5px;
}
#video-container {
@ -248,15 +255,15 @@ a:hover .demo-logo {
overflow: hidden;
}
#video-container video.two{
#video-container video.two {
width: 50%;
}
#video-container video.three{
#video-container video.three {
width: 33.33%;
}
#video-container video.four{
#video-container video.four {
width: 25%;
}
@ -295,7 +302,7 @@ video {
#session {
height: 100%;
padding-bottom: 80px;
padding-bottom: 80px;
}
#session img {
@ -353,19 +360,20 @@ table i {
height: 40%;
}
#recording-btns input {
margin-top: 5px;
#recording-btns #btns {
margin-top: 10px;
}
#recording-btns #btns {
margin-top: 5px;
#recording-btns #btns .form-control {
width: initial;
display: inline-block;
}
#recording-btns #text-area {
display: inline-block;
width: 100%;
height: 80%;
margin-top: 20px;
width: 100%;
height: 80%;
margin-top: 20px;
}
/* xs ans md screen resolutions*/

View File

@ -67,7 +67,7 @@ app.post('/api/get-token', function (req, res) {
console.log("Getting a token | {sessionName}={" + sessionName + "}");
// Build tokenOptions object with PUBLIHSER role
// Build tokenOptions object with PUBLISHER role
var tokenOptions = { role: role }
if (mapSessions[sessionName]) {
@ -133,8 +133,8 @@ app.post('/api/remove-user', function (req, res) {
var token = req.body.token;
console.log('Removing user | {sessionName, token}={' + sessionName + ', ' + token + '}');
// If the session exists
if (mapSessions[sessionName] && mapSessionNamesTokens[sessionName]) {
// If the session exists
if (mapSessions[sessionName] && mapSessionNamesTokens[sessionName]) {
var tokens = mapSessionNamesTokens[sessionName];
var index = tokens.indexOf(token);
@ -161,6 +161,96 @@ app.post('/api/remove-user', function (req, res) {
}
});
// Close session
app.delete('/api/close-session', function (req, res) {
// Retrieve params from POST body
var sessionName = req.body.sessionName;
console.log("Closing session | {sessionName}=" + sessionName);
// If the session exists
if (mapSessions[sessionName]) {
var session = mapSessions[sessionName];
session.close();
delete mapSessions[sessionName];
delete mapSessionNamesTokens[sessionName];
res.status(200).send();
} else {
var msg = 'Problems in the app server: the SESSION does not exist';
console.log(msg);
res.status(500).send(msg);
}
});
// Fetch session info
app.post('/api/fetch-info', function (req, res) {
// Retrieve params from POST body
var sessionName = req.body.sessionName;
console.log("Fetching session info | {sessionName}=" + sessionName);
// If the session exists
if (mapSessions[sessionName]) {
mapSessions[sessionName].fetch()
.then(changed => {
console.log("Any change: " + changed);
res.status(200).send(sessionToJson(mapSessions[sessionName]));
})
.catch(error => res.status(400).send(error.message));
} else {
var msg = 'Problems in the app server: the SESSION does not exist';
console.log(msg);
res.status(500).send(msg);
}
});
// Fetch all session info
app.get('/api/fetch-all', function (req, res) {
console.log("Fetching all session info");
OV.fetch()
.then(changed => {
var sessions = [];
OV.activeSessions.forEach(s => {
sessions.push(sessionToJson(s));
});
console.log("Any change: " + changed);
res.status(200).send(sessions);
})
.catch(error => res.status(400).send(error.message));
});
// Force disconnect
app.delete('/api/force-disconnect', function (req, res) {
// Retrieve params from POST body
var sessionName = req.body.sessionName;
var connectionId = req.body.connectionId;
// If the session exists
if (mapSessions[sessionName]) {
mapSessions[sessionName].forceDisconnect(connectionId)
.then(() => res.status(200).send())
.catch(error => res.status(400).send(error.message));
} else {
var msg = 'Problems in the app server: the SESSION does not exist';
console.log(msg);
res.status(500).send(msg);
}
});
// Force unpublish
app.delete('/api/force-unpublish', function (req, res) {
// Retrieve params from POST body
var sessionName = req.body.sessionName;
var streamId = req.body.streamId;
// If the session exists
if (mapSessions[sessionName]) {
mapSessions[sessionName].forceUnpublish(streamId)
.then(() => res.status(200).send())
.catch(error => res.status(400).send(error.message));
} else {
var msg = 'Problems in the app server: the SESSION does not exist';
console.log(msg);
res.status(500).send(msg);
}
});
/* Recording API */
@ -180,7 +270,7 @@ app.post('/api/recording/start', function (req, res) {
app.post('/api/recording/stop', function (req, res) {
// Retrieve params from POST body
var recordingId = req.body.recording;
console.log("Stoping recording | {recordingId}=" + recordingId);
console.log("Stopping recording | {recordingId}=" + recordingId);
OV.stopRecording(recordingId)
.then(recording => res.status(200).send(getJsonFromRecording(recording)))
@ -240,4 +330,49 @@ function getJsonArrayFromRecordingList(recordings) {
jsonArray.push(getJsonFromRecording(recording));
})
return jsonArray;
}
function sessionToJson(session) {
var json = {};
json.sessionId = session.sessionId;
json.customSessionId = !!session.properties.customSessionId ? session.properties.customSessionId : "";
json.recording = session.recording;
json.mediaMode = session.properties.mediaMode;
json.recordingMode = session.properties.recordingMode;
json.defaultRecordingLayout = session.properties.defaultRecordingLayout;
json.defaultCustomLayout = !!session.properties.defaultCustomLayout ? session.properties.defaultCustomLayout : "";
var connections = {};
connections.numberOfElements = session.activeConnections.length;
var jsonArrayConnections = [];
session.activeConnections.forEach(con => {
var c = {};
c.connectionId = con.connectionId;
c.role = con.role;
c.token = con.token;
c.clientData = con.clientData;
c.serverData = con.serverData;
var pubs = [];
con.publishers.forEach(p => {
jsonP = {};
jsonP.streamId = p.streamId
jsonP.hasAudio = p.hasAudio;
jsonP.hasVideo = p.hasVideo;
jsonP.audioActive = p.audioActive;
jsonP.videoActive = p.videoActive;
jsonP.frameRate = p.frameRate;
jsonP.typeOfVideo = p.typeOfVideo;
jsonP.videoDimensions = p.videoDimensions;
pubs.push(jsonP);
});
var subs = [];
con.subscribers.forEach(s => {
subs.push(s);
});
c.publishers = pubs;
c.subscribers = subs;
jsonArrayConnections.push(c);
});
connections.content = jsonArrayConnections;
json.connections = connections;
return json;
}