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

126 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var OpenViduRole_1 = require("./OpenViduRole");
var SessionProperties_1 = require("./SessionProperties");
2017-06-10 01:44:31 +02:00
var https = require('https');
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 = "";
if (properties == null) {
this.properties = new SessionProperties_1.SessionProperties.Builder().build();
}
else {
this.properties = properties;
}
2017-06-10 01:44:31 +02:00
}
Session.prototype.getSessionId = function (callback) {
var _this = this;
if (this.sessionId) {
2017-09-04 17:22:46 +02:00
callback(this.sessionId);
return;
2017-06-10 01:44:31 +02:00
}
var requestBody = JSON.stringify({
'archiveLayout': this.properties.archiveLayout(),
'archiveMode': this.properties.archiveMode(),
'mediaMode': this.properties.mediaMode()
});
2017-06-10 01:44:31 +02:00
var options = {
hostname: this.hostname,
port: this.port,
2018-03-14 18:48:29 +01:00
path: Session.API_SESSIONS,
2017-06-10 01:44:31 +02:00
method: 'POST',
headers: {
2018-03-14 18:48:29 +01:00
'Authorization': this.basicAuth,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(requestBody)
2017-06-10 01:44:31 +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 () {
2018-03-14 18:48:29 +01:00
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve sessionId
var parsed = JSON.parse(body);
_this.sessionId = parsed.id;
callback(parsed.id);
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
console.error(res.statusCode);
}
2017-06-10 01:44:31 +02:00
});
});
req.on('error', function (e) {
console.error(e);
});
req.write(requestBody);
2017-06-10 01:44:31 +02:00
req.end();
};
Session.prototype.generateToken = function (tokenOptions, callback) {
var requestBody;
if (callback) {
requestBody = JSON.stringify({
'session': this.sessionId,
'role': tokenOptions.getRole(),
'data': tokenOptions.getData()
});
}
else {
requestBody = JSON.stringify({
'session': this.sessionId,
'role': OpenViduRole_1.OpenViduRole.PUBLISHER,
'data': ''
});
callback = tokenOptions;
}
2017-06-10 01:44:31 +02:00
var options = {
hostname: this.hostname,
port: this.port,
2018-03-14 18:48:29 +01:00
path: Session.API_TOKENS,
2017-06-10 01:44:31 +02:00
method: 'POST',
headers: {
2018-03-14 18:48:29 +01:00
'Authorization': this.basicAuth,
2017-06-10 01:44:31 +02:00
'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 () {
2018-03-14 18:48:29 +01:00
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve token
var parsed = JSON.parse(body);
callback(parsed.id);
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
console.error(res.statusCode);
}
2017-06-10 01:44:31 +02:00
});
});
req.on('error', function (e) {
console.error(e);
});
req.write(requestBody);
req.end();
};
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