2017-06-10 01:44:31 +02:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2017-06-19 10:16:14 +02:00
|
|
|
var OpenViduRole_1 = require("./OpenViduRole");
|
2018-01-27 19:39:49 +01:00
|
|
|
var SessionProperties_1 = require("./SessionProperties");
|
2017-06-10 01:44:31 +02:00
|
|
|
var https = require('https');
|
2018-01-27 19:39:49 +01:00
|
|
|
var Session = /** @class */ (function () {
|
2018-03-14 18:48:29 +01:00
|
|
|
function Session(hostname, port, basicAuth, properties) {
|
|
|
|
this.hostname = hostname;
|
|
|
|
this.port = port;
|
|
|
|
this.basicAuth = basicAuth;
|
2017-06-10 01:44:31 +02:00
|
|
|
this.sessionId = "";
|
2018-01-27 19:39:49 +01:00
|
|
|
if (properties == null) {
|
|
|
|
this.properties = new SessionProperties_1.SessionProperties.Builder().build();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
Session.prototype.getSessionId = function () {
|
2017-06-10 01:44:31 +02:00
|
|
|
var _this = this;
|
2018-04-18 10:56:28 +02:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (_this.sessionId) {
|
|
|
|
resolve(_this.sessionId);
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
var requestBody = JSON.stringify({
|
|
|
|
'recordingLayout': _this.properties.recordingLayout(),
|
|
|
|
'recordingMode': _this.properties.recordingMode(),
|
|
|
|
'mediaMode': _this.properties.mediaMode()
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
var options = {
|
|
|
|
hostname: _this.hostname,
|
|
|
|
port: _this.port,
|
|
|
|
path: Session.API_SESSIONS,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': _this.basicAuth,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': Buffer.byteLength(requestBody)
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
};
|
|
|
|
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. Resolve sessionId
|
|
|
|
var parsed = JSON.parse(body);
|
|
|
|
_this.sessionId = parsed.id;
|
|
|
|
resolve(parsed.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
req.on('error', function (e) {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
};
|
2018-04-18 10:56:28 +02:00
|
|
|
Session.prototype.generateToken = function (tokenOptions) {
|
|
|
|
var _this = this;
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var requestBody;
|
|
|
|
if (!!tokenOptions) {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
'session': _this.sessionId,
|
|
|
|
'role': tokenOptions.getRole(),
|
|
|
|
'data': tokenOptions.getData()
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
else {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
'session': _this.sessionId,
|
|
|
|
'role': OpenViduRole_1.OpenViduRole.PUBLISHER,
|
|
|
|
'data': ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var options = {
|
|
|
|
hostname: _this.hostname,
|
|
|
|
port: _this.port,
|
|
|
|
path: Session.API_TOKENS,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': _this.basicAuth,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': Buffer.byteLength(requestBody)
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-04-18 10:56:28 +02:00
|
|
|
};
|
|
|
|
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. Resolve token
|
|
|
|
var parsed = JSON.parse(body);
|
|
|
|
resolve(parsed.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
2018-04-18 10:56:28 +02:00
|
|
|
req.on('error', function (e) {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
2017-06-10 01:44:31 +02:00
|
|
|
});
|
|
|
|
};
|
2018-01-27 19:39:49 +01:00
|
|
|
Session.prototype.getProperties = function () {
|
|
|
|
return this.properties;
|
|
|
|
};
|
2018-03-14 18:48:29 +01:00
|
|
|
Session.API_SESSIONS = '/api/sessions';
|
|
|
|
Session.API_TOKENS = '/api/tokens';
|
2017-06-10 01:44:31 +02:00
|
|
|
return Session;
|
|
|
|
}());
|
|
|
|
exports.Session = Session;
|
|
|
|
//# sourceMappingURL=Session.js.map
|