Namespace: Utils

RS.Utils

Provides assorted utilities useful to RealityServer users.

Classes

EventEmitter

Methods

create_random_string (length)String

Creates a random string of the given length using characters 0-9 and a-z. Useful for creating random scope, scene or render loop names.

Name Type Description
length Number

The length of the string

Returns:
Type Description
String The random string

extract_url_details (url)Object

Extracts details from the provided URL which can be used to create the web socket connection URL. Useful when the webpage is served directly from the RealityServer we wish to connect to. Supports the following protocols:

  • file
  • http
  • https

If a file URL is supplied the we assume RealityServer is running on localhost port 8080.

The returned object has the following properties:

  • host the hostname portion of the url. Supports hostnames, IPv4 and IPv6 addresses.
  • port the port used by the url. If no port was specified then the default port for the protocol is returned.
  • secure whether a secure protocol was supplied.
Name Type Description
url String

The url to extract from.

Throws:

Throws if the URL protocol is unsupported.

Type
String
Returns:
Type Description
Object
Example
const url_info = RS.Utils.extract_url_details(document.location.toString());
const ws_url = (url_info.secure ? 'wss://' : 'ws://') +
                url_info.host + ':' + url_info.port +
                '/service/';

html_image_display (image, url_creator)

Returns a function that can be used as a event handler for render loop stream image events to display rendered images via an HTML Image element (or any object that can process a URL assigned to it's src property).

Name Type Description
image Image

the image element to use

url_creator Object optional

an object that implements URL.createObjectUrl(Blob) and URL.revokeObjectURL(String). If not provided then window.URL or window.webkitURL will be used.

Example
// Assumptions: There exists a DOM element <img src="" id="rs_display">

async start_render_loop(service) {
    // start the render loop
    try {
        await service.execute_command(
                 new RS.Command('start_render_loop',{
                     render_loop_name: 'meyemii_render_loop',
                     render_loop_handler_name: 'default',
                     scene_name: 'meyemii',
                     render_loop_handler_parameters: [ 'renderer', 'iray' ],
                     timeout: 30
                 }),true);
     } catch(err) {
         console.error(`Start render loop failed ${JSON.stringify(err)}`);
         return;
     }
     // get image to display in
     const image_element = Document.getElementById('rs_display');

     // stream rendered results back from the render loop and display them in
     // rs_display
     try {
         const stream = await service.stream(
             {
                 render_loop_name: 'meyemii_render_loop',
                 image_format: 'jpg',
                 quality: '100'
             });
         stream.on('image',RS.Utils.html_image_display(image_element));
         stream.on('image',image => {
                 if (image.result < 0) {
                     console.error(`Render error: ${image.result}`)
                     return; // error on render
                 }
                 console.log('Image rendered.');
         });
     } catch (err) {
         console.error(`Start render loop stream failed ${JSON.stringify(err)}`)
     };
 }