openvidu-backend-client: Session.close()

pull/87/merge
pabloFuente 2018-07-13 12:56:27 +02:00
parent 062f43d4f4
commit 79dcb26e6b
3 changed files with 70 additions and 3 deletions

View File

@ -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
*/

View File

@ -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 {

View File

@ -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<string>((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
*/