Provides access to the remote services. For Spectacles, this module process access to the open internet.

Loading an image:

//@input Asset.RemoteServiceModule rsm
//@input Asset.RemoteMediaModule rmm
//@input Component.Image image

let request = RemoteServiceHttpRequest.create();
request.url = "https://docs.snap.com/img/spectacles/spectacles-project-info-settings.png";

script.rsm
.performHttpRequest(request, function(response){
const dynamicResource = response.asResource()

script.rmm
.loadResourceAsImageTexture(dynamicResource, function(texture) {

script.image.mainPass.baseTex = texture
},function(error){
print(error)
})
})

Webview texture:

// Create the options 
var resolution = new vec2(512, 512);
var options = RemoteServiceModule.createWebViewOptions(resolution);

// Create the WebView
script.remoteServiceModule.createWebView(
options,
(texture) => {
script.image.mainPass.baseTex = texture;
webViewControl = texture.control;
webViewControl.onReady.add(() => {
print("onReady");
webViewControl.loadUrl("https://snap.com");
});
},
(msg) => {
print("Error:" + msg);
}
);

Webview texture in TypeScript:

const resolution = new vec2(512,512)
const options = RemoteServiceModule.createWebViewOptions(resolution)

this.remoteServiceModule.createWebView(
options,
(texture: Asset.Texutre) => {
this.image.mainPass.baseTex = texture
this.webViewControl = texture.control
this.webViewControl.onReady.add(() => {
print("onReady")
this.webViewControl.loadUrl("https://snap.com")
})
},
(msg: string) => {
print(`Error: ${msg}`)
}
)

Hierarchy (View Summary, Expand)

Constructors

Properties

name: string

The name of the Asset in Lens Studio.

uniqueIdentifier: string

Methods

  • Wearable Only

    The createWebSocket() method initiates a WebSocket connection with the given wss URL and returns a WebSocket object that can be used to send and receive messages from the server.

    Syntax

    createWebSocket(url)
    

    url Defines the wss URL to which to establish the WebSocket connection.

    Example

    //@input Asset.RemoteServiceModule remoteServiceModule
    var remoteServiceModule = script.remoteServiceModule;

    // Create WebSocket connection.
    let socket = script.remoteServiceModule.createWebSocket("wss://<some-url>");
    socket.binaryType = "blob";

    // Listen for the open event
    socket.onopen = (event) => {
    // Socket has opened, send a message back to the server
    socket.send("Message 1");

    // Try sending a binary message
    // (the bytes below spell 'Message 2')
    const message = [77, 101, 115, 115, 97, 103, 101, 32, 50];
    const bytes = new Uint8Array(message);
    socket.send(bytes);
    };

    // Listen for messages
    socket.onmessage = async (event) => {
    if (event.data instanceof Blob) {
    // Binary frame, can be retrieved as either Uint8Array or string
    let bytes = await event.data.bytes();
    let text = await event.data.text();

    print("Received binary message, printing as text: " + text);
    } else {
    // Text frame
    let text = event.data;
    print("Received text message: " + text);
    }
    });

    socket.onclose = (event) => {
    if (event.wasClean) {
    print("Socket closed cleanly");
    } else {
    print("Socket closed with error, code: " + event.code);
    }
    };

    socket.onerror = (event) => {
    print("Socket error");
    };

    Parameters

    • url: string

    Returns WebSocket

  • Experimental Wearable Only

    This function will create a new instance of a webview with the specified options. Once it has been created onSuccess will be invoked which returns the Texture instance of the WebView for rendering which contains a reference to WebPageTextureProvider through the Control property. WebPageTextureProvider can be used for sending events and actions to the WebView. In the event of an error, the onError callback is invoked with the error message.

    • options: the options for a specific webview.
    • onSuccess: invoked on webview creation. Provides the Asset.Texture for rendering and control.
    • onError: invoked on creation failed. Provides the Error message.

    Note: Only 1 callback will be invoked, and only once. Note: After creation, the webview will later invoke onReady on the WebPageTextureProvider.control object to indicate it is ready for handling events and actions.

    Parameters

    Returns void

  • Experimental Wearable Only

    This function creates a WebViewOptions instance that allows you to configure your webview.

    Resolution:* vec2 type representing the desired webpage resolution.

    Note: This is capped at 2048x2048. Note: Once a webview has been created this can not be changed.

    Parameters

    Returns WebViewOptions

  • Wearable Only

    The fetch() method starts the process of fetching a resource from the internet, returning a promise that is fulfilled once the response is available.

    The promise resolves to a Response object representing the response to your request.

    A fetch() promise only rejects in cases of malformed URLs or network errors. A fetch() promise does not reject if the server responds with HTTP status codes that indicate errors. These errors can be checked manually via the Response status properties.

    Syntax

    fetch(resource)
    fetch(resource, options)

    resource Defines the URL you wish to fetch, or a Request object.

    options Object containing any custom settings you want to apply to the request. Available options are body, method, headers, redirect, and keepalive. For more information on these properties see the Request class. If a Request was given for resource, then these options will entry-wise override the options specified in the Request object.

    Example

    //@input Asset.RemoteServiceModule remoteServiceModule
    var remoteServiceModule = script.remoteServiceModule;

    // For this example assume this URL simply responds with the same body
    // that it receives.
    let request = new Request("https://<Your URL>.com", {
    method: "POST",
    body: JSON.stringify({ user: { name: "user", career: "developer" }}),
    headers: {
    "Content-Type": "application/json",
    },
    });

    let response = await remoteServiceModule.fetch(request, {
    body: JSON.stringify({ user: { name: "user", career: "salesman" }})
    });
    if (response.status != 200) {
    print("Failure: response not successful");
    return;
    }

    let contentTypeHeader = response.headers.get("content-type");
    if (!contentTypeHeader.includes("application/json")) {
    print("Failure: wrong content type in response");
    return;
    }

    let responseJson = await response.json();
    let username = responseJson.json["user"]["name"];
    let career = responseJson.json["user"]["career"];

    print(career); // will print "salesman"

    Parameters

    • request: string | Request
    • Optionaloptions: any

    Returns Promise<Response>

  • Returns the name of this object's type.

    Returns string

  • Returns true if the object matches or derives from the passed in type.

    Parameters

    • type: string

    Returns boolean

  • Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

    Parameters

    Returns boolean

  • Wearable Only Camera Kit

    Get a DynamicResource to be used with RemoteMediaModule from mediaUrl.

    Parameters

    • mediaUrl: string

    Returns DynamicResource

MMNEPVFCICPMFPCPTTAAATR