mirror of https://github.com/OpenVidu/openvidu.git
openvidu-backend-client: Session.close()
parent
062f43d4f4
commit
79dcb26e6b
|
@ -23,6 +23,7 @@ import java.io.UnsupportedEncodingException;
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.util.EntityUtils;
|
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
|
* Returns the properties defining the session
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -18,10 +18,9 @@
|
||||||
import { Session } from './Session';
|
import { Session } from './Session';
|
||||||
import { SessionProperties } from './SessionProperties';
|
import { SessionProperties } from './SessionProperties';
|
||||||
import { Recording } from './Recording';
|
import { Recording } from './Recording';
|
||||||
import { RecordingLayout } from './RecordingLayout';
|
|
||||||
import { RecordingProperties } from './RecordingProperties';
|
import { RecordingProperties } from './RecordingProperties';
|
||||||
|
|
||||||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
export class OpenVidu {
|
export class OpenVidu {
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ import { RecordingMode } from './RecordingMode';
|
||||||
import { SessionProperties } from './SessionProperties';
|
import { SessionProperties } from './SessionProperties';
|
||||||
import { TokenOptions } from './TokenOptions';
|
import { TokenOptions } from './TokenOptions';
|
||||||
|
|
||||||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
export class Session {
|
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
|
* @hidden
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue