openvidu/openvidu-node-client/lib/OpenVidu.js

250 lines
9.9 KiB
JavaScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Session_1 = require("./Session");
var Recording_1 = require("./Recording");
2018-03-14 18:48:29 +01:00
var https = require('https');
var OpenVidu = /** @class */ (function () {
2017-06-10 01:44:31 +02:00
function OpenVidu(urlOpenViduServer, secret) {
this.urlOpenViduServer = urlOpenViduServer;
2018-03-14 18:48:29 +01:00
this.setHostnameAndPort();
this.basicAuth = this.getBasicAuth(secret);
2017-06-10 01:44:31 +02:00
}
OpenVidu.prototype.createSession = function (properties) {
2018-03-14 18:48:29 +01:00
return new Session_1.Session(this.hostname, this.port, this.basicAuth, properties);
};
2018-04-18 14:23:16 +02:00
OpenVidu.prototype.startRecording = function (sessionId, param2) {
2018-03-14 18:48:29 +01:00
var _this = this;
return new Promise(function (resolve, reject) {
2018-04-18 14:23:16 +02:00
var requestBody;
if (!!param2) {
if (!(typeof param2 === 'string')) {
requestBody = JSON.stringify({
session: sessionId,
name: param2.name()
});
}
else {
requestBody = JSON.stringify({
session: sessionId,
name: param2
});
}
}
else {
requestBody = JSON.stringify({
session: sessionId,
name: ''
});
}
2018-03-14 18:48:29 +01:00
var options = {
hostname: _this.hostname,
port: _this.port,
path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_START,
method: 'POST',
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBody)
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (d) {
// Continuously update stream with data
body += d;
});
res.on('end', function () {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(JSON.parse(body)));
2018-03-14 18:48:29 +01:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', function (e) {
reject(new Error(e));
});
req.write(requestBody);
req.end();
});
};
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.stopRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
var options = {
hostname: _this.hostname,
port: _this.port,
path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_STOP + '/' + recordingId,
method: 'POST',
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (d) {
// Continuously update stream with data
body += d;
});
res.on('end', function () {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(JSON.parse(body)));
2018-03-14 18:48:29 +01:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', function (e) {
reject(new Error(e));
});
//req.write();
req.end();
});
2017-06-10 01:44:31 +02:00
};
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.getRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
var options = {
hostname: _this.hostname,
port: _this.port,
path: OpenVidu.API_RECORDINGS + '/' + recordingId,
method: 'GET',
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (d) {
// Continuously update stream with data
body += d;
});
res.on('end', function () {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(JSON.parse(body)));
2018-03-14 18:48:29 +01:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', function (e) {
reject(new Error(e));
});
//req.write();
req.end();
});
};
OpenVidu.prototype.listRecordings = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var options = {
hostname: _this.hostname,
port: _this.port,
path: OpenVidu.API_RECORDINGS,
method: 'GET',
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (d) {
// Continuously update stream with data
body += d;
});
res.on('end', function () {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server (JSON arrays of recordings in JSON format). Resolve list of new recordings
var recordingArray = [];
2018-03-14 18:48:29 +01:00
var responseItems = JSON.parse(body)['items'];
for (var i = 0; i < responseItems.length; i++) {
recordingArray.push(new Recording_1.Recording(responseItems[i]));
2018-03-14 18:48:29 +01:00
}
resolve(recordingArray);
2018-03-14 18:48:29 +01:00
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', function (e) {
reject(new Error(e));
});
//req.write();
req.end();
});
};
OpenVidu.prototype.deleteRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
var options = {
hostname: _this.hostname,
port: _this.port,
path: OpenVidu.API_RECORDINGS + '/' + recordingId,
method: 'DELETE',
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = https.request(options, function (res) {
var body = '';
res.on('data', function (d) {
// Continuously update stream with data
body += d;
});
res.on('end', function () {
if (res.statusCode === 204) {
// SUCCESS response from openvidu-server. Resolve undefined
resolve(undefined);
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', function (e) {
reject(new Error(e));
});
//req.write();
req.end();
});
};
OpenVidu.prototype.getBasicAuth = function (secret) {
return 'Basic ' + (new Buffer('OPENVIDUAPP:' + secret).toString('base64'));
};
OpenVidu.prototype.setHostnameAndPort = function () {
var urlSplitted = this.urlOpenViduServer.split(':');
if (urlSplitted.length === 3) {
this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, '');
this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, ''));
}
else if (urlSplitted.length == 2) {
this.hostname = this.urlOpenViduServer.split(':')[0].replace(/\//g, '');
this.port = parseInt(this.urlOpenViduServer.split(':')[1].replace(/\//g, ''));
}
else {
console.error("URL format incorrect: it must contain hostname and port (current value: '" + this.urlOpenViduServer + "')");
}
};
OpenVidu.API_RECORDINGS = '/api/recordings';
OpenVidu.API_RECORDINGS_START = '/start';
OpenVidu.API_RECORDINGS_STOP = '/stop';
2017-06-10 01:44:31 +02:00
return OpenVidu;
}());
exports.OpenVidu = OpenVidu;
//# sourceMappingURL=OpenVidu.js.map