Function: createSession()#

createSession(options, delegates?): Session

Creates a new CloudXR streaming session with the provided configuration.

This is the primary entry point for creating CloudXR streaming sessions. The returned session is ready to be connected to the CloudXR Runtime using the connect() method.

Parameters#

options#

SessionOptions

Configuration options for the session. Must include all required parameters such as serverAddress, serverPort, gl context, and per-eye dimensions.

delegates?#

SessionDelegates

Optional delegate object to receive essential session events such as onStreamStarted, onStreamStopped, and WebGL state change notifications.

Returns#

Session

A Session object ready to connect to the CloudXR Runtime

Throws#

When perEyeWidth or perEyeHeight is not a positive integer, perEyeWidth not a multiple of 16, perEyeHeight not a multiple of 64, or either is less than 256

Examples#

// Basic session creation
const session = createSession({
  serverAddress: '192.168.1.100',
  serverPort: 49100,
  useSecureConnection: false,
  gl: webglContext,
  perEyeWidth: 2048,
  perEyeHeight: 1792,
  referenceSpace: xrReferenceSpace
});

// With event delegates
const session = createSession(sessionOptions, {
  onStreamStarted: () => {
    console.info('CloudXR streaming started');
  },
  onStreamStopped: (error) => {
    if (error) {
      console.error('Streaming error:', error);
    } else {
      console.info('Streaming stopped normally');
    }
  }
});

// Connect to CloudXR Runtime
if (session.connect()) {
  console.info('Connection initiated');
}
// Complete WebXR integration example
async function setupCloudXR() {
  // Request WebXR session
  const xrSession = await navigator.xr.requestSession('immersive-vr', {
    requiredFeatures: ['local-floor']
  });

  // Get WebGL context and reference space
  const gl = xrSession.renderState.baseLayer.context;
  const referenceSpace = await xrSession.requestReferenceSpace('local-floor');

  // Create CloudXR streaming session
  const session = createSession({
    serverAddress: 'your-server-ip',
    serverPort: 49100,
    useSecureConnection: false,
    gl: gl,
    perEyeWidth: 2048,
    perEyeHeight: 1792,
    referenceSpace: referenceSpace,
    deviceFrameRate: 90,
    maxStreamingBitrateKbps: 150000
  }, {
    onStreamStarted: () => console.info('Ready to render'),
    onStreamStopped: (error) => console.info('Streaming stopped', error)
  });

  // Connect and start rendering
  if (session.connect()) {
    function renderFrame(time, frame) {
      session.sendTrackingStateToServer(time, frame);
      session.render(time, frame, xrSession.renderState.baseLayer);
      xrSession.requestAnimationFrame(renderFrame);
    }
    xrSession.requestAnimationFrame(renderFrame);
  }
}