openvidu-node-client: async call from getSessionId to createSession

pull/73/head
pabloFuente 2018-04-25 17:50:55 +02:00
parent 5fd6db8994
commit 969795645b
10 changed files with 170 additions and 125 deletions

View File

@ -15,7 +15,12 @@ export declare class OpenVidu {
* @param secret Secret used on OpenVidu Server initialization
*/
constructor(urlOpenViduServer: string, secret: string);
createSession(properties?: SessionProperties): Session;
/**
* Creates an OpenVidu session. You can call [[Session.getSessionId]] in the resolved promise to retrieve the `sessionId`
*
* @returns A Promise that is resolved to the [[Session]] if success and rejected with an Error object if not
*/
createSession(properties?: SessionProperties): Promise<Session>;
startRecording(sessionId: string): Promise<Recording>;
startRecording(sessionId: string, name: string): Promise<Recording>;
startRecording(sessionId: string, properties: RecordingProperties): Promise<Recording>;

View File

@ -29,8 +29,23 @@ var OpenVidu = /** @class */ (function () {
this.setHostnameAndPort();
this.basicAuth = this.getBasicAuth(secret);
}
/**
* Creates an OpenVidu session. You can call [[Session.getSessionId]] in the resolved promise to retrieve the `sessionId`
*
* @returns A Promise that is resolved to the [[Session]] if success and rejected with an Error object if not
*/
OpenVidu.prototype.createSession = function (properties) {
return new Session_1.Session(this.hostname, this.port, this.basicAuth, properties);
var _this = this;
return new Promise(function (resolve, reject) {
var session = new Session_1.Session(_this.hostname, _this.port, _this.basicAuth, properties);
session.getSessionIdHttp()
.then(function (sessionId) {
resolve(session);
})
.catch(function (error) {
reject(error);
});
});
};
/**
* Starts the recording of a [[Session]]
@ -38,7 +53,7 @@ var OpenVidu = /** @class */ (function () {
* @param sessionId The `sessionId` of the [[Session]] you want to start recording
* @param name The name you want to give to the video file. You can access this same value in your clients on recording events (`recordingStarted`, `recordingStopped`)
*
* @returns A Promise that is resolved to the [[Recording]] if it successfully started and rejected with an Error object if not. This Error object has as `message` property with the following values:
* @returns A Promise that is resolved to the [[Recording]] if it successfully started (the recording can be stopped with guarantees) and rejected with an Error object if not. This Error object has as `message` property with the following values:
* - `404`: no session exists for the passed `sessionId`
* - `400`: the session has no connected participants
* - `409`: the session is not configured for using [[MediaMode.ROUTED]] or it is already being recorded

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
{"version":3,"file":"OpenViduRole.js","sourceRoot":"","sources":["../src/OpenViduRole.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;AAEH,IAAY,YAgBX;AAhBD,WAAY,YAAY;IAEvB;;OAEG;IACH,yCAAyB,CAAA;IAEzB;;OAEG;IACH,uCAAuB,CAAA;IAEvB;;OAEG;IACH,uCAAuB,CAAA;AACxB,CAAC,EAhBW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAgBvB"}
{"version":3,"file":"OpenViduRole.js","sourceRoot":"","sources":["../src/OpenViduRole.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;AAEH,IAAY,YAgBX;AAhBD,WAAY,YAAY;IAEpB;;OAEG;IACH,yCAAyB,CAAA;IAEzB;;OAEG;IACH,uCAAuB,CAAA;IAEvB;;OAEG;IACH,uCAAuB,CAAA;AAC3B,CAAC,EAhBW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAgBvB"}

View File

@ -10,16 +10,17 @@ export declare class Session {
properties: SessionProperties;
constructor(hostname: string, port: number, basicAuth: string, properties?: SessionProperties);
/**
* Gets the unique identifier of the Session. This translates into a new request to OpenVidu Server if this Session has no `sessionId` yet
* or simply returns the existing value if it has already been retrieved
*
* @returns A Promise that is resolved to the _sessionId_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
* Gets the unique identifier of the Session
*/
getSessionId(): Promise<string>;
getSessionId(): string;
/**
* Gets a new token associated to Session object. This translates into a new request to OpenVidu Server
* Gets a new token associated to Session object
*
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not
*/
generateToken(tokenOptions?: TokenOptions): Promise<string>;
/**
* @hidden
*/
getSessionIdHttp(): Promise<string>;
}

View File

@ -34,12 +34,64 @@ var Session = /** @class */ (function () {
}
}
/**
* Gets the unique identifier of the Session. This translates into a new request to OpenVidu Server if this Session has no `sessionId` yet
* or simply returns the existing value if it has already been retrieved
*
* @returns A Promise that is resolved to the _sessionId_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
* Gets the unique identifier of the Session
*/
Session.prototype.getSessionId = function () {
return this.sessionId;
};
/**
* Gets a new token associated to Session object
*
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not
*/
Session.prototype.generateToken = function (tokenOptions) {
var _this = this;
return new Promise(function (resolve, reject) {
var requestBody = JSON.stringify({
session: _this.sessionId,
role: !!tokenOptions.role ? tokenOptions.role : OpenViduRole_1.OpenViduRole.PUBLISHER,
data: !!tokenOptions.data ? tokenOptions.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)
}
};
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));
}
});
});
req.on('error', function (e) {
reject(e);
});
req.write(requestBody);
req.end();
});
};
/**
* @hidden
*/
Session.prototype.getSessionIdHttp = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (!!_this.sessionId) {
@ -88,55 +140,6 @@ var Session = /** @class */ (function () {
req.end();
});
};
/**
* Gets a new token associated to Session object. This translates into a new request to OpenVidu Server
*
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
*/
Session.prototype.generateToken = function (tokenOptions) {
var _this = this;
return new Promise(function (resolve, reject) {
var requestBody = JSON.stringify({
session: _this.sessionId,
role: !!tokenOptions.role ? tokenOptions.role : OpenViduRole_1.OpenViduRole.PUBLISHER,
data: !!tokenOptions.data ? tokenOptions.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)
}
};
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));
}
});
});
req.on('error', function (e) {
reject(e);
});
req.write(requestBody);
req.end();
});
};
Session.API_SESSIONS = '/api/sessions';
Session.API_TOKENS = '/api/tokens';
return Session;

View File

@ -1 +1 @@
{"version":3,"file":"Session.js","sourceRoot":"","sources":["../src/Session.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;AAEH,yCAAwC;AACxC,+CAA8C;AAC9C,qDAAoD;AACpD,iDAAgD;AAOhD,IAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE/B;IAQI,iBAAoB,QAAgB,EAAU,IAAY,EAAU,SAAiB,EAAE,UAA8B;QAAjG,aAAQ,GAAR,QAAQ,CAAQ;QAAU,SAAI,GAAJ,IAAI,CAAQ;QAAU,cAAS,GAAT,SAAS,CAAQ;QACjF,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;YACd,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACzB,CAAC;QAAC,IAAI,CAAC,CAAC;YACJ,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QACjC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,8BAAY,GAAnB;QAAA,iBAmDC;QAlDG,MAAM,CAAC,IAAI,OAAO,CAAS,UAAC,OAAO,EAAE,MAAM;YAEv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C,WAAW,CAAC;iBACnD;aACJ,CAAC;YAEF,IAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,UAAC,GAAG;gBACnC,IAAI,IAAI,GAAG,EAAE,CAAC;gBACd,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,UAAC,CAAC;oBACb,uCAAuC;oBACvC,IAAI,IAAI,CAAC,CAAC;gBACd,CAAC,CAAC,CAAC;gBACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE;oBACV,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,CAAC,CAAC;wBACzB,2DAA2D;wBAC3D,IAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;wBAChC,KAAI,CAAC,SAAS,GAAG,MAAM,CAAC,EAAE,CAAC;wBAC3B,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;oBACvB,CAAC;oBAAC,IAAI,CAAC,CAAC;wBACJ,2DAA2D;wBAC3D,MAAM,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;oBACtC,CAAC;gBACL,CAAC,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;YAEH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,UAAC,CAAC;gBACd,MAAM,CAAC,CAAC,CAAC,CAAC;YACd,CAAC,CAAC,CAAC;YACH,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;YACvB,GAAG,CAAC,GAAG,EAAE,CAAC;QACd,CAAC,CAAC,CAAC;IACP,CAAC;IA/HuB,oBAAY,GAAG,eAAe,CAAC;IAC/B,kBAAU,GAAG,aAAa,CAAC;IAgIvD,cAAC;CAAA,AAnID,IAmIC;AAnIY,0BAAO"}

View File

@ -7,5 +7,8 @@ export interface TokenOptions {
* parameter passed in OpenVidu Browser in method `Session.connect` and `SERVER_DATA` this [[TokenOptions.data]] property.
*/
data: string;
/**
* The role assigned to this token
*/
role: OpenViduRole;
}

View File

@ -43,8 +43,22 @@ export class OpenVidu {
this.basicAuth = this.getBasicAuth(secret);
}
public createSession(properties?: SessionProperties): Session {
return new Session(this.hostname, this.port, this.basicAuth, properties);
/**
* Creates an OpenVidu session. You can call [[Session.getSessionId]] in the resolved promise to retrieve the `sessionId`
*
* @returns A Promise that is resolved to the [[Session]] if success and rejected with an Error object if not
*/
public createSession(properties?: SessionProperties): Promise<Session> {
return new Promise<Session>((resolve, reject) => {
const session = new Session(this.hostname, this.port, this.basicAuth, properties);
session.getSessionIdHttp()
.then(sessionId => {
resolve(session);
})
.catch(error => {
reject(error);
});
});
}
public startRecording(sessionId: string): Promise<Recording>;

View File

@ -44,12 +44,68 @@ export class Session {
}
/**
* Gets the unique identifier of the Session. This translates into a new request to OpenVidu Server if this Session has no `sessionId` yet
* or simply returns the existing value if it has already been retrieved
*
* @returns A Promise that is resolved to the _sessionId_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
* Gets the unique identifier of the Session
*/
public getSessionId(): Promise<string> {
public getSessionId(): string {
return this.sessionId;
}
/**
* Gets a new token associated to Session object
*
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not
*/
public generateToken(tokenOptions?: TokenOptions): Promise<string> {
return new Promise<string>((resolve, reject) => {
const requestBody = JSON.stringify({
session: this.sessionId,
role: !!tokenOptions.role ? tokenOptions.role : OpenViduRole.PUBLISHER,
data: !!tokenOptions.data ? tokenOptions.data : ''
});
const 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)
}
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve token
const parsed = JSON.parse(body);
resolve(parsed.id);
} else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', (e) => {
reject(e);
});
req.write(requestBody);
req.end();
});
}
/**
* @hidden
*/
public getSessionIdHttp(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (!!this.sessionId) {
@ -102,56 +158,4 @@ export class Session {
});
}
/**
* Gets a new token associated to Session object. This always translates into a new request to OpenVidu Server
*
* @returns A Promise that is resolved to the _token_ if success and rejected with an Error object if not (due to a `400 (Bad Request)` error in OpenVidu Server)
*/
public generateToken(tokenOptions?: TokenOptions): Promise<string> {
return new Promise<string>((resolve, reject) => {
const requestBody = JSON.stringify({
session: this.sessionId,
role: !!tokenOptions.role ? tokenOptions.role : OpenViduRole.PUBLISHER,
data: !!tokenOptions.data ? tokenOptions.data : ''
});
const 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)
}
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (d) => {
// Continuously update stream with data
body += d;
});
res.on('end', () => {
if (res.statusCode === 200) {
// SUCCESS response from openvidu-server. Resolve token
const parsed = JSON.parse(body);
resolve(parsed.id);
} else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.statusCode));
}
});
});
req.on('error', (e) => {
reject(e);
});
req.write(requestBody);
req.end();
});
}
}