openvidu/openvidu-node-client/src/Session.ts

137 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
import { TokenOptions } from './TokenOptions';
import { OpenViduRole } from './OpenViduRole';
import { SessionProperties } from './SessionProperties';
2017-06-10 01:44:31 +02:00
declare const Buffer;
declare const require;
let https = require('https');
2017-06-10 01:44:31 +02:00
export class Session {
2018-03-14 18:48:29 +01:00
private static readonly API_SESSIONS: string = '/api/sessions';
private static readonly API_TOKENS: string = '/api/tokens';
2017-06-10 01:44:31 +02:00
private sessionId: string = "";
private properties: SessionProperties;
2017-06-10 01:44:31 +02:00
2018-03-14 18:48:29 +01:00
constructor(private hostname: string, private port: number, private basicAuth: string, properties?: SessionProperties) {
if (properties == null) {
this.properties = new SessionProperties.Builder().build();
} else {
this.properties = properties;
}
2017-06-10 01:44:31 +02:00
}
public getSessionId(callback: Function) {
if (this.sessionId) {
callback(this.sessionId);
return;
2017-06-10 01:44:31 +02:00
}
let requestBody = JSON.stringify({
'archiveLayout': this.properties.archiveLayout(),
'archiveMode': this.properties.archiveMode(),
'mediaMode': this.properties.mediaMode()
});
2017-06-10 01:44:31 +02:00
let 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
}
}
const req = https.request(options, (res) => {
let body = '';
2017-06-10 01:44:31 +02:00
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
2018-03-14 18:48:29 +01:00
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve sessionId
let 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', (e) => {
console.error(e);
});
req.write(requestBody);
2017-06-10 01:44:31 +02:00
req.end();
}
public generateToken(callback: Function);
public generateToken(tokenOptions: TokenOptions, callback: Function);
public generateToken(tokenOptions: any, callback?: any) {
let requestBody;
if (callback) {
requestBody = JSON.stringify({
'session': this.sessionId,
'role': tokenOptions.getRole(),
'data': tokenOptions.getData()
});
} else {
requestBody = JSON.stringify({
'session': this.sessionId,
'role': OpenViduRole.PUBLISHER,
'data': ''
});
callback = tokenOptions;
}
let options = {
2017-06-10 01:44:31 +02:00
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)
}
};
const req = https.request(options, (res) => {
let body = '';
2017-06-10 01:44:31 +02:00
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
2018-03-14 18:48:29 +01:00
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve token
let 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', (e) => {
console.error(e);
});
req.write(requestBody);
req.end();
}
public getProperties(): SessionProperties {
2018-03-14 18:48:29 +01:00
return this.properties;
2017-06-10 01:44:31 +02:00
}
}