Insecure tutorials updated to 2.0.0
This commit is contained in:
parent
040e904e1c
commit
0443e22aa3
@ -34,14 +34,16 @@ function joinRoom() {
|
||||
sessionId = randomString();
|
||||
}
|
||||
|
||||
|
||||
// --- 1) Get an OpenVidu object and init a session ---
|
||||
// --- 1) Get an OpenVidu object ---
|
||||
|
||||
OV = new OpenVidu();
|
||||
|
||||
// --- 2) Init a session ---
|
||||
|
||||
session = OV.initSession();
|
||||
|
||||
|
||||
// --- 2) Specify the actions when events take place ---
|
||||
// --- 3) Specify the actions when events take place in the session ---
|
||||
|
||||
// On every new Stream received...
|
||||
session.on('streamCreated', function (event) {
|
||||
@ -62,58 +64,64 @@ function joinRoom() {
|
||||
});
|
||||
|
||||
|
||||
// --- 3) Connect to the session with a valid user token ---
|
||||
// --- 4) Connect to the session with a valid user token ---
|
||||
|
||||
// 'getToken' method is simulating what your server-side should do.
|
||||
// 'token' parameter should be retrieved and returned by your own backend
|
||||
getToken().then(token => {
|
||||
getToken(sessionId).then(token => {
|
||||
|
||||
// Connect with the token
|
||||
session.connect(token)
|
||||
.then(() => {
|
||||
|
||||
// --- 4) Get your own camera stream with the desired properties ---
|
||||
// --- 5) Set page layout for active call ---
|
||||
|
||||
// Update the URL shown in the browser's navigation bar to show the session id
|
||||
var path = (location.pathname.slice(-1) == "/" ? location.pathname : location.pathname + "/");
|
||||
window.history.pushState("", "", path + '#' + sessionId);
|
||||
|
||||
// Auxiliary methods to show the session's view
|
||||
showSessionHideJoin();
|
||||
initializeSessionView();
|
||||
|
||||
// --- 6) Get your own camera stream with the desired properties ---
|
||||
|
||||
publisher = OV.initPublisher('publisher', {
|
||||
audioSource: undefined, // The source of audio. If undefined default audio input
|
||||
videoSource: undefined, // The source of video. If undefined default video input
|
||||
publishAudio: true, // Whether you want to start the publishing with your audio unmuted or muted
|
||||
publishVideo: true, // Whether you want to start the publishing with your video enabled or disabled
|
||||
publishAudio: true, // Whether to start publishing with your audio unmuted or not
|
||||
publishVideo: true, // Whether to start publishing with your video enabled or not
|
||||
resolution: '640x480', // The resolution of your video
|
||||
frameRate: 30, // The frame rate of your video
|
||||
insertMode: 'APPEND', // How the video will be inserted in the target element 'video-container'
|
||||
insertMode: 'APPEND', // How the video is inserted in target element 'video-container'
|
||||
mirror: true // Whether to mirror your local video or not
|
||||
});
|
||||
|
||||
// --- 7) Specify the actions when events take place in our publisher ---
|
||||
|
||||
// When our HTML video has been added to DOM...
|
||||
publisher.on('videoElementCreated', function (event) {
|
||||
// When your own video is added to DOM, update the page layout to fit it
|
||||
numOfVideos++;
|
||||
updateLayout();
|
||||
$(event.element).prop('muted', true); // Mute local video
|
||||
$(event.element).prop('muted', true); // Mute local video to avoid feedback
|
||||
});
|
||||
|
||||
// --- 5) Publish your stream ---
|
||||
// --- 8) Publish your stream ---
|
||||
|
||||
session.publish(publisher);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('There was an error connecting to the session:', error.code, error.message);
|
||||
});
|
||||
|
||||
// Update the URL shown in the browser's navigation bar to show the session id
|
||||
var pathname = (location.pathname.slice(-1) === "/" ? location.pathname : location.pathname + "/");
|
||||
window.history.pushState("", "", pathname + '#' + sessionId);
|
||||
|
||||
// Auxiliary methods to show the session's view
|
||||
showSessionHideJoin();
|
||||
initializeSessionView();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function leaveRoom() {
|
||||
|
||||
// --- 6) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
// --- 9) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
|
||||
session.disconnect();
|
||||
|
||||
// Back to welcome page
|
||||
@ -253,60 +261,43 @@ function updateLayout() {
|
||||
* These methods retrieve the mandatory user token from OpenVidu Server.
|
||||
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
|
||||
* the API REST, openvidu-java-client or openvidu-node-client):
|
||||
* 1) POST /api/sessions
|
||||
* 2) POST /api/tokens
|
||||
* 3) The value returned by /api/tokens must be consumed in Session.connect() method
|
||||
* 1) Initialize a session in OpenVidu Server (POST /api/sessions)
|
||||
* 2) Generate a token in OpenVidu Server (POST /api/tokens)
|
||||
* 3) The token must be consumed in Session.connect() method
|
||||
*/
|
||||
|
||||
function getToken() {
|
||||
return new Promise(async function (resolve) {
|
||||
// POST /api/sessions
|
||||
await apiSessions();
|
||||
// POST /api/tokens
|
||||
let t = await apiTokens();
|
||||
// Return the user token
|
||||
resolve(t);
|
||||
});
|
||||
function getToken(mySessionId) {
|
||||
return createSession(mySessionId).then(sId => createToken(sId));
|
||||
}
|
||||
|
||||
function apiSessions() {
|
||||
function createSession(sId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "https://" + location.hostname + ":4443/api/sessions",
|
||||
data: JSON.stringify({ customSessionId: sessionId }),
|
||||
data: JSON.stringify({ customSessionId: sId }),
|
||||
headers: {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => {
|
||||
if (error.status === 409) {
|
||||
resolve(sessionId);
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
}
|
||||
success: response => resolve(response.id),
|
||||
error: error => error.status === 409 ? resolve(sId) : reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function apiTokens() {
|
||||
function createToken(sId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "https://" + location.hostname + ":4443/api/tokens",
|
||||
data: JSON.stringify({ session: sessionId }),
|
||||
data: JSON.stringify({ session: sId }),
|
||||
headers: {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => { reject(error) }
|
||||
success: response => resolve(response.token),
|
||||
error: error => reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@ -1,41 +1,39 @@
|
||||
var OV;
|
||||
var session;
|
||||
var sessionId;
|
||||
|
||||
function joinSession() {
|
||||
|
||||
var mySessionId = document.getElementById("sessionId").value;
|
||||
|
||||
OV = new OpenVidu();
|
||||
session = OV.initSession();
|
||||
|
||||
session.on('streamCreated', function (event) {
|
||||
session.subscribe(event.stream, 'subscriber');
|
||||
session.on("streamCreated", function (event) {
|
||||
session.subscribe(event.stream, "subscriber");
|
||||
});
|
||||
|
||||
sessionId = document.getElementById("sessionId").value;
|
||||
getToken().then(token => {
|
||||
getToken(mySessionId).then(token => {
|
||||
|
||||
session.connect(token)
|
||||
.then(() => {
|
||||
var publisher = OV.initPublisher('publisher');
|
||||
document.getElementById("session-header").innerText = mySessionId;
|
||||
document.getElementById("join").style.display = "none";
|
||||
document.getElementById("session").style.display = "block";
|
||||
|
||||
var publisher = OV.initPublisher("publisher");
|
||||
session.publish(publisher);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('There was an error connecting to the session:', error.code, error.message);
|
||||
console.log("There was an error connecting to the session:", error.code, error.message);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
document.getElementById('session-header').innerText = sessionId;
|
||||
document.getElementById('join').style.display = 'none';
|
||||
document.getElementById('session').style.display = 'block';
|
||||
}
|
||||
|
||||
function leaveSession() {
|
||||
|
||||
session.disconnect();
|
||||
|
||||
document.getElementById('join').style.display = 'block';
|
||||
document.getElementById('session').style.display = 'none';
|
||||
document.getElementById("join").style.display = "block";
|
||||
document.getElementById("session").style.display = "none";
|
||||
}
|
||||
|
||||
window.onbeforeunload = function () {
|
||||
@ -50,23 +48,16 @@ window.onbeforeunload = function () {
|
||||
* These methods retrieve the mandatory user token from OpenVidu Server.
|
||||
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
|
||||
* the API REST, openvidu-java-client or openvidu-node-client):
|
||||
* 1) POST /api/sessions
|
||||
* 2) POST /api/tokens
|
||||
* 3) The value returned by /api/tokens must be consumed in Session.connect() method
|
||||
* 1) Initialize a session in OpenVidu Server (POST /api/sessions)
|
||||
* 2) Generate a token in OpenVidu Server (POST /api/tokens)
|
||||
* 3) The token must be consumed in Session.connect() method
|
||||
*/
|
||||
|
||||
function getToken() {
|
||||
return new Promise(async function (resolve) {
|
||||
// POST /api/sessions
|
||||
await apiSessions();
|
||||
// POST /api/tokens
|
||||
let t = await apiTokens();
|
||||
// Return the user token
|
||||
resolve(t);
|
||||
});
|
||||
function getToken(mySessionId) {
|
||||
return createSession(mySessionId).then(sessionId => createToken(sessionId));
|
||||
}
|
||||
|
||||
function apiSessions() {
|
||||
function createSession(sessionId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
@ -76,21 +67,13 @@ function apiSessions() {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => {
|
||||
if (error.status === 409) {
|
||||
resolve(sessionId);
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
}
|
||||
success: response => resolve(response.id),
|
||||
error: error => error.status === 409 ? resolve(sessionId) : reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function apiTokens() {
|
||||
function createToken(sessionId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
@ -100,10 +83,8 @@ function apiTokens() {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => { reject(error) }
|
||||
success: response => resolve(response.token),
|
||||
error: error => reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@ -41,6 +41,9 @@
|
||||
}
|
||||
},
|
||||
"defaults": {
|
||||
"build": {
|
||||
"showCircularDependencies": false
|
||||
},
|
||||
"styleExt": "css",
|
||||
"prefixInterfaces": false
|
||||
}
|
||||
|
||||
@ -1,46 +1,29 @@
|
||||
{
|
||||
"name": "openvidu-insecure-angular",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT",
|
||||
"angular-cli": {},
|
||||
"license": "Apache-2.0",
|
||||
"scripts": {
|
||||
"start": "ng serve",
|
||||
"lint": "tslint \"src/**/*.ts\"",
|
||||
"test": "ng test",
|
||||
"pree2e": "webdriver-manager update",
|
||||
"e2e": "protractor"
|
||||
"start": "ng serve"
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@angular/common": "^2.4.0",
|
||||
"@angular/compiler": "^2.4.0",
|
||||
"@angular/core": "^2.4.0",
|
||||
"@angular/forms": "^2.4.0",
|
||||
"@angular/http": "^2.4.0",
|
||||
"@angular/platform-browser": "^2.4.0",
|
||||
"@angular/platform-browser-dynamic": "^2.4.0",
|
||||
"@angular/router": "^3.4.0",
|
||||
"core-js": "^2.4.1",
|
||||
"openvidu-browser": "2.0.0",
|
||||
"zone.js": "^0.7.6"
|
||||
"@angular/common": "5.2.10",
|
||||
"@angular/compiler": "5.2.10",
|
||||
"@angular/core": "5.2.10",
|
||||
"@angular/forms": "5.2.10",
|
||||
"@angular/platform-browser": "5.2.10",
|
||||
"@angular/platform-browser-dynamic": "5.2.10",
|
||||
"core-js": "2.5.6",
|
||||
"openvidu-browser": "1.9.0",
|
||||
"openvidu-node-client": "1.9.0",
|
||||
"zone.js": "0.8.26"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jasmine": "2.5.38",
|
||||
"@types/node": "~6.0.60",
|
||||
"@angular/cli": "1.4.3",
|
||||
"@angular/compiler-cli": "^2.4.0",
|
||||
"codelyzer": "~2.0.0",
|
||||
"jasmine-core": "~2.5.2",
|
||||
"jasmine-spec-reporter": "~3.2.0",
|
||||
"karma": "~1.4.1",
|
||||
"karma-chrome-launcher": "~2.0.0",
|
||||
"karma-cli": "~1.0.1",
|
||||
"karma-jasmine": "~1.1.0",
|
||||
"karma-jasmine-html-reporter": "^0.2.2",
|
||||
"karma-coverage-istanbul-reporter": "^0.2.0",
|
||||
"protractor": "~5.1.0",
|
||||
"ts-node": "~2.0.0",
|
||||
"tslint": "~4.4.2",
|
||||
"typescript": "2.5.3"
|
||||
"@types/node": "10.0.4",
|
||||
"@angular/cli": "1.7.4",
|
||||
"@angular/compiler-cli": "5.2.10",
|
||||
"codelyzer": "4.3.0",
|
||||
"ts-node": "6.0.3",
|
||||
"tslint": "5.10.0",
|
||||
"typescript": "2.7.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -6,11 +6,11 @@
|
||||
<form class="form-group" (submit)="joinSession()">
|
||||
<p>
|
||||
<label>Participant</label>
|
||||
<input class="form-control" type="text" id="userName" name="userName" [(ngModel)]="userName" required>
|
||||
<input class="form-control" type="text" id="userName" name="userName" [(ngModel)]="myUserName" required>
|
||||
</p>
|
||||
<p>
|
||||
<label>Session</label>
|
||||
<input class="form-control" type="text" id="sessionId" name="sessionId" [(ngModel)]="sessionId" required>
|
||||
<input class="form-control" type="text" id="sessionId" name="sessionId" [(ngModel)]="mySessionId" required>
|
||||
</p>
|
||||
<p class="text-center">
|
||||
<input class="btn btn-lg btn-success" type="submit" name="commit" value="Join!">
|
||||
@ -21,7 +21,7 @@
|
||||
|
||||
<div *ngIf="session" id="session">
|
||||
<div id="session-header">
|
||||
<h1 id="session-title">{{sessionId}}</h1>
|
||||
<h1 id="session-title">{{mySessionId}}</h1>
|
||||
<input class="btn btn-large btn-danger" type="button" id="buttonLeaveSession" (click)="leaveSession()" value="Leave session">
|
||||
</div>
|
||||
<div *ngIf="this.mainVideoStream" id="main-video" class="col-md-6">
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { OpenVidu, Session, Stream, StreamEvent } from 'openvidu-browser';
|
||||
import { Component, HostListener, Input, OnDestroy } from '@angular/core';
|
||||
|
||||
declare var $;
|
||||
import { OpenVidu, Session, Stream, StreamEvent } from 'openvidu-browser';
|
||||
import { OpenVidu as OpenViduAPI } from 'openvidu-node-client';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
@ -19,8 +18,8 @@ export class AppComponent implements OnDestroy {
|
||||
localStream: Stream;
|
||||
|
||||
// Join form
|
||||
sessionId: string;
|
||||
userName: string;
|
||||
mySessionId: string;
|
||||
myUserName: string;
|
||||
|
||||
// Main video of the page, will be 'localStream' or one of the 'remoteStreams',
|
||||
// updated by an Output event of StreamComponent children
|
||||
@ -43,13 +42,16 @@ export class AppComponent implements OnDestroy {
|
||||
|
||||
joinSession() {
|
||||
|
||||
// --- 1) Get an OpenVidu object and init a session ---
|
||||
// --- 1) Get an OpenVidu object ---
|
||||
|
||||
this.OV = new OpenVidu();
|
||||
|
||||
// --- 2) Init a session ---
|
||||
|
||||
this.session = this.OV.initSession();
|
||||
|
||||
|
||||
// --- 2) Specify the actions when events take place ---
|
||||
// --- 3) Specify the actions when events take place in the session ---
|
||||
|
||||
// On every new Stream received...
|
||||
this.session.on('streamCreated', (event: StreamEvent) => {
|
||||
@ -65,37 +67,43 @@ export class AppComponent implements OnDestroy {
|
||||
// On every Stream destroyed...
|
||||
this.session.on('streamDestroyed', (event: StreamEvent) => {
|
||||
|
||||
// Avoid OpenVidu trying to remove the HTML video element
|
||||
event.preventDefault();
|
||||
|
||||
// Remove the stream from 'remoteStreams' array
|
||||
this.deleteRemoteStream(event.stream);
|
||||
});
|
||||
|
||||
|
||||
// --- 3) Connect to the session with a valid user token ---
|
||||
// --- 4) Connect to the session with a valid user token ---
|
||||
|
||||
// 'getToken' method is simulating what your server-side should do.
|
||||
// 'token' parameter should be retrieved and returned by your own backend
|
||||
this.getToken().then(token => {
|
||||
|
||||
// First param is the token retrieved from OpenVidu Server. Second param will be received by every user
|
||||
// in Stream.connection.data property, which will be appended to DOM as the user's nickname
|
||||
this.session.connect(token, '{"clientData": "' + this.userName + '"}')
|
||||
// First param is the token got from OpenVidu Server. Second param can be retrieved by every user on event
|
||||
// 'streamCreated' (property Stream.connection.data), and will be appended to DOM as the user's nickname
|
||||
this.session.connect(token, { clientData: this.myUserName })
|
||||
.then(() => {
|
||||
|
||||
// --- 4) Get your own camera stream ---
|
||||
// --- 5) Get your own camera stream ---
|
||||
|
||||
// Init a publisher passing undefined as targetElement (we don't want OpenVidu to insert a video
|
||||
// element: we will manage it ourselves) and with no properties (default ones)
|
||||
let publisher = this.OV.initPublisher(undefined);
|
||||
// element: we will manage it ourselves) and with the desired properties
|
||||
let publisher = this.OV.initPublisher(undefined, {
|
||||
audioSource: undefined, // The source of audio. If undefined default microphone
|
||||
videoSource: undefined, // The source of video. If undefined default webcam
|
||||
publishAudio: true, // Whether you want to start publishing with your audio unmuted or not
|
||||
publishVideo: true, // Whether you want to start publishing with your video enabled or not
|
||||
resolution: '640x480', // The resolution of your video
|
||||
frameRate: 30, // The frame rate of your video
|
||||
insertMode: 'APPEND', // How the video is inserted in the target element 'video-container'
|
||||
mirror: false // Whether to mirror your local video or not
|
||||
});
|
||||
|
||||
// Store your webcam stream in 'localStream' object
|
||||
this.localStream = publisher.stream;
|
||||
// Set the main video in the page to display our webcam
|
||||
this.mainVideoStream = this.localStream;
|
||||
|
||||
// --- 5) Publish your stream ---
|
||||
// --- 6) Publish your stream ---
|
||||
|
||||
this.session.publish(publisher);
|
||||
})
|
||||
@ -106,7 +114,9 @@ export class AppComponent implements OnDestroy {
|
||||
}
|
||||
|
||||
leaveSession() {
|
||||
// --- 6) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
|
||||
// --- 7) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
|
||||
if (this.session) { this.session.disconnect(); };
|
||||
|
||||
// Empty all properties...
|
||||
@ -120,8 +130,8 @@ export class AppComponent implements OnDestroy {
|
||||
|
||||
private generateParticipantInfo() {
|
||||
// Random user nickname and sessionId
|
||||
this.sessionId = 'SessionA';
|
||||
this.userName = 'Participant' + Math.floor(Math.random() * 100);
|
||||
this.mySessionId = 'SessionA';
|
||||
this.myUserName = 'Participant' + Math.floor(Math.random() * 100);
|
||||
}
|
||||
|
||||
private deleteRemoteStream(stream: Stream): void {
|
||||
@ -141,65 +151,20 @@ export class AppComponent implements OnDestroy {
|
||||
* --------------------------
|
||||
* SERVER-SIDE RESPONSABILITY
|
||||
* --------------------------
|
||||
* These methods retrieve the mandatory user token from OpenVidu Server.
|
||||
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
|
||||
* the API REST, openvidu-java-client or openvidu-node-client):
|
||||
* 1) POST /api/sessions
|
||||
* 2) POST /api/tokens
|
||||
* 3) The value returned by /api/tokens must be consumed in Session.connect() method
|
||||
* This method retrieve the mandatory user token from OpenVidu Server,
|
||||
* in this case making use of OpenVidu Node Client.
|
||||
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION. In this case:
|
||||
* 1) Initialize a session in OpenVidu Server (OpenVidu.createSession with OpenVidu Node Client)
|
||||
* 2) Generate a token in OpenVidu Server (Session.generateToken with OpenVidu Node Client)
|
||||
* 3) The token must be consumed in Session.connect() method of OpenVidu Browser
|
||||
*/
|
||||
|
||||
private getToken() {
|
||||
return new Promise<string>(async (resolve) => {
|
||||
// POST /api/sessions
|
||||
await this.apiSessions();
|
||||
// POST /api/tokens
|
||||
let t = await this.apiTokens();
|
||||
// Return the user token
|
||||
resolve(<string>t);
|
||||
});
|
||||
}
|
||||
|
||||
private apiSessions() {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: 'https://' + location.hostname + ':4443/api/sessions',
|
||||
data: JSON.stringify({ customSessionId: this.sessionId }),
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + btoa('OPENVIDUAPP:MY_SECRET'),
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => {
|
||||
if (error.status === 409) {
|
||||
resolve(this.sessionId);
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
}
|
||||
getToken(): Promise<string> {
|
||||
let OV_NodeClient = new OpenViduAPI('https://' + location.hostname + ':4443', 'MY_SECRET');
|
||||
return OV_NodeClient.createSession({ customSessionId: this.mySessionId })
|
||||
.then(session_NodeClient => {
|
||||
return session_NodeClient.generateToken();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private apiTokens() {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: 'https://' + location.hostname + ':4443/api/tokens',
|
||||
data: JSON.stringify({ session: this.sessionId }),
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + btoa('OPENVIDUAPP:MY_SECRET'),
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => { reject(error) }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { HttpModule } from '@angular/http';
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
import { StreamComponent } from './stream.component';
|
||||
@ -12,8 +11,7 @@ import { StreamComponent } from './stream.component';
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
FormsModule,
|
||||
HttpModule
|
||||
FormsModule
|
||||
],
|
||||
providers: [],
|
||||
bootstrap: [AppComponent]
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import { Component, Input, Output, AfterViewInit, DoCheck, EventEmitter, ViewChild, ElementRef } from '@angular/core';
|
||||
|
||||
import { Stream } from 'openvidu-browser';
|
||||
|
||||
@Component({
|
||||
@ -61,7 +60,7 @@ export class StreamComponent implements AfterViewInit, DoCheck {
|
||||
return JSON.parse(this.stream.connection.data).clientData;
|
||||
}
|
||||
|
||||
videoClicked() { // Triggers event for the parent component to update its view
|
||||
videoClicked() { // Triggers event for the parent component to update its main video display
|
||||
this.mainVideoStream.next(this.stream);
|
||||
}
|
||||
|
||||
|
||||
@ -1,31 +1,32 @@
|
||||
var OV;
|
||||
var session;
|
||||
var sessionId;
|
||||
|
||||
|
||||
/* OPENVIDU METHODS */
|
||||
|
||||
function joinSession() {
|
||||
|
||||
sessionId = document.getElementById("sessionId").value;
|
||||
var userName = document.getElementById("userName").value;
|
||||
var mySessionId = document.getElementById("sessionId").value;
|
||||
var myUserName = document.getElementById("userName").value;
|
||||
|
||||
// --- 1) Get an OpenVidu object and init a session ---
|
||||
// --- 1) Get an OpenVidu object ---
|
||||
|
||||
OV = new OpenVidu();
|
||||
|
||||
// --- 2) Init a session ---
|
||||
|
||||
session = OV.initSession();
|
||||
|
||||
|
||||
// --- 2) Specify the actions when events take place ---
|
||||
// --- 3) Specify the actions when events take place in the session ---
|
||||
|
||||
// On every new Stream received...
|
||||
session.on('streamCreated', function (event) {
|
||||
session.on('streamCreated', event => {
|
||||
|
||||
// Subscribe to the Stream to receive it. HTML video will be appended to element with 'video-container' id
|
||||
var subscriber = session.subscribe(event.stream, 'video-container');
|
||||
|
||||
// When the HTML video has been appended to DOM...
|
||||
subscriber.on('videoElementCreated', function (event) {
|
||||
subscriber.on('videoElementCreated', event => {
|
||||
|
||||
// Add a new <p> element for the user's nickname just below its video
|
||||
appendUserData(event.element, subscriber.stream.connection);
|
||||
@ -33,65 +34,70 @@ function joinSession() {
|
||||
});
|
||||
|
||||
// On every Stream destroyed...
|
||||
session.on('streamDestroyed', function (event) {
|
||||
session.on('streamDestroyed', event => {
|
||||
|
||||
// Delete the HTML element with the user's nickname. HTML videos are automatically removed from DOM
|
||||
removeUserData(event.stream.connection);
|
||||
});
|
||||
|
||||
|
||||
// --- 3) Connect to the session with a valid user token ---
|
||||
// --- 4) Connect to the session with a valid user token ---
|
||||
|
||||
// 'getToken' method is simulating what your server-side should do.
|
||||
// 'token' parameter should be retrieved and returned by your own backend
|
||||
getToken().then(token => {
|
||||
getToken(mySessionId).then(token => {
|
||||
|
||||
// First param is the token retrieved from OpenVidu Server. Second param will be received by every user
|
||||
// in Stream.connection.data property, which will be appended to DOM as the user's nickname
|
||||
session.connect(token, '{"clientData": "' + userName + '"}')
|
||||
// First param is the token got from OpenVidu Server. Second param can be retrieved by every user on event
|
||||
// 'streamCreated' (property Stream.connection.data), and will be appended to DOM as the user's nickname
|
||||
session.connect(token, { clientData: myUserName })
|
||||
.then(() => {
|
||||
|
||||
// --- 4) Get your own camera stream with the desired properties ---
|
||||
// --- 5) Set page layout for active call ---
|
||||
|
||||
document.getElementById('session-title').innerText = mySessionId;
|
||||
document.getElementById('join').style.display = 'none';
|
||||
document.getElementById('session').style.display = 'block';
|
||||
|
||||
// --- 6) Get your own camera stream with the desired properties ---
|
||||
|
||||
var publisher = OV.initPublisher('video-container', {
|
||||
audioSource: undefined, // The source of audio. If undefined default audio input
|
||||
videoSource: undefined, // The source of video. If undefined default video input
|
||||
publishAudio: true, // Whether you want to start the publishing with your audio unmuted or muted
|
||||
publishVideo: true, // Whether you want to start the publishing with your video enabled or disabled
|
||||
audioSource: undefined, // The source of audio. If undefined default microphone
|
||||
videoSource: undefined, // The source of video. If undefined default webcam
|
||||
publishAudio: true, // Whether you want to start publishing with your audio unmuted or not
|
||||
publishVideo: true, // Whether you want to start publishing with your video enabled or not
|
||||
resolution: '640x480', // The resolution of your video
|
||||
frameRate: 30, // The frame rate of your video
|
||||
insertMode: 'APPEND', // How the video will be 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
|
||||
});
|
||||
|
||||
// --- 7) Specify the actions when events take place in our publisher ---
|
||||
|
||||
// When our HTML video has been added to DOM...
|
||||
publisher.on('videoElementCreated', function (event) {
|
||||
initMainVideo(event.element, userName);
|
||||
appendUserData(event.element, userName);
|
||||
initMainVideo(event.element, myUserName);
|
||||
appendUserData(event.element, myUserName);
|
||||
event.element['muted'] = true;
|
||||
});
|
||||
|
||||
// --- 5) Publish your stream ---
|
||||
// --- 8) Publish your stream ---
|
||||
|
||||
session.publish(publisher);
|
||||
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('There was an error connecting to the session:', error.code, error.message);
|
||||
});
|
||||
|
||||
document.getElementById('session-title').innerText = sessionId;
|
||||
document.getElementById('join').style.display = 'none';
|
||||
document.getElementById('session').style.display = 'block';
|
||||
});
|
||||
}
|
||||
|
||||
function leaveSession() {
|
||||
|
||||
// --- 6) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
// --- 9) Leave the session by calling 'disconnect' method over the Session object ---
|
||||
|
||||
session.disconnect();
|
||||
|
||||
// Removing all HTML elements with the user's nicknames.
|
||||
// Removing all HTML elements with user's nicknames.
|
||||
// HTML videos are automatically removed when leaving a Session
|
||||
removeAllUserData();
|
||||
|
||||
@ -173,23 +179,16 @@ function initMainVideo(videoElement, userData) {
|
||||
* These methods retrieve the mandatory user token from OpenVidu Server.
|
||||
* This behaviour MUST BE IN YOUR SERVER-SIDE IN PRODUCTION (by using
|
||||
* the API REST, openvidu-java-client or openvidu-node-client):
|
||||
* 1) POST /api/sessions
|
||||
* 2) POST /api/tokens
|
||||
* 3) The value returned by /api/tokens must be consumed in Session.connect() method
|
||||
* 1) Initialize a session in OpenVidu Server (POST /api/sessions)
|
||||
* 2) Generate a token in OpenVidu Server (POST /api/tokens)
|
||||
* 3) The token must be consumed in Session.connect() method
|
||||
*/
|
||||
|
||||
function getToken() {
|
||||
return new Promise(async function (resolve) {
|
||||
// POST /api/sessions
|
||||
await apiSessions();
|
||||
// POST /api/tokens
|
||||
let t = await apiTokens();
|
||||
// Return the user token
|
||||
resolve(t);
|
||||
});
|
||||
function getToken(mySessionId) {
|
||||
return createSession(mySessionId).then(sessionId => createToken(sessionId));
|
||||
}
|
||||
|
||||
function apiSessions() {
|
||||
function createSession(sessionId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
@ -199,21 +198,13 @@ function apiSessions() {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => {
|
||||
if (error.status === 409) {
|
||||
resolve(sessionId);
|
||||
} else {
|
||||
reject(error)
|
||||
}
|
||||
}
|
||||
success: response => resolve(response.id),
|
||||
error: error => error.status === 409 ? resolve(sessionId) : reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function apiTokens() {
|
||||
function createToken(sessionId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
@ -223,10 +214,8 @@ function apiTokens() {
|
||||
"Authorization": "Basic " + btoa("OPENVIDUAPP:MY_SECRET"),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
success: (response) => {
|
||||
resolve(response.id)
|
||||
},
|
||||
error: (error) => { reject(error) }
|
||||
success: response => resolve(response.token),
|
||||
error: error => reject(error)
|
||||
});
|
||||
});
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user