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

interface SessionDelegates {
onStreamStarted ?: () => void;
onStreamStopped ?: (error?: Error) => void;
onWebGLStateChangeBegin ?: () => void;
onWebGLStateChangeEnd ?: () => void;
onServerMessageReceived ?: (messageData: Uint8Array) => void;
}

#####
Index

### Properties

onStreamStarted?
onStreamStopped?
onWebGLStateChangeBegin?
onWebGLStateChangeEnd?
onServerMessageReceived?

##
Properties

### `Optional` onStreamStarted

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.

#### Example

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

### `Optional` onStreamStopped

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

    - `Optional` error: Error

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

    #### Returns void

#### Example

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

### `Optional` onWebGLStateChangeBegin

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.

#### Example

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

### `Optional` onWebGLStateChangeEnd

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.

#### Example

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

### `Optional` onServerMessageReceived

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

#### Example

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