mirror of https://github.com/OpenVidu/openvidu.git
openvidu-backend-client refactoring
parent
5175c7e89b
commit
d4a270859d
72
README.md
72
README.md
|
@ -163,7 +163,7 @@ We recommend trying [this sample Angular app](#basic-angular-app).
|
||||||
|
|
||||||
#### ***Step by step*** ####
|
#### ***Step by step*** ####
|
||||||
|
|
||||||
1. Get an *OpenVidu* object and initialize a session with a *sessionId*. Remember this is the field that defines which video call to connect.
|
1. Get an *OpenVidu* object and initialize a session with a *sessionId*. Remember that this is the parameter that defines which video call to connect.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var OV = new OpenVidu("wss://" + OPENVIDU_SERVER_IP + ":8443/");
|
var OV = new OpenVidu("wss://" + OPENVIDU_SERVER_IP + ":8443/");
|
||||||
|
@ -222,7 +222,7 @@ We recommend trying [this sample app](#advanced-secure-app).
|
||||||
|
|
||||||
#### ***Step by step*** ####
|
#### ***Step by step*** ####
|
||||||
|
|
||||||
1. Import OpenVidu package and get an *OpenVidu* object. You need to provide to the constructor the IP of your OpenVidu Server and the secret shared with it (initialized by `openvidu.secret=MY_SECRET` property, as you can see [here](#advanced-secure-app) in the snippet which starts Docker container).
|
1. Import OpenVidu package and get an **OpenVidu** object. You need to provide to the constructor the IP of your OpenVidu Server and the secret shared with it (initialized by `openvidu.secret=MY_SECRET` property, as you can see [here](#advanced-secure-app) in the snippet which starts Docker container).
|
||||||
|
|
||||||
```java
|
```java
|
||||||
import org.openvidu.client.OpenVidu;
|
import org.openvidu.client.OpenVidu;
|
||||||
|
@ -230,13 +230,13 @@ We recommend trying [this sample app](#advanced-secure-app).
|
||||||
|
|
||||||
OpenVidu openVidu = new OpenVidu(OPENVIDU_SERVER_IP, YOUR_SECRET);
|
OpenVidu openVidu = new OpenVidu(OPENVIDU_SERVER_IP, YOUR_SECRET);
|
||||||
```
|
```
|
||||||
2. Get all the sessionId and tokens you need by calling the following methods. This process is up to you. As the developer of your app, you will have to decide when and how to return to clients these parameters, as well as the way they should be stored, reused and finally deleted.
|
2. Get all the **sessionId** and **tokens** you need by calling the following methods. This process is up to you. As the developer of your app, you will have to decide when and how to return to clients these parameters, as well as the way they should be stored, reused and finally deleted.
|
||||||
In [this class](https://github.com/OpenVidu/openvidu/blob/master/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java) of the secure sample app you have a way of dealing with it by using some concurrent maps and some REST controllers, but you can handle it as you wish.
|
In [this class](https://github.com/OpenVidu/openvidu/blob/master/openvidu-sample-app/src/main/java/openvidu/openvidu_sample_app/session_manager/SessionController.java) of the secure sample app you have a way of dealing with it by using some concurrent maps and some REST controllers, but you can handle it as you wish.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
String sessionId = openVidu.createSession();
|
Session session = openVidu.createSession();
|
||||||
...
|
String sessionId = session.getSessionId();
|
||||||
String token = openVidu.generateToken(sessionId, role);
|
String token = session.generateToken(OpenViduRole.PUSBLISHER);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -266,6 +266,14 @@ For secret "MY_SECRET", the final header would be
|
||||||
| **Returns** | {"0": "TOKEN"} |
|
| **Returns** | {"0": "TOKEN"} |
|
||||||
|
|
||||||
|
|
||||||
|
> **ROLE** value in Body field of POST to "/newToken" can be:
|
||||||
|
>
|
||||||
|
> - SUBSCRIBER
|
||||||
|
> - PUBLISHER
|
||||||
|
> - MODERATOR
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|
||||||
|
@ -273,24 +281,24 @@ API reference
|
||||||
===================
|
===================
|
||||||
## openvidu-browser
|
## openvidu-browser
|
||||||
|
|
||||||
| Class | Description |
|
| Class | Description |
|
||||||
| --------- | ---------------------------------------------------------- |
|
| ---------- | ---------------------------------------------------------- |
|
||||||
| [OpenVidu](#openvidu) | Use it to initialize your sessions and publishers |
|
| OpenVidu | Use it to initialize your sessions and publishers |
|
||||||
| [Session](#session) | Represents a video call. It can also be seen as a room where multiple users can connect. Participants who publish their videos to a session will be seen by the rest of users connected to that specific session |
|
| Session | Represents a video call. It can also be seen as a room where multiple users can connect. Participants who publish their videos to a session will be seen by the rest of users connected to that specific session |
|
||||||
| [Publisher](#publisher) | Packs local media streams. Users can publish it to a session |
|
| Publisher | Packs local media streams. Users can publish it to a session |
|
||||||
| [Subscriber](#subscriber) | Packs remote media streams. Users automatically receive them when others publish their streams|
|
| Subscriber | Packs remote media streams. Users automatically receive them when others publish their streams|
|
||||||
| [Stream](#stream) | Represents each of the videos send and receive by a user in a session. Therefore each Publisher and Subscriber has an attribute of type Stream |
|
| Stream | Represents each of the videos send and receive by a user in a session. Therefore each Publisher and Subscriber has an attribute of type Stream |
|
||||||
|
|
||||||
#### **OpenVidu**
|
#### **OpenVidu**
|
||||||
| Method | Returns | Parameters (show in order, optional italic) | Description |
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
| ---------------- | ------- | ------------------------------------------- | ----------- |
|
| ---------------- | ------- | ------------------------------------------- | ----------- |
|
||||||
| `initSession` | [Session](#session) | _`apikey:string`_<br/>`sessionId:string` | Returns a session with id **sessionId** |
|
| `initSession` | Session | _`apikey:string`_<br/>`sessionId:string` | Returns a session with id **sessionId** |
|
||||||
| `initPublisher` | [Publisher](#publisher) | `parentId:string`<br/>`cameraOptions:any`<br/>_`callback:function`_ | Starts local video stream, appending it to **parentId** HTML element, with the specific **cameraOptions** settings and executing **callback** function in the end |
|
| `initPublisher` | Publisher | `parentId:string`<br/>`cameraOptions:any`<br/>_`callback:function`_ | Starts local video stream, appending it to **parentId** HTML element, with the specific **cameraOptions** settings and executing **callback** function in the end |
|
||||||
| `checkSystemRequirements` | Number | | Returns 1 if the browser supports WebRTC, 0 otherwise|
|
| `checkSystemRequirements` | Number | | Returns 1 if the browser supports WebRTC, 0 otherwise|
|
||||||
| `getDevices` | Promise | `callback(error, deviceInfo):function` | Collects information about the media input and output devices available on the system, returned in **deviceInfo** array |
|
| `getDevices` | Promise | `callback(error, deviceInfo):function` | Collects information about the media input and output devices available on the system, returned in **deviceInfo** array |
|
||||||
|
|
||||||
#### **Session**
|
#### **Session**
|
||||||
| Method | Returns | Parameters (show in order, optional italic) | Description |
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
| ---------------- | ------- | ------------------------------------------- | ----------- |
|
| ---------------- | ------- | ------------------------------------------- | ----------- |
|
||||||
| `connect` | | `token:string`<br/>`callback(error):function` | Connects to the session using **token** and executes **callback** in the end (_error_ parameter null if success)|
|
| `connect` | | `token:string`<br/>`callback(error):function` | Connects to the session using **token** and executes **callback** in the end (_error_ parameter null if success)|
|
||||||
| `disconnect` | | | Leaves the session, destroying all streams and deleting the user as a participant |
|
| `disconnect` | | | Leaves the session, destroying all streams and deleting the user as a participant |
|
||||||
|
@ -299,7 +307,7 @@ API reference
|
||||||
| `on` | | `eventName:string`<br/>`callback:function` | **callback** function will be triggered each time **eventName** event is recieved |
|
| `on` | | `eventName:string`<br/>`callback:function` | **callback** function will be triggered each time **eventName** event is recieved |
|
||||||
| `once` | | `eventName:string`<br/>`callback:function` | **callback** function will be triggered once when **eventName** event is recieved. The listener is removed immediately |
|
| `once` | | `eventName:string`<br/>`callback:function` | **callback** function will be triggered once when **eventName** event is recieved. The listener is removed immediately |
|
||||||
| `off` | | `eventName:string`<br/>`eventHandler:any` | Removes **eventHandler** handler for **eventName** event |
|
| `off` | | `eventName:string`<br/>`eventHandler:any` | Removes **eventHandler** handler for **eventName** event |
|
||||||
| `subscribe` | [Subscriber](#subscriber) | `stream:Stream`<br/>`htmlId:string`<br/>_`videoOptions:any`_ | Subscribes to **stream**, appending a new HTML Video element to DOM element of **htmlId** id, with **videoOptions** settings. This method is usually called in the callback of _streamCreated_ event |
|
| `subscribe` | Subscriber | `stream:Stream`<br/>`htmlId:string`<br/>_`videoOptions:any`_ | Subscribes to **stream**, appending a new HTML Video element to DOM element of **htmlId** id, with **videoOptions** settings. This method is usually called in the callback of _streamCreated_ event |
|
||||||
| `unsubscribe` | | `subscriber:Subscriber` | Unsubscribes from **subscriber**, automatically removing its HTML Video element |
|
| `unsubscribe` | | `subscriber:Subscriber` | Unsubscribes from **subscriber**, automatically removing its HTML Video element |
|
||||||
|
|
||||||
| Property | Type | Description |
|
| Property | Type | Description |
|
||||||
|
@ -308,11 +316,11 @@ API reference
|
||||||
|
|
||||||
|
|
||||||
#### **Publisher**
|
#### **Publisher**
|
||||||
| Method | Returns | Parameters (show in order, optional italic) | Description |
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
| -------------- | ------- | ------------------------------------------- | ----------- |
|
| -------------- | ------- | ------------------------------------------- | ----------- |
|
||||||
| `publishAudio` | | `value:boolean`| Enable or disable the audio track depending on whether value is _true_ or _false_ |
|
| `publishAudio` | | `value:boolean`| Enable or disable the audio track depending on whether value is _true_ or _false_ |
|
||||||
| `publishVideo` | | `value:boolean`| Enable or disable the video track depending on whether value is _true_ or _false_ |
|
| `publishVideo` | | `value:boolean`| Enable or disable the video track depending on whether value is _true_ or _false_ |
|
||||||
| `destroy` | [Publisher](#publisher) || Delets the publisher object and removes it from DOM. The rest of users will trigger a _streamDestroyed_ event |
|
| `destroy` | Publisher || Delets the publisher object and removes it from DOM. The rest of users will trigger a _streamDestroyed_ event |
|
||||||
|
|
||||||
| Property | Type | Description |
|
| Property | Type | Description |
|
||||||
| ------------| ------ | ---------------------------- |
|
| ------------| ------ | ---------------------------- |
|
||||||
|
@ -320,10 +328,10 @@ API reference
|
||||||
| `element` | Element | The parent HTML Element which contains the publisher |
|
| `element` | Element | The parent HTML Element which contains the publisher |
|
||||||
| `id` | string | The id of the HTML Video element of the publisher |
|
| `id` | string | The id of the HTML Video element of the publisher |
|
||||||
| `stream` | Stream | The stream object of the publisher |
|
| `stream` | Stream | The stream object of the publisher |
|
||||||
| `session` | [Session](#session) | The session to which the publisher belongs |
|
| `session` | Session | The session to which the publisher belongs |
|
||||||
|
|
||||||
#### **Subscriber**
|
#### **Subscriber**
|
||||||
| Method | Returns | Parameters (show in order, optional italic) | Description |
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
| -------------- | ------- | ------------------------------------------- | ----------- |
|
| -------------- | ------- | ------------------------------------------- | ----------- |
|
||||||
| | | | |
|
| | | | |
|
||||||
|
|
||||||
|
@ -336,9 +344,23 @@ API reference
|
||||||
|
|
||||||
## openvidu-backend-client
|
## openvidu-backend-client
|
||||||
|
|
||||||
| Class | Description |
|
| Class | Description |
|
||||||
| --------- | ---------------------------------------------------------- |
|
| -------- | ------------------------------------------------------- |
|
||||||
| [`OpenVidu`](#openvidu-backend-client) | |
|
| OpenVidu | Use it to create all the sessions you need |
|
||||||
|
| Session | Allows the creation of tokens with different roles |
|
||||||
|
|
||||||
|
#### **OpenVidu**
|
||||||
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
|
| -------------- | ------- | --------------------------------------------- | ----------- |
|
||||||
|
| OpenVidu() | | `String urlOpenViduServer`<br>`String secret` | The constructor receives the URL of your OpenVidu Server and the secret shared with it |
|
||||||
|
| createSession() | Session | | Get a Session object by calling this method. You can then store it as you want |
|
||||||
|
|
||||||
|
#### **Session**
|
||||||
|
| Method | Returns | Parameters (show in order, optional _italic_) | Description |
|
||||||
|
| -------------- | ------- | --------------------------------------------- | ----------- |
|
||||||
|
| getSessionId() | String | | Returns the unique identifier of the session. You will need to return this parameter to the client side to pass it during the connection process to the session |
|
||||||
|
| generateToken() | String | _OpenViduRole_ | You can choose which role each user has in a certain session. The value returned is required in the client side just as the sessionId in order to connect to a session. The input parameter can be _OpenViduRole.SUBSCRIBER_, _OpenViduRole.PUBLISHER_ or _OpenViduRole.MODERATOR_. The default value if it is void is _OpenViduRole.PUBLISHER_ |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----------
|
----------
|
||||||
|
|
|
@ -60,48 +60,105 @@ public class OpenVidu {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject createSession() throws IOException, ParseException {
|
public Session createSession() throws OpenViduException {
|
||||||
|
Session s = new Session(myHttpClient, urlOpenViduServer);
|
||||||
HttpResponse response = myHttpClient.execute(new HttpGet(this.urlOpenViduServer + "getSessionId"));
|
return s;
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
|
||||||
System.out.println("Returning a SESSIONID");
|
|
||||||
return this.httpResponseToJsonObject(response);
|
|
||||||
} else {
|
|
||||||
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a sessionID");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject generateToken(String sessionId, String role) throws IOException, ParseException {
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
json.put(0, sessionId);
|
|
||||||
json.put(1, role);
|
|
||||||
|
|
||||||
HttpPost request = new HttpPost(this.urlOpenViduServer + "newToken");
|
|
||||||
StringEntity params = new StringEntity(json.toString());
|
|
||||||
request.addHeader("content-type", "application/json");
|
|
||||||
request.setEntity(params);
|
|
||||||
|
|
||||||
HttpResponse response = myHttpClient.execute(request);
|
public class Session {
|
||||||
|
|
||||||
|
private HttpClient httpClient;
|
||||||
|
private String urlOpenViduServer;
|
||||||
|
private String sessionId;
|
||||||
|
|
||||||
|
private Session(HttpClient httpClient, String urlOpenViduServer) throws OpenViduException {
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
this.urlOpenViduServer = urlOpenViduServer;
|
||||||
|
this.sessionId = this.getSessionId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSessionId() throws OpenViduException {
|
||||||
|
|
||||||
|
if (this.hasSessionId()) {
|
||||||
|
return this.sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpResponse response = httpClient.execute(new HttpGet(this.urlOpenViduServer + "getSessionId"));
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
||||||
|
System.out.println("Returning a SESSIONID");
|
||||||
|
String id = "";
|
||||||
|
id = this.httpResponseToString(response);
|
||||||
|
this.sessionId = id;
|
||||||
|
return id;
|
||||||
|
} else {
|
||||||
|
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new OpenViduException(Code.SESSIONID_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a sessionID: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String generateToken() throws OpenViduException {
|
||||||
|
return this.generateToken(OpenViduRole.PUBLISHER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String generateToken(OpenViduRole role) throws OpenViduException {
|
||||||
|
|
||||||
|
if (!this.hasSessionId()){
|
||||||
|
this.getSessionId();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
json.put(0, this.sessionId);
|
||||||
|
json.put(1, role.name());
|
||||||
|
|
||||||
|
HttpPost request = new HttpPost(this.urlOpenViduServer + "newToken");
|
||||||
|
StringEntity params = new StringEntity(json.toString());
|
||||||
|
request.addHeader("content-type", "application/json");
|
||||||
|
request.setEntity(params);
|
||||||
|
|
||||||
|
HttpResponse response = httpClient.execute(request);
|
||||||
|
|
||||||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
||||||
|
System.out.println("Returning a TOKEN");
|
||||||
|
return this.httpResponseToString(response);
|
||||||
|
} else {
|
||||||
|
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, Integer.toString(statusCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a token: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String httpResponseToString(HttpResponse response) throws IOException, ParseException{
|
||||||
|
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
||||||
|
StringBuffer buf = new StringBuffer();
|
||||||
|
String line = "";
|
||||||
|
while ((line = rd.readLine()) != null) {
|
||||||
|
buf.append(line);
|
||||||
|
}
|
||||||
|
JSONParser parser = new JSONParser();
|
||||||
|
return ((String) ((JSONObject) parser.parse(buf.toString())).get("0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasSessionId() {
|
||||||
|
return (this.sessionId != null && !this.sessionId.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
|
||||||
if ((statusCode == org.apache.http.HttpStatus.SC_OK)){
|
|
||||||
System.out.println("Returning a TOKEN");
|
|
||||||
return this.httpResponseToJsonObject(response);
|
|
||||||
} else {
|
|
||||||
throw new OpenViduException(Code.TOKEN_CANNOT_BE_CREATED_ERROR_CODE, "Unable to generate a token");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject httpResponseToJsonObject(HttpResponse response) throws ParseException, UnsupportedOperationException, IOException{
|
|
||||||
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
String line = "";
|
|
||||||
while ((line = rd.readLine()) != null) {
|
|
||||||
buf.append(line);
|
|
||||||
}
|
|
||||||
JSONParser parser = new JSONParser();
|
|
||||||
return ((JSONObject) parser.parse(buf.toString()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package org.openvidu.client;
|
||||||
|
|
||||||
|
public enum OpenViduRole {
|
||||||
|
SUBSCRIBER,
|
||||||
|
PUBLISHER,
|
||||||
|
MODERATOR;
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.openvidu.client.OpenVidu;
|
import org.openvidu.client.OpenVidu;
|
||||||
|
import org.openvidu.client.OpenVidu.Session;
|
||||||
|
import org.openvidu.client.OpenViduRole;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
@ -33,7 +35,7 @@ public class SessionController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserComponent user;
|
private UserComponent user;
|
||||||
|
|
||||||
private Map<Long, String> lessonIdSessionId = new ConcurrentHashMap<>();
|
private Map<Long, Session> lessonIdSession = new ConcurrentHashMap<>();
|
||||||
private Map<String, Map<Long, String>> sessionIdUserIdToken = new ConcurrentHashMap<>();
|
private Map<String, Map<Long, String>> sessionIdUserIdToken = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final String OPENVIDU_URL = "https://localhost:8443/";
|
private final String OPENVIDU_URL = "https://localhost:8443/";
|
||||||
|
@ -71,16 +73,18 @@ public class SessionController {
|
||||||
|
|
||||||
JSONObject responseJson = new JSONObject();
|
JSONObject responseJson = new JSONObject();
|
||||||
|
|
||||||
if(this.lessonIdSessionId.get(id_lesson) != null) {
|
if(this.lessonIdSession.get(id_lesson) != null) {
|
||||||
// If there's already a valid sessionId for this lesson, not necessary to ask for a new one
|
// If there's already a valid sessionId for this lesson, not necessary to ask for a new one
|
||||||
responseJson.put(0, this.lessonIdSessionId.get(id_lesson));
|
responseJson.put(0, this.lessonIdSession.get(id_lesson).getSessionId());
|
||||||
return new ResponseEntity<>(responseJson, HttpStatus.OK);
|
return new ResponseEntity<>(responseJson, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
JSONObject json = this.openVidu.createSession();
|
|
||||||
String sessionId = (String) json.get("0");
|
Session session = this.openVidu.createSession();
|
||||||
this.lessonIdSessionId.put(id_lesson, sessionId);
|
String sessionId = session.getSessionId();
|
||||||
|
|
||||||
|
this.lessonIdSession.put(id_lesson, session);
|
||||||
this.sessionIdUserIdToken.put(sessionId, new HashMap<>());
|
this.sessionIdUserIdToken.put(sessionId, new HashMap<>());
|
||||||
|
|
||||||
showMap();
|
showMap();
|
||||||
|
@ -115,21 +119,21 @@ public class SessionController {
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.lessonIdSessionId.get(id_lesson) == null){
|
if (this.lessonIdSession.get(id_lesson) == null){
|
||||||
System.out.println("There's no sessionId fot this lesson");
|
System.out.println("There's no Session fot this lesson");
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
String sessionId = this.lessonIdSessionId.get(id_lesson);
|
Session session = this.lessonIdSession.get(id_lesson);
|
||||||
String role = user.hasRoleTeacher() ? "PUBLISHER" : "SUBSCRIBER";
|
OpenViduRole role = user.hasRoleTeacher() ? OpenViduRole.PUBLISHER : OpenViduRole.SUBSCRIBER;
|
||||||
|
|
||||||
JSONObject responseJson = new JSONObject();
|
JSONObject responseJson = new JSONObject();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String token = (String) this.openVidu.generateToken(sessionId, role).get("0");
|
String token = (String) this.lessonIdSession.get(id_lesson).generateToken(role);
|
||||||
this.sessionIdUserIdToken.get(sessionId).put(this.user.getLoggedUser().getId(), token);
|
this.sessionIdUserIdToken.get(session.getSessionId()).put(this.user.getLoggedUser().getId(), token);
|
||||||
|
|
||||||
responseJson.put(0, sessionId);
|
responseJson.put(0, session.getSessionId());
|
||||||
responseJson.put(1, token);
|
responseJson.put(1, token);
|
||||||
|
|
||||||
showMap();
|
showMap();
|
||||||
|
@ -161,16 +165,16 @@ public class SessionController {
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.lessonIdSessionId.get(id_lesson) == null){
|
if (this.lessonIdSession.get(id_lesson) == null){
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
String sessionId = this.lessonIdSessionId.get(id_lesson);
|
String sessionId = this.lessonIdSession.get(id_lesson).getSessionId();
|
||||||
if (this.sessionIdUserIdToken.get(sessionId).remove(this.user.getLoggedUser().getId()) != null){
|
if (this.sessionIdUserIdToken.get(sessionId).remove(this.user.getLoggedUser().getId()) != null){
|
||||||
// This user has left the lesson
|
// This user has left the lesson
|
||||||
if(this.sessionIdUserIdToken.get(sessionId).isEmpty()){
|
if(this.sessionIdUserIdToken.get(sessionId).isEmpty()){
|
||||||
// The last user has left the lesson
|
// The last user has left the lesson
|
||||||
this.lessonIdSessionId.remove(id_lesson);
|
this.lessonIdSession.remove(id_lesson);
|
||||||
this.sessionIdUserIdToken.remove(sessionId);
|
this.sessionIdUserIdToken.remove(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,7 +190,7 @@ public class SessionController {
|
||||||
|
|
||||||
private void showMap(){
|
private void showMap(){
|
||||||
System.out.println("------------------------------");
|
System.out.println("------------------------------");
|
||||||
System.out.println(this.lessonIdSessionId.toString());
|
System.out.println(this.lessonIdSession.toString());
|
||||||
System.out.println(this.sessionIdUserIdToken.toString());
|
System.out.println(this.sessionIdUserIdToken.toString());
|
||||||
System.out.println("------------------------------");
|
System.out.println("------------------------------");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue