import NodeWebSocket from "ws" ; import { ITransport , ITransportEventMap } from "./ITransport" ; const WebSocket = globalThis . WebSocket || NodeWebSocket ; export class WebSocketTransport implements ITransport { ws : WebSocket | NodeWebSocket ; protocols ? : string | string [ ] ; constructor ( public events ) { } public send ( data ) { if ( data instanceof ArrayBuffer ) { this . ws . send ( data ) ; } else if ( Array . isArray ( data ) ) { this . ws . send ( ( new Uint8Array ( data ) ) . buffer ) ; } } public connect ( url ) { this . ws = new WebSocket ( url , this . protocols ) ; this . ws . binaryType = 'arraybuffer' ; this . ws . onopen = this . events . onopen ; this . ws . onmessage = this . events . onmessage ; this . ws . onclose = this . events . onclose ; this . ws . onerror = this . events . onerror ; } public close ( code ? , reason ? ) { this . ws . close ( code , reason ) ; } }