简体   繁体   中英

How to get started using NICE DCV SDK

I am trying out the NICE DCV SDK from AWS, documentation linkedhere But I can't figure out how to run the example given on the docs page. I tried running it in a node server but get this error:

ReferenceError: self is not defined

So, it would be fantastic if someone could help me get started with the example .

Edit: I tried simply opening the index.html from a browser as well.

Simply copying the example from the Developer Guide, hence having:

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>DCV first connection</title>
    </head>
    <body>
        <script type="module" src="main.js"></script>
        <div id="dcv-display"></div>
    </body>
</html>

main.js

import "./dcvjs/dcv.js";

let auth,
    connection,
    serverUrl;

function onPromptCredentials(auth, challenge) {
    // Let's check if in challenge we have a username and password request
    if (challengeHasField(challenge, "username") && challengeHasField(challenge, "password")) {
        auth.sendCredentials({username: "my_dcv_user", password: "my_password"})
    } else {
        // Challenge is requesting something else...
    }
}

function challengeHasField(challenge, field) {
    return challenge.requiredCredentials.some(credential => credential.name === field);
}

function onError(_, error) {
    console.log("Error during the authentication: ", error.message);
}

// We connect to the first session returned
function onSuccess(_, result) {
    let {sessionId, authToken} = {...result[0]};
    connect(sessionId, authToken);
}

function connect (sessionId, authToken) {
    console.log(sessionId, authToken);
    dcv.connect({
        url: serverUrl,
        sessionId: sessionId,
        authToken: authToken,
        divId: "dcv-display",
        callbacks: {
            firstFrame: () => console.log("First frame received")
        }
    }).then(function (conn) {
        console.log("Connection established!");
        connection= conn;
    }).catch(function (error) {
        console.log("Connection failed with error " + error.message);
    });
}

function main () {
    console.log("Setting log level to INFO");
    dcv.setLogLevel(dcv.LogLevel.INFO);
    serverUrl = "https://your-dcv-server-url:port/";
    console.log("Starting authentication with", serverUrl);
    auth = dcv.authenticate(
        serverUrl,
        {
            promptCredentials: onPromptCredentials,
            error: onError,
            success: onSuccess
        }
    );
}

console.log("Using NICE DCV Web Client SDK version " + dcv.version.versionStr);
document.addEventListener('DOMContentLoaded', main);

changing the username , password and serverUrl , should work in a web environment.

I was also able to serve it from a node.js server set up as in the example here: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework

We have created a demo of the NICE DCV SDK integration. Please try it or download it from https://www.ni-sp.com/DCVSDK/

Here are the 2 files including the bonus login form and a couple of issues fixed (please adapt the serverUrl in main.js):

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>DCV first connection</title>
    </head>
    <body>
        <script type="module" src="main.js"></script>
        <div id="dcv-display"></div>
    </body>
</html>

main.js

import "./dcvjs/dcv.js"
let auth,
    connection,
    serverUrl;

console.log("Using NICE DCV Web Client SDK version " + dcv.version.versionStr);
document.addEventListener('DOMContentLoaded', main);

function main () {
    console.log("Setting log level to INFO");
    dcv.setLogLevel(dcv.LogLevel.INFO);

    serverUrl = "https://your-dcv-server-url:port/";
    
    console.log("Starting authentication with", serverUrl);

    auth = dcv.authenticate(
        serverUrl,
        {
            promptCredentials: onPromptCredentials2,
            error: onError,
            success: onSuccess
        }
    );
}


function onPromptCredentials(auth, challenge) {
    // Let's check if in challege we have a username and password request
    if (challengeHasField(challenge, "username") && challengeHasField(challenge, "password")) {
        auth.sendCredentials({username: "YOURUSER", password: "YOUR_PW"})
    } else {
        // Challenge is requesting something else...
    }
}

function challengeHasField(challenge, field) {
    return challenge.requiredCredentials.some(credential => credential.name === field);
}

function onError(auth, error) {
    console.log("Error during the authentication: ", error.message);
}

// We connect to the first session returned
function onSuccess(auth, result) {
    let {sessionId, authToken} = {...result[0]};

    connect(sessionId, authToken);
}

function connect (sessionId, authToken) {
    console.log(sessionId, authToken);

    dcv.connect({
        url: serverUrl,
        sessionId: sessionId,
        authToken: authToken,
        divId: "dcv-display",
        callbacks: {
            firstFrame: () => console.log("First frame received")
        }
    }).then(function (conn) {
        console.log("Connection established!");
        connection= conn;
    }).catch(function (error) {
        console.log("Connection failed with error " + error.message);
    });
}


function submitCredentials (e) {
    var credentials = {};
    fieldSet.childNodes.forEach(input => credentials[input.id] = input.value);
    auth.sendCredentials(credentials);
    e.preventDefault();
}

var fieldSet;

function createLoginForm () {
    var submitButton = document.createElement("button");

    submitButton.type = "submit";
    submitButton.textContent = "Login";

    var form = document.createElement("form");
    fieldSet = document.createElement("fieldset");

    form.onsubmit = submitCredentials;
    form.appendChild(fieldSet);
    form.appendChild(submitButton);

    document.body.appendChild(form);
}

function addInput (name) {
    var type = name === "password" ? "password" : "text";

    var inputField = document.createElement("input");
    inputField.name = name;
    inputField.id = name;
    inputField.placeholder = name;
    inputField.type = type;
    fieldSet.appendChild(inputField);
}

function onPromptCredentials2 (_, credentialsChallenge) {
    createLoginForm();
    credentialsChallenge.requiredCredentials.forEach(challenge => addInput(challenge.name));
}

If anyone is wondering that Ni-sp site works and not yours. Here is solution:

The web server & DCV Server both needs to have certificate assigned to them when both trust (most probably a internal root CA) and the client connecting to the Web server must have the root certificates so that it can validate the web server and DCV server certificates.

I tested it worked correctly. Without valid certificate you will get the exception "Error during the authentication: Failed to communicate with server dcv.js:2 [authentication] Auth WebSocket connection closed. Code 1006"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM