Defines callbacks for CloudXR session events.

Applications implement these callbacks to receive notifications about session lifecycle events and WebGL state changes. All callbacks are optional.

const delegates: SessionDelegates = {
onStreamStarted: () => {
console.info('CloudXR streaming started');
// Update UI to show streaming status
},
onStreamStopped: (error) => {
if (error) {
console.error('Streaming stopped due to error:', error);
} else {
console.info('Streaming stopped normally');
}
}
};
interface SessionDelegates {
    onStreamStarted?: () => void;
    onStreamStopped?: (error?: Error) => void;
    onWebGLStateChangeBegin?: () => void;
    onWebGLStateChangeEnd?: () => void;
    onServerMessageReceived?: (messageData: Uint8Array) => void;
}

Properties

onStreamStarted?: () => void

Invoked when streaming connects successfully.

Called when the session transitions to the Connected state and streaming is ready to begin. At this point, the session is ready for rendering and all WebGL resources have been initialized.

onStreamStarted: () => {
console.info('Ready to render CloudXR content');
// Start your render loop here
}
onStreamStopped?: (error?: Error) => void

Invoked when streaming stops (either by client or server).

Called when the session stops streaming, either due to a normal disconnection or an error condition. Check the error parameter to determine the reason for the stop.

Type declaration

    • (error?: Error): void
    • Parameters

      • Optionalerror: Error

        Optional error object if streaming stopped due to an error. If undefined, the stop was intentional (e.g., disconnect() called).

      Returns void

onStreamStopped: (error) => {
if (error) {
console.error('Streaming error:', error.message);
// Handle error condition
} else {
console.info('Streaming stopped normally');
// Handle normal disconnection
}
}
onWebGLStateChangeBegin?: () => void

Invoked before the session changes any WebGL state.

Called before the session modifies WebGL state during rendering operations. Use this to save the current WebGL state if needed for your application's rendering pipeline.

onWebGLStateChangeBegin: () => {
// Save current WebGL state
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
onWebGLStateChangeEnd?: () => void

Invoked after the session changes any WebGL state.

Called after the session has completed its WebGL state modifications during rendering operations. Use this to restore any WebGL state that your application needs.

onWebGLStateChangeEnd: () => {
// Restore WebGL state
gl.bindFramebuffer(gl.FRAMEBUFFER, myFramebuffer);
}
onServerMessageReceived?: (messageData: Uint8Array) => void

Invoked when a server message is received through any opaque data channel.

Type declaration

    • (messageData: Uint8Array): void
    • Parameters

      • messageData: Uint8Array

        Raw message data from the server

      Returns void

onServerMessageReceived: (messageData) => {
const messageString = new TextDecoder().decode(messageData);
console.log('Received message:', messageString);

// Parse JSON if expected
try {
const message = JSON.parse(messageString);
console.log('Parsed message:', message);
} catch (error) {
console.log('Raw binary data:', messageData);
}
}