RemoteDesktop
The following example demonstrates how to request a browser instance, connect to it via Puppeteer, start a remote desktop session, and then stop the remote desktop session.
const svc = new BrowserService(cloudBrowserToken);
const rp = await svc.open();
if (rp.status !== ResponseStatus.SUCCESS || rp.address == null) {
console.log(`Error requesting browser: ${rp.status}`);
return;
}
console.log("Browser requested");
const rmt = await svc.startRemoteDesktop(rp.address);
console.log("Remote desktop address:");
console.log(
`https://browser.cloudbrowser.ai${obtainId(rp.address)}/${rmt.password}`
);
await new Promise((resolve) => setTimeout(resolve, 5000));
await svc.stopRemoteDesktop(rp.address);Full example
This code initializes a browser session, starts a remote desktop session for that browser, and then stops the remote desktop after a short delay. Here’s a breakdown:
1. Browser Session Initialization
A
BrowserServiceinstance is created usingcloudBrowserTokenfor authentication.The
svc.open()method is called to request a browser session. If the request fails or the WebSocket endpoint (rp.address) is not provided, an error message is logged, and the script terminates.If successful, it logs that the browser has been requested.
2. Starting a Remote Desktop Session
The
svc.startRemoteDesktop(rp.address)method is used to enable a remote desktop interface for the browser session. Thermt.passwordis generated for secure access.The script constructs and logs a URL to access the remote desktop session using the browser's ID (
obtainId(rp.address)) and password.
3. Delay and Stop Remote Desktop
The script pauses for 5 seconds using
setTimeout, allowing interaction with the remote desktop session during this time.After the delay, the
svc.stopRemoteDesktop(rp.address)method is called to terminate the remote desktop session.
Last updated