Interface: SessionDelegates#

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.

Example#

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');
    }
  }
};

Properties#

onStreamStarted()?#

optional 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.

Returns#

void

Example#

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

onStreamStopped()?#

optional onStreamStopped: (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.

Parameters#

error?#

StreamingError

Optional StreamingError if streaming stopped due to an error. If undefined, the stop was intentional (e.g., disconnect() called). Error objects include: - message: User-friendly error description - code: Error code for debugging/reporting (numeric, e.g., 0xC0F2220C) - reasonCode: Stream stop reason code (when available)

Returns#

void

Example#

onStreamStopped: (error) => {
  if (error) {
    // Display user-friendly message
    console.error('Streaming error:', error.message);

    // Include error code for reporting/debugging (format as hex)
    if (error.code) {
      console.error('Error code: 0x' + error.code.toString(16).toUpperCase());
    }
  } else {
    console.info('Streaming stopped normally');
  }
}

onWebGLStateChangeBegin()?#

optional 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.

Returns#

void

Example#

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

onWebGLStateChangeEnd()?#

optional 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.

Returns#

void

Example#

onWebGLStateChangeEnd: () => {
  // Restore WebGL state
  gl.bindFramebuffer(gl.FRAMEBUFFER, myFramebuffer);
}

onServerMessageReceived()?#

optional onServerMessageReceived: (messageData) => void

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

Parameters#

messageData#

Uint8Array

Raw message data from the server

Returns#

void

Deprecated#

Use session.availableMessageChannels to get MessageChannel objects and receive messages directly.

Example#

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

onMetrics()?#

optional onMetrics: (metrics, cadence) => void

Invoked when metrics are produced by the session.

Parameters#

metrics#

Metrics

Object containing metrics by name

cadence#

MetricsCadence

The cadence at which the metrics are presented

Returns#

void