adrift/client/DevWsTransport.ts
2023-08-12 13:12:58 -04:00

27 lines
612 B
TypeScript

import Transport from "../protocol/Transport";
export class DevWsTransport extends Transport {
ws: WebSocket;
constructor(onopen, onclose) {
super(onopen, onclose);
this.ws = new WebSocket("ws://localhost:3000/dev-ws");
this.ws.binaryType = "arraybuffer";
this.ws.onopen = onopen;
this.ws.onclose = onclose;
this.ws.onmessage = this.onmessage.bind(this);
}
onmessage(msg: MessageEvent<any>) {
if (msg.data instanceof ArrayBuffer) {
this.ondata(msg.data);
return;
}
// ignore text messages
}
send(data: ArrayBuffer) {
this.ws.send(data);
}
}