Updated tutorials with new OpenVidu API

WIP: Test the tutorials which use Java or Node client
This commit is contained in:
csantosm 2020-11-10 17:08:18 +01:00
parent 9e3b0265e6
commit e5166450d0
23 changed files with 187 additions and 176 deletions

View File

@ -137,15 +137,15 @@ public class SessionActivity extends AppCompatActivity {
try { try {
// Session Request // Session Request
RequestBody sessionBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"customSessionId\": \"" + sessionId + "\"}"); RequestBody sessionBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"customSessionId\": \"" + sessionId + "\"}");
this.httpClient.httpCall("/api/sessions", "POST", "application/json", sessionBody, new Callback() { this.httpClient.httpCall("/openvidu/api/sessions", "POST", "application/json", sessionBody, new Callback() {
@Override @Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.d(TAG, "responseString: " + response.body().string()); Log.d(TAG, "responseString: " + response.body().string());
// Token Request // Token Request
RequestBody tokenBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"session\": \"" + sessionId + "\"}"); RequestBody tokenBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{}");
httpClient.httpCall("/api/tokens", "POST", "application/json", tokenBody, new Callback() { httpClient.httpCall("/openvidu/api/sessions/" + sessionId + "/connection", "POST", "application/json", tokenBody, new Callback() {
@Override @Override
public void onResponse(@NotNull Call call, @NotNull Response response) { public void onResponse(@NotNull Call call, @NotNull Response response) {

View File

@ -82,7 +82,8 @@ public class CustomHttpClient {
url = url.startsWith("/") ? url.substring(1) : url; url = url.startsWith("/") ? url.substring(1) : url;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(this.baseUrl + url) .url(this.baseUrl + url)
.header("Authorization", this.basicAuth).header("Content-Type", contentType).method(method, body) .header("Authorization", this.basicAuth).header("Content-Type", contentType)
.method(method, body)
.build(); .build();
Call call = client.newCall(request); Call call = client.newCall(request);
call.enqueue(callback); call.enqueue(callback);

View File

@ -91,9 +91,9 @@ function openScreenShareModal() {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://localhost:4443"; var OPENVIDU_SERVER_URL = "https://localhost:4443";
@ -103,10 +103,10 @@ function getToken(mySessionId) {
return createSession(mySessionId).then(sessionId => createToken(sessionId)); return createSession(mySessionId).then(sessionId => createToken(sessionId));
} }
function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.post( axios.post(
OPENVIDU_SERVER_URL + "/api/sessions", OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
JSON.stringify({ JSON.stringify({
customSessionId: sessionId customSessionId: sessionId
}), { }), {
@ -138,13 +138,11 @@ function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/re
}); });
} }
function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.post( axios.post(
OPENVIDU_SERVER_URL + "/api/tokens", OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
JSON.stringify({ {
session: sessionId
}), {
headers: { headers: {
'Authorization': "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), 'Authorization': "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@ -91,7 +91,7 @@ function joinSession() {
publishVideo: true, // Whether you want to start publishing with your video enabled or not publishVideo: true, // Whether you want to start publishing with your video enabled or not
resolution: '1280x720', // The resolution of your video resolution: '1280x720', // The resolution of your video
frameRate: 30, // The frame rate of your video frameRate: 30, // The frame rate of your video
insertMode: 'APPEND', // How the video is inserted in the target element 'video-container' insertMode: 'APPEND', // How the video is inserted in the target element 'video-container'
mirror: false // Whether to mirror your local video or not mirror: false // Whether to mirror your local video or not
}; };
@ -161,7 +161,7 @@ function leaveSession() {
session.disconnect(); session.disconnect();
// Removing all HTML elements with user's nicknames. // Removing all HTML elements with user's nicknames.
// HTML videos are automatically removed when leaving a Session // HTML videos are automatically removed when leaving a Session
removeAllUserData(); removeAllUserData();
@ -369,9 +369,9 @@ function initMainVideo(streamManager, userData) {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the REST API, openvidu-java-client or openvidu-node-client): * the REST API, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443"; var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443";
@ -382,11 +382,11 @@ function getToken(mySessionId, role) {
} }
function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/sessions", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
data: JSON.stringify({ customSessionId: sessionId }), data: JSON.stringify({ customSessionId: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
@ -409,10 +409,9 @@ function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/re
} }
function createToken(sessionId, role) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId, role) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
var openviduRole; var openviduRole;
var jsonBody = { var jsonBody = {
session: sessionId,
role: role, role: role,
kurentoOptions: {} kurentoOptions: {}
}; };
@ -427,7 +426,7 @@ function createToken(sessionId, role) { // See https://docs.openvidu.io/en/stabl
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/tokens", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
data: JSON.stringify(jsonBody), data: JSON.stringify(jsonBody),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),

View File

@ -173,7 +173,7 @@ function showSessionHideJoin() {
$('#main-container').removeClass('container'); $('#main-container').removeClass('container');
} }
// 'Join' page // 'Join' page
function showJoinHideSession() { function showJoinHideSession() {
$('#nav-join').show(); $('#nav-join').show();
$('#nav-session').hide(); $('#nav-session').hide();
@ -261,9 +261,9 @@ function updateLayout() {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443"; var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443";
@ -273,12 +273,12 @@ function getToken(mySessionId) {
return createSession(mySessionId).then(sId => createToken(sId)); return createSession(mySessionId).then(sId => createToken(sId));
} }
function createSession(sId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/sessions", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
data: JSON.stringify({ customSessionId: sId }), data: JSON.stringify({ customSessionId: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
"Content-Type": "application/json" "Content-Type": "application/json"
@ -286,7 +286,7 @@ function createSession(sId) { // See https://docs.openvidu.io/en/stable/referenc
success: response => resolve(response.id), success: response => resolve(response.id),
error: (error) => { error: (error) => {
if (error.status === 409) { if (error.status === 409) {
resolve(sId); resolve(sessionId);
} else { } else {
console.warn('No connection to OpenVidu Server. This may be a certificate error at ' + OPENVIDU_SERVER_URL); console.warn('No connection to OpenVidu Server. This may be a certificate error at ' + OPENVIDU_SERVER_URL);
if (window.confirm('No connection to OpenVidu Server. This may be a certificate error at \"' + OPENVIDU_SERVER_URL + '\"\n\nClick OK to navigate and accept it. ' + if (window.confirm('No connection to OpenVidu Server. This may be a certificate error at \"' + OPENVIDU_SERVER_URL + '\"\n\nClick OK to navigate and accept it. ' +
@ -299,12 +299,12 @@ function createSession(sId) { // See https://docs.openvidu.io/en/stable/referenc
}); });
} }
function createToken(sId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/tokens", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
data: JSON.stringify({ session: sId }), data: JSON.stringify({}),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -48,9 +48,9 @@ window.onbeforeunload = function () {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443"; var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443";
@ -60,11 +60,11 @@ function getToken(mySessionId) {
return createSession(mySessionId).then(sessionId => createToken(sessionId)); return createSession(mySessionId).then(sessionId => createToken(sessionId));
} }
function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/sessions", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
data: JSON.stringify({ customSessionId: sessionId }), data: JSON.stringify({ customSessionId: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
@ -86,12 +86,12 @@ function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/re
}); });
} }
function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/tokens", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
data: JSON.stringify({ session: sessionId }), data: JSON.stringify({}),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -153,9 +153,9 @@ export class AppComponent implements OnDestroy {
* This method retrieve the mandatory user token from OpenVidu Server, * This method retrieve the mandatory user token from OpenVidu Server,
* in this case making use Angular http API. * in this case making use Angular http API.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case: * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case:
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method of OpenVidu Browser * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken(): Promise<string> { getToken(): Promise<string> {
@ -175,7 +175,7 @@ export class AppComponent implements OnDestroy {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}) })
}; };
return this.httpClient.post(this.OPENVIDU_SERVER_URL + '/api/sessions', body, options) return this.httpClient.post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions', body, options)
.pipe( .pipe(
catchError(error => { catchError(error => {
if (error.status === 409) { if (error.status === 409) {
@ -201,14 +201,14 @@ export class AppComponent implements OnDestroy {
createToken(sessionId): Promise<string> { createToken(sessionId): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const body = JSON.stringify({ session: sessionId }); const body = {};
const options = { const options = {
headers: new HttpHeaders({ headers: new HttpHeaders({
'Authorization': 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET), 'Authorization': 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}) })
}; };
return this.httpClient.post(this.OPENVIDU_SERVER_URL + '/api/tokens', body, options) return this.httpClient.post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection', body, options)
.pipe( .pipe(
catchError(error => { catchError(error => {
reject(error); reject(error);

View File

@ -181,9 +181,9 @@ function initMainVideo(videoElement, userData) {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443"; var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443";
@ -193,11 +193,11 @@ function getToken(mySessionId) {
return createSession(mySessionId).then(sessionId => createToken(sessionId)); return createSession(mySessionId).then(sessionId => createToken(sessionId));
} }
function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/sessions", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
data: JSON.stringify({ customSessionId: sessionId }), data: JSON.stringify({ customSessionId: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
@ -219,12 +219,11 @@ function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/re
}); });
} }
function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/tokens", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
data: JSON.stringify({ session: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -266,9 +266,9 @@ class App extends Component {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken() { getToken() {
@ -279,7 +279,7 @@ class App extends Component {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var data = JSON.stringify({ customSessionId: sessionId }); var data = JSON.stringify({ customSessionId: sessionId });
axios axios
.post(OPENVIDU_SERVER_URL + '/api/sessions', data, { .post(OPENVIDU_SERVER_URL + '/openvidu/api/sessions', data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -318,9 +318,9 @@ class App extends Component {
createToken(sessionId) { createToken(sessionId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var data = JSON.stringify({ session: sessionId }); var data = {};
axios axios
.post(OPENVIDU_SERVER_URL + '/api/tokens', data, { .post(OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection", data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@ -151,20 +151,20 @@ export default {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken (mySessionId) { getToken (mySessionId) {
return this.createSession(mySessionId).then(sessionId => this.createToken(sessionId)); return this.createSession(mySessionId).then(sessionId => this.createToken(sessionId));
}, },
// See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
createSession (sessionId) { createSession (sessionId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(`${OPENVIDU_SERVER_URL}/api/sessions`, JSON.stringify({ .post(`${OPENVIDU_SERVER_URL}/openvidu/api/sessions`, JSON.stringify({
customSessionId: sessionId, customSessionId: sessionId,
}), { }), {
auth: { auth: {
@ -188,13 +188,11 @@ export default {
}); });
}, },
// See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
createToken (sessionId) { createToken (sessionId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(`${OPENVIDU_SERVER_URL}/api/tokens`, JSON.stringify({ .post(`${OPENVIDU_SERVER_URL}/openvidu/api/sessions/${sessionId}/connection`, {}, {
session: sessionId,
}), {
auth: { auth: {
username: 'OPENVIDUAPP', username: 'OPENVIDUAPP',
password: OPENVIDU_SERVER_SECRET, password: OPENVIDU_SERVER_SECRET,

View File

@ -69,7 +69,7 @@ function joinSession() {
publishVideo: true, // Whether you want to start publishing with your video enabled or not publishVideo: true, // Whether you want to start publishing with your video enabled or not
resolution: '640x480', // The resolution of your video resolution: '640x480', // The resolution of your video
frameRate: 30, // The frame rate of your video frameRate: 30, // The frame rate of your video
insertMode: 'APPEND', // How the video is inserted in the target element 'video-container' insertMode: 'APPEND', // How the video is inserted in the target element 'video-container'
mirror: false // Whether to mirror your local video or not mirror: false // Whether to mirror your local video or not
}); });
@ -105,7 +105,7 @@ function leaveSession() {
session.disconnect(); session.disconnect();
// Removing all HTML elements with user's nicknames. // Removing all HTML elements with user's nicknames.
// HTML videos are automatically removed when leaving a Session // HTML videos are automatically removed when leaving a Session
removeAllUserData(); removeAllUserData();
@ -206,9 +206,9 @@ function initMainVideo(publisher, userData) {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443"; var OPENVIDU_SERVER_URL = "https://" + location.hostname + ":4443";
@ -218,11 +218,11 @@ function getToken(mySessionId) {
return createSession(mySessionId).then(function(sessionId) { return createToken(sessionId); }); return createSession(mySessionId).then(function(sessionId) { return createToken(sessionId); });
} }
function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/sessions", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions",
data: JSON.stringify({ customSessionId: sessionId }), data: JSON.stringify({ customSessionId: sessionId }),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
@ -244,12 +244,12 @@ function createSession(sessionId) { // See https://docs.openvidu.io/en/stable/re
}); });
} }
function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens function createToken(sessionId) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: OPENVIDU_SERVER_URL + "/api/tokens", url: OPENVIDU_SERVER_URL + "/openvidu/api/sessions/" + sessionId + "/connection",
data: JSON.stringify({ session: sessionId }), data: JSON.stringify({}),
headers: { headers: {
"Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET), "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
"Content-Type": "application/json" "Content-Type": "application/json"

View File

@ -283,9 +283,9 @@ export class AppComponent implements OnDestroy {
* This method retrieve the mandatory user token from OpenVidu Server, * This method retrieve the mandatory user token from OpenVidu Server,
* in this case making use Angular http API. * in this case making use Angular http API.
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case: * This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case:
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method of OpenVidu Browser * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken(): Promise<string> { getToken(): Promise<string> {
@ -308,7 +308,7 @@ export class AppComponent implements OnDestroy {
}), }),
}; };
return this.httpClient return this.httpClient
.post(this.OPENVIDU_SERVER_URL + '/api/sessions', body, options) .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions', body, options)
.pipe( .pipe(
catchError((error) => { catchError((error) => {
if (error.status === 409) { if (error.status === 409) {
@ -344,7 +344,7 @@ export class AppComponent implements OnDestroy {
createToken(sessionId): Promise<string> { createToken(sessionId): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const body = JSON.stringify({ session: sessionId }); const body = JSON.stringify({});
const options = { const options = {
headers: new HttpHeaders({ headers: new HttpHeaders({
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET),
@ -352,7 +352,7 @@ export class AppComponent implements OnDestroy {
}), }),
}; };
return this.httpClient return this.httpClient
.post(this.OPENVIDU_SERVER_URL + '/api/tokens', body, options) .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection', body, options)
.pipe( .pipe(
catchError((error) => { catchError((error) => {
reject(error); reject(error);

View File

@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import io.openvidu.java.client.OpenVidu; import io.openvidu.java.client.OpenVidu;
import io.openvidu.java.client.OpenViduHttpException; import io.openvidu.java.client.OpenViduHttpException;
import io.openvidu.java.client.OpenViduJavaClientException; import io.openvidu.java.client.OpenViduJavaClientException;
import io.openvidu.java.client.OpenViduRole;
import io.openvidu.java.client.ConnectionType;
import io.openvidu.java.client.Session; import io.openvidu.java.client.Session;
import io.openvidu.java.client.SessionProperties; import io.openvidu.java.client.SessionProperties;
@ -69,8 +71,13 @@ public class MyRestController {
// Generate a token for the user // Generate a token for the user
String token = null; String token = null;
ConnectionProperties connectionProperties = new ConnectionProperties.Builder()
.type(ConnectionType.WEBRTC)
.role(OpenViduRole.PUBLISHER)
.data("user_data")
.build();
try { try {
token = this.session.generateToken(); token = this.session.createConnection(connectionProperties).getToken();
} catch (OpenViduHttpException e) { } catch (OpenViduHttpException e) {
if (e.getStatus() == 404) { if (e.getStatus() == 404) {
// Session was closed in openvidu-server. Create it again // Session was closed in openvidu-server. Create it again

View File

@ -21,7 +21,7 @@ import io.openvidu.java.client.OpenViduHttpException;
import io.openvidu.java.client.OpenViduJavaClientException; import io.openvidu.java.client.OpenViduJavaClientException;
import io.openvidu.java.client.OpenViduRole; import io.openvidu.java.client.OpenViduRole;
import io.openvidu.java.client.Session; import io.openvidu.java.client.Session;
import io.openvidu.java.client.TokenOptions; import io.openvidu.java.client.ConnectionProperties;
@RestController @RestController
@RequestMapping("/api-sessions") @RequestMapping("/api-sessions")
@ -71,8 +71,8 @@ public class SessionController {
// object on login // object on login
String serverData = "{\"serverData\": \"" + httpSession.getAttribute("loggedUser") + "\"}"; String serverData = "{\"serverData\": \"" + httpSession.getAttribute("loggedUser") + "\"}";
// Build tokenOptions object with the serverData and the role // Build connectionProperties object with the serverData and the role
TokenOptions tokenOptions = new TokenOptions.Builder().data(serverData).role(role).build(); ConnectionProperties connectionProperties = new ConnectionProperties.Builder().type(ConnectionType.WEBRTC).data(serverData).role(role).build();
JSONObject responseJson = new JSONObject(); JSONObject responseJson = new JSONObject();
@ -81,8 +81,8 @@ public class SessionController {
System.out.println("Existing session " + sessionName); System.out.println("Existing session " + sessionName);
try { try {
// Generate a new token with the recently created tokenOptions // Generate a new Connection with the recently created connectionProperties
String token = this.mapSessions.get(sessionName).generateToken(tokenOptions); String token = this.mapSessions.get(sessionName).createConnection(connectionProperties).getToken();
// Update our collection storing the new token // Update our collection storing the new token
this.mapSessionNamesTokens.get(sessionName).put(token, role); this.mapSessionNamesTokens.get(sessionName).put(token, role);
@ -111,8 +111,8 @@ public class SessionController {
// Create a new OpenVidu Session // Create a new OpenVidu Session
Session session = this.openVidu.createSession(); Session session = this.openVidu.createSession();
// Generate a new token with the recently created tokenOptions // Generate a new Connection with the recently created connectionProperties
String token = session.generateToken(tokenOptions); String token = this.mapSessions.get(sessionName).createConnection(connectionProperties).getToken();
// Store the session and the token in our collections // Store the session and the token in our collections
this.mapSessions.put(sessionName, session); this.mapSessions.put(sessionName, session);

View File

@ -86,7 +86,7 @@ app.post('/api-login/login', function (req, res) {
console.log("Logging in | {user, pass}={" + user + ", " + pass + "}"); console.log("Logging in | {user, pass}={" + user + ", " + pass + "}");
if (login(user, pass)) { // Correct user-pass if (login(user, pass)) { // Correct user-pass
// Validate session and return OK // Validate session and return OK
// Value stored in req.session allows us to identify the user in future requests // Value stored in req.session allows us to identify the user in future requests
console.log("'" + user + "' has logged in"); console.log("'" + user + "' has logged in");
req.session.loggedUser = user; req.session.loggedUser = user;
@ -124,8 +124,8 @@ app.post('/api-sessions/get-token', function (req, res) {
console.log("Getting a token | {sessionName}={" + sessionName + "}"); console.log("Getting a token | {sessionName}={" + sessionName + "}");
// Build tokenOptions object with the serverData and the role // Build connectionProperties object with the serverData and the role
var tokenOptions = { var connectionProperties = {
data: serverData, data: serverData,
role: role role: role
}; };
@ -137,16 +137,16 @@ app.post('/api-sessions/get-token', function (req, res) {
// Get the existing Session from the collection // Get the existing Session from the collection
var mySession = mapSessions[sessionName]; var mySession = mapSessions[sessionName];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new token asynchronously with the recently created connectionProperties
mySession.generateToken(tokenOptions) mySession.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return the token to the client // Return the token to the client
res.status(200).send({ res.status(200).send({
0: token 0: connection.token
}); });
}) })
.catch(error => { .catch(error => {
@ -164,16 +164,16 @@ app.post('/api-sessions/get-token', function (req, res) {
// Store a new empty array in the collection of tokens // Store a new empty array in the collection of tokens
mapSessionNamesTokens[sessionName] = []; mapSessionNamesTokens[sessionName] = [];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new connection asynchronously with the recently created connectionProperties
session.generateToken(tokenOptions) session.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return the Token to the client // Return the Token to the client
res.status(200).send({ res.status(200).send({
0: token 0: connection.token
}); });
}) })
.catch(error => { .catch(error => {

View File

@ -88,9 +88,9 @@ export class AppComponent {
* This method retrieve the mandatory user token from OpenVidu Server, * This method retrieve the mandatory user token from OpenVidu Server,
* in this case making use Angular http API. * in this case making use Angular http API.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case: * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case:
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method of OpenVidu Browser * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken(): Promise<string> { getToken(): Promise<string> {
@ -109,7 +109,7 @@ export class AppComponent {
}), }),
}; };
return this.httpClient return this.httpClient
.post(this.OPENVIDU_SERVER_URL + '/api/sessions', body, options) .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions', body, options)
.pipe( .pipe(
catchError((error) => { catchError((error) => {
if (error.status === 409) { if (error.status === 409) {
@ -141,7 +141,7 @@ export class AppComponent {
createToken(sessionId): Promise<string> { createToken(sessionId): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const body = JSON.stringify({ session: sessionId }); const body = JSON.stringify({});
const options = { const options = {
headers: new HttpHeaders({ headers: new HttpHeaders({
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET),
@ -149,7 +149,7 @@ export class AppComponent {
}), }),
}; };
return this.httpClient return this.httpClient
.post(this.OPENVIDU_SERVER_URL + '/api/tokens', body, options) .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection', body, options)
.pipe( .pipe(
catchError((error) => { catchError((error) => {
reject(error); reject(error);

View File

@ -122,9 +122,9 @@ class App extends Component {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken() { getToken() {
@ -137,7 +137,7 @@ class App extends Component {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var data = JSON.stringify({ customSessionId: sessionId }); var data = JSON.stringify({ customSessionId: sessionId });
axios axios
.post(this.OPENVIDU_SERVER_URL + '/api/sessions', data, { .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions', data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -175,9 +175,9 @@ class App extends Component {
createToken(sessionId) { createToken(sessionId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var data = JSON.stringify({ session: sessionId }); var data = JSON.stringify({});
axios axios
.post(this.OPENVIDU_SERVER_URL + '/api/tokens', data, { .post(this.OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection', data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + this.OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import io.openvidu.java.client.OpenVidu; import io.openvidu.java.client.OpenVidu;
import io.openvidu.java.client.OpenViduRole; import io.openvidu.java.client.OpenViduRole;
import io.openvidu.java.client.Session; import io.openvidu.java.client.Session;
import io.openvidu.java.client.TokenOptions; import io.openvidu.java.client.ConnectionProperties;
@Controller @Controller
public class SessionController { public class SessionController {
@ -57,16 +57,20 @@ public class SessionController {
// In this case, a JSON with the value we stored in the HttpSession object on login // In this case, a JSON with the value we stored in the HttpSession object on login
String serverData = "{\"serverData\": \"" + httpSession.getAttribute("loggedUser") + "\"}"; String serverData = "{\"serverData\": \"" + httpSession.getAttribute("loggedUser") + "\"}";
// Build tokenOptions object with the serverData and the role // Build connectionProperties object with the serverData and the role
TokenOptions tokenOptions = new TokenOptions.Builder().data(serverData).role(role).build(); ConnectionProperties connectionProperties = new ConnectionProperties.Builder()
.type(ConnectionType.WEBRTC)
.role(role)
.data(serverData)
.build();
if (this.mapSessions.get(sessionName) != null) { if (this.mapSessions.get(sessionName) != null) {
// Session already exists // Session already exists
System.out.println("Existing session " + sessionName); System.out.println("Existing session " + sessionName);
try { try {
// Generate a new token with the recently created tokenOptions // Generate a new token with the recently created connectionProperties
String token = this.mapSessions.get(sessionName).generateToken(tokenOptions); String token = this.mapSessions.get(sessionName).createConnection(connectionProperties).getToken();
// Update our collection storing the new token // Update our collection storing the new token
this.mapSessionNamesTokens.get(sessionName).put(token, role); this.mapSessionNamesTokens.get(sessionName).put(token, role);
@ -92,8 +96,8 @@ public class SessionController {
// Create a new OpenVidu Session // Create a new OpenVidu Session
Session session = this.openVidu.createSession(); Session session = this.openVidu.createSession();
// Generate a new token with the recently created tokenOptions // Generate a new token with the recently created connectionProperties
String token = session.generateToken(tokenOptions); String token = session.createConnection(connectionProperties).getToken();
// Store the session and the token in our collections // Store the session and the token in our collections
this.mapSessions.put(sessionName, session); this.mapSessions.put(sessionName, session);

View File

@ -117,7 +117,7 @@ function dashboardController(req, res) {
console.log("Logging in | {user, pass}={" + user + ", " + pass + "}"); console.log("Logging in | {user, pass}={" + user + ", " + pass + "}");
if (login(user, pass)) { // Correct user-pass if (login(user, pass)) { // Correct user-pass
// Validate session and return OK // Validate session and return OK
// Value stored in req.session allows us to identify the user in future requests // Value stored in req.session allows us to identify the user in future requests
console.log("'" + user + "' has logged in"); console.log("'" + user + "' has logged in");
req.session.loggedUser = user; req.session.loggedUser = user;
@ -152,8 +152,8 @@ app.post('/session', (req, res) => {
console.log("Getting a token | {sessionName}={" + sessionName + "}"); console.log("Getting a token | {sessionName}={" + sessionName + "}");
// Build tokenOptions object with the serverData and the role // Build connectionProperties object with the serverData and the role
var tokenOptions = { var connectionProperties = {
data: serverData, data: serverData,
role: role role: role
}; };
@ -165,17 +165,17 @@ app.post('/session', (req, res) => {
// Get the existing Session from the collection // Get the existing Session from the collection
var mySession = mapSessions[sessionName]; var mySession = mapSessions[sessionName];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new token asynchronously with the recently created connectionProperties
mySession.generateToken(tokenOptions) mySession.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return session template with all the needed attributes // Return session template with all the needed attributes
res.render('session.ejs', { res.render('session.ejs', {
sessionId: mySession.getSessionId(), sessionId: mySession.getSessionId(),
token: token, token: connection.token,
nickName: clientData, nickName: clientData,
userName: req.session.loggedUser, userName: req.session.loggedUser,
sessionName: sessionName sessionName: sessionName
@ -196,17 +196,17 @@ app.post('/session', (req, res) => {
// Store a new empty array in the collection of tokens // Store a new empty array in the collection of tokens
mapSessionNamesTokens[sessionName] = []; mapSessionNamesTokens[sessionName] = [];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new token asynchronously with the recently created connectionProperties
session.generateToken(tokenOptions) session.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return session template with all the needed attributes // Return session template with all the needed attributes
res.render('session.ejs', { res.render('session.ejs', {
sessionName: sessionName, sessionName: sessionName,
token: token, token: connection.token,
nickName: clientData, nickName: clientData,
userName: req.session.loggedUser, userName: req.session.loggedUser,
}); });

View File

@ -379,9 +379,9 @@ export default class App extends Component<Props> {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) The token must be consumed in Session.connect() method * 3) The Connection.token must be consumed in Session.connect() method
*/ */
getToken() { getToken() {
@ -394,7 +394,7 @@ export default class App extends Component<Props> {
return new Promise((resolve) => { return new Promise((resolve) => {
var data = JSON.stringify({ customSessionId: sessionId }); var data = JSON.stringify({ customSessionId: sessionId });
axios axios
.post(OPENVIDU_SERVER_URL + '/api/sessions', data, { .post(OPENVIDU_SERVER_URL + '/openvidu/api/sessions', data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -453,9 +453,9 @@ export default class App extends Component<Props> {
createToken(sessionId) { createToken(sessionId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var data = JSON.stringify({ session: sessionId }); var data = JSON.stringify({});
axios axios
.post(OPENVIDU_SERVER_URL + '/api/tokens', data, { .post(OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection', data, {
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@ -21,10 +21,11 @@ import io.openvidu.java.client.OpenVidu;
import io.openvidu.java.client.OpenViduHttpException; import io.openvidu.java.client.OpenViduHttpException;
import io.openvidu.java.client.OpenViduJavaClientException; import io.openvidu.java.client.OpenViduJavaClientException;
import io.openvidu.java.client.OpenViduRole; import io.openvidu.java.client.OpenViduRole;
import io.openvidu.java.client.ConnectionType;
import io.openvidu.java.client.Recording; import io.openvidu.java.client.Recording;
import io.openvidu.java.client.RecordingProperties; import io.openvidu.java.client.RecordingProperties;
import io.openvidu.java.client.Session; import io.openvidu.java.client.Session;
import io.openvidu.java.client.TokenOptions; import io.openvidu.java.client.ConnectionProperties;
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
@ -69,8 +70,12 @@ public class MyRestController {
// Role associated to this user // Role associated to this user
OpenViduRole role = OpenViduRole.PUBLISHER; OpenViduRole role = OpenViduRole.PUBLISHER;
// Build tokenOptions object with the serverData and the role // Build connectionProperties object with the serverData and the role
TokenOptions tokenOptions = new TokenOptions.Builder().role(role).build(); ConnectionProperties connectionProperties = new ConnectionProperties.Builder()
.type(ConnectionType.WEBRTC)
.role(role)
.data("user_data")
.build();
JSONObject responseJson = new JSONObject(); JSONObject responseJson = new JSONObject();
@ -79,8 +84,8 @@ public class MyRestController {
System.out.println("Existing session " + sessionName); System.out.println("Existing session " + sessionName);
try { try {
// Generate a new token with the recently created tokenOptions // Generate a new token with the recently created connectionProperties
String token = this.mapSessions.get(sessionName).generateToken(tokenOptions); String token = this.mapSessions.get(sessionName).createConnection(connectionProperties).getToken();
// Update our collection storing the new token // Update our collection storing the new token
this.mapSessionNamesTokens.get(sessionName).put(token, role); this.mapSessionNamesTokens.get(sessionName).put(token, role);
@ -111,8 +116,8 @@ public class MyRestController {
// Create a new OpenVidu Session // Create a new OpenVidu Session
Session session = this.openVidu.createSession();// new Session session = this.openVidu.createSession();// new
// SessionProperties.Builder().customSessionId("CUSTOMSESSIONID").defaultRecordingLayout(RecordingLayout.CUSTOM).defaultCustomLayout("CUSTOM/LAYOUT").recordingMode(RecordingMode.ALWAYS).build()); // SessionProperties.Builder().customSessionId("CUSTOMSESSIONID").defaultRecordingLayout(RecordingLayout.CUSTOM).defaultCustomLayout("CUSTOM/LAYOUT").recordingMode(RecordingMode.ALWAYS).build());
// Generate a new token with the recently created tokenOptions // Generate a new token with the recently created connectionProperties
String token = session.generateToken(tokenOptions); String token = session.createConnection(connectionProperties).getToken();
// Store the session and the token in our collections // Store the session and the token in our collections
this.mapSessions.put(sessionName, session); this.mapSessions.put(sessionName, session);

View File

@ -65,8 +65,8 @@ app.post('/api/get-token', function (req, res) {
console.log("Getting a token | {sessionName}={" + sessionName + "}"); console.log("Getting a token | {sessionName}={" + sessionName + "}");
// Build tokenOptions object with PUBLISHER role // Build connectionProperties object with PUBLISHER role
var tokenOptions = { var connectionProperties = {
role: role role: role
} }
@ -77,16 +77,16 @@ app.post('/api/get-token', function (req, res) {
// Get the existing Session from the collection // Get the existing Session from the collection
var mySession = mapSessions[sessionName]; var mySession = mapSessions[sessionName];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new Connection asynchronously with the recently created connectionProperties
mySession.generateToken(tokenOptions) mySession.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return the token to the client // Return the token to the client
res.status(200).send({ res.status(200).send({
0: token 0: connection.token
}); });
}) })
.catch(error => { .catch(error => {
@ -94,15 +94,15 @@ app.post('/api/get-token', function (req, res) {
if (error.message === "404") { if (error.message === "404") {
delete mapSessions[sessionName]; delete mapSessions[sessionName];
delete mapSessionNamesTokens[sessionName]; delete mapSessionNamesTokens[sessionName];
newSession(sessionName, tokenOptions, res); newSession(sessionName, connectionProperties, res);
} }
}); });
} else { } else {
newSession(sessionName, tokenOptions, res); newSession(sessionName, connectionProperties, res);
} }
}); });
function newSession(sessionName, tokenOptions, res) { function newSession(sessionName, connectionProperties, res) {
// New session // New session
console.log('New session ' + sessionName); console.log('New session ' + sessionName);
@ -114,16 +114,16 @@ function newSession(sessionName, tokenOptions, res) {
// Store a new empty array in the collection of tokens // Store a new empty array in the collection of tokens
mapSessionNamesTokens[sessionName] = []; mapSessionNamesTokens[sessionName] = [];
// Generate a new token asynchronously with the recently created tokenOptions // Generate a new connection asynchronously with the recently created connectionProperties
session.generateToken(tokenOptions) session.createConnection(connectionProperties)
.then(token => { .then(connection => {
// Store the new token in the collection of tokens // Store the new token in the collection of tokens
mapSessionNamesTokens[sessionName].push(token); mapSessionNamesTokens[sessionName].push(connection.token);
// Return the Token to the client // Return the Token to the client
res.status(200).send({ res.status(200).send({
0: token 0: connection.token
}); });
}) })
.catch(error => { .catch(error => {

View File

@ -81,9 +81,9 @@ async function joinSession() {
* These methods retrieve the mandatory user token from OpenVidu Server. * These methods retrieve the mandatory user token from OpenVidu Server.
* This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using * This behavior MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
* the API REST, openvidu-java-client or openvidu-node-client): * the API REST, openvidu-java-client or openvidu-node-client):
* 1) Initialize a session in OpenVidu Server (POST /api/sessions) * 1) Initialize a session in OpenVidu Server (POST /openvidu/api/sessions)
* 2) Generate a token in OpenVidu Server (POST /api/tokens) * 2) Generate a Connection in OpenVidu Server (POST /openvidu/api/sessions/<SESSION_ID>/connection)
* 3) Configure OpenVidu Web Component in your client side with the token * 3) The Connection.token must be consumed in Session.connect() method
*/ */
var OPENVIDU_SERVER_URL = "https://demos.openvidu.io"; var OPENVIDU_SERVER_URL = "https://demos.openvidu.io";
@ -93,11 +93,11 @@ function getToken(sessionName) {
return createSession(sessionName).then((sessionId) => createToken(sessionId)); return createSession(sessionName).then((sessionId) => createToken(sessionId));
} }
function createSession(sessionName) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apisessions function createSession(sessionName) { // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessions
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: OPENVIDU_SERVER_URL + '/api/sessions', url: OPENVIDU_SERVER_URL + '/openvidu/api/sessions',
data: JSON.stringify({ customSessionId: sessionName }), data: JSON.stringify({ customSessionId: sessionName }),
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),
@ -128,11 +128,11 @@ function createSession(sessionName) { // See https://docs.openvidu.io/en/stable/
} }
function createToken(sessionId) { function createToken(sessionId) {
// See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-apitokens // See https://docs.openvidu.io/en/stable/reference-docs/REST-API/#post-openviduapisessionsltsession_idgtconnection
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: OPENVIDU_SERVER_URL + '/api/tokens', url: OPENVIDU_SERVER_URL + '/openvidu/api/sessions/' + sessionId + '/connection',
data: JSON.stringify({ session: sessionId }), data: JSON.stringify({ session: sessionId }),
headers: { headers: {
Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET), Authorization: 'Basic ' + btoa('OPENVIDUAPP:' + OPENVIDU_SERVER_SECRET),