From 79dcb26e6b9fe8a762d476488ce453b64803df23 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Fri, 13 Jul 2018 12:56:27 +0200 Subject: [PATCH] openvidu-backend-client: Session.close() --- .../java/io/openvidu/java/client/Session.java | 27 ++++++++++++ openvidu-node-client/src/OpenVidu.ts | 3 +- openvidu-node-client/src/Session.ts | 43 ++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java index ef6d31c6..561e18e5 100644 --- a/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java +++ b/openvidu-java-client/src/main/java/io/openvidu/java/client/Session.java @@ -23,6 +23,7 @@ import java.io.UnsupportedEncodingException; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; @@ -128,6 +129,32 @@ public class Session { } } + /** + * Gracefully closes the Session: unpublishes all streams and evicts every + * participant + * + * @throws OpenViduJavaClientException + * @throws OpenViduHttpException + */ + public void close() throws OpenViduJavaClientException, OpenViduHttpException { + HttpDelete request = new HttpDelete(this.urlOpenViduServer + API_SESSIONS + "/" + this.sessionId); + request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); + + HttpResponse response; + try { + response = httpClient.execute(request); + } catch (IOException e) { + throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); + } + + int statusCode = response.getStatusLine().getStatusCode(); + if ((statusCode == org.apache.http.HttpStatus.SC_NO_CONTENT)) { + System.out.println("Session closed"); + } else { + throw new OpenViduHttpException(statusCode); + } + } + /** * Returns the properties defining the session */ diff --git a/openvidu-node-client/src/OpenVidu.ts b/openvidu-node-client/src/OpenVidu.ts index 553ea182..3e71f0a7 100644 --- a/openvidu-node-client/src/OpenVidu.ts +++ b/openvidu-node-client/src/OpenVidu.ts @@ -18,10 +18,9 @@ import { Session } from './Session'; import { SessionProperties } from './SessionProperties'; import { Recording } from './Recording'; -import { RecordingLayout } from './RecordingLayout'; import { RecordingProperties } from './RecordingProperties'; -import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; +import axios from 'axios'; export class OpenVidu { diff --git a/openvidu-node-client/src/Session.ts b/openvidu-node-client/src/Session.ts index 40045a8c..303ed086 100644 --- a/openvidu-node-client/src/Session.ts +++ b/openvidu-node-client/src/Session.ts @@ -22,7 +22,7 @@ import { RecordingMode } from './RecordingMode'; import { SessionProperties } from './SessionProperties'; import { TokenOptions } from './TokenOptions'; -import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; +import axios from 'axios'; export class Session { @@ -98,6 +98,47 @@ export class Session { }); } + /** + * Gracefully closes the Session: unpublishes all streams and evicts every participant + * + * @returns A Promise that is resolved to the if the session has been closed successfully and rejected with an Error object if not + */ + public close() { + return new Promise((resolve, reject) => { + axios.delete( + 'https://' + this.hostname + ':' + this.port + Session.API_SESSIONS + '/' + this.sessionId, + { + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + ) + .then(res => { + if (res.status === 204) { + // SUCCESS response from openvidu-server + resolve(); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.status.toString())); + } + }).catch(error => { + if (error.response) { + // The request was made and the server responded with a status code (not 2xx) + reject(new Error(error.response.status.toString())); + } else if (error.request) { + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + console.error(error.request); + } else { + // Something happened in setting up the request that triggered an Error + console.error('Error', error.message); + } + }); + }); + } + /** * @hidden */