UNPKG

@needle-tools/engine

Version:

Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.

38 lines 1.36 kB
/** Default transport that wraps the `websocket-ts` library */ export class WebsocketTransport { _url; _ws; onOpen = null; onClose = null; onError = null; onMessage = null; get url() { return this._ws?.url ?? this._url; } constructor(url) { this._url = url; } async start() { const pkg = await import('websocket-ts'); // @ts-ignore const WebsocketBuilder = pkg.default?.WebsocketBuilder ?? pkg.WebsocketBuilder; const ExponentialBackoff = pkg.default?.ExponentialBackoff ?? pkg.ExponentialBackoff; const ws = new WebsocketBuilder(this._url) .withMaxRetries(10) .withBackoff(new ExponentialBackoff(2000, 4)) .onOpen(() => { this._ws = ws; this.onOpen?.(); }) .onClose(() => { this.onClose?.(); }) .onError(() => { this.onError?.(new Error("Websocket connection failed")); }) .onRetry(() => { console.log("Retry connecting to networking websocket"); }) .build(); ws.addEventListener(pkg.WebsocketEvent.message, (_, msg) => { this.onMessage?.(msg.data); }); } send(data) { this._ws?.send(data); } close() { this._ws?.close(); this._ws = undefined; } } //# sourceMappingURL=engine_networking.transport.websocket.js.map