Spaces:
Paused
Paused
websocket client
Browse files- web/src/ws.js +12 -15
web/src/ws.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import { getToken } from './api.js';
|
| 2 |
|
| 3 |
-
const
|
| 4 |
-
const
|
| 5 |
|
| 6 |
export class DispatchSocket {
|
| 7 |
constructor(handlers) {
|
|
@@ -15,29 +15,27 @@ export class DispatchSocket {
|
|
| 15 |
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 16 |
this.ws = new WebSocket(`${proto}://${location.host}/ws`);
|
| 17 |
this.ws.binaryType = 'arraybuffer';
|
| 18 |
-
|
| 19 |
this.ws.onopen = () => {
|
| 20 |
this.retries = 0;
|
| 21 |
this.ws.send(JSON.stringify({ type: 'auth', token: getToken() }));
|
| 22 |
this.handlers.onStatus?.(true);
|
| 23 |
};
|
| 24 |
-
|
| 25 |
this.ws.onmessage = (ev) => {
|
| 26 |
if (typeof ev.data === 'string') {
|
| 27 |
let msg;
|
| 28 |
try { msg = JSON.parse(ev.data); } catch { return; }
|
| 29 |
switch (msg.type) {
|
| 30 |
-
case 'welcome':
|
| 31 |
-
case 'members':
|
| 32 |
-
case 'groups':
|
| 33 |
-
case 'sos_new':
|
| 34 |
case 'sos_update': this.handlers.onSosUpdate?.(msg.alert); break;
|
| 35 |
-
case 'floor':
|
| 36 |
-
case 'error':
|
| 37 |
}
|
| 38 |
} else if (ev.data instanceof ArrayBuffer && ev.data.byteLength > HDR) {
|
| 39 |
const dv = new DataView(ev.data);
|
| 40 |
-
if (dv.getUint8(0) !==
|
| 41 |
this.handlers.onAudio?.({
|
| 42 |
groupId: dv.getUint32(1),
|
| 43 |
memberId: dv.getUint32(5),
|
|
@@ -45,7 +43,6 @@ export class DispatchSocket {
|
|
| 45 |
});
|
| 46 |
}
|
| 47 |
};
|
| 48 |
-
|
| 49 |
this.ws.onclose = () => {
|
| 50 |
this.handlers.onStatus?.(false);
|
| 51 |
if (this.closed) return;
|
|
@@ -59,14 +56,14 @@ export class DispatchSocket {
|
|
| 59 |
if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify(obj));
|
| 60 |
}
|
| 61 |
|
| 62 |
-
sendAudio(groupId, memberId,
|
| 63 |
if (this.ws?.readyState !== WebSocket.OPEN) return;
|
| 64 |
const head = new Uint8Array(HDR);
|
| 65 |
const dv = new DataView(head.buffer);
|
| 66 |
-
dv.setUint8(0,
|
| 67 |
dv.setUint32(1, groupId);
|
| 68 |
dv.setUint32(5, memberId);
|
| 69 |
-
const body = new Uint8Array(
|
| 70 |
const out = new Uint8Array(HDR + body.byteLength);
|
| 71 |
out.set(head);
|
| 72 |
out.set(body, HDR);
|
|
|
|
| 1 |
import { getToken } from './api.js';
|
| 2 |
|
| 3 |
+
const HDR = 9; // type(1) + groupId(4) + memberId(4)
|
| 4 |
+
const AUDIO = 0x01;
|
| 5 |
|
| 6 |
export class DispatchSocket {
|
| 7 |
constructor(handlers) {
|
|
|
|
| 15 |
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
|
| 16 |
this.ws = new WebSocket(`${proto}://${location.host}/ws`);
|
| 17 |
this.ws.binaryType = 'arraybuffer';
|
|
|
|
| 18 |
this.ws.onopen = () => {
|
| 19 |
this.retries = 0;
|
| 20 |
this.ws.send(JSON.stringify({ type: 'auth', token: getToken() }));
|
| 21 |
this.handlers.onStatus?.(true);
|
| 22 |
};
|
|
|
|
| 23 |
this.ws.onmessage = (ev) => {
|
| 24 |
if (typeof ev.data === 'string') {
|
| 25 |
let msg;
|
| 26 |
try { msg = JSON.parse(ev.data); } catch { return; }
|
| 27 |
switch (msg.type) {
|
| 28 |
+
case 'welcome': this.handlers.onWelcome?.(msg); break;
|
| 29 |
+
case 'members': this.handlers.onMembers?.(msg.members); break;
|
| 30 |
+
case 'groups': this.handlers.onGroups?.(msg.groups); break;
|
| 31 |
+
case 'sos_new': this.handlers.onSosNew?.(msg.alert); break;
|
| 32 |
case 'sos_update': this.handlers.onSosUpdate?.(msg.alert); break;
|
| 33 |
+
case 'floor': this.handlers.onFloor?.(msg); break;
|
| 34 |
+
case 'error': console.warn('[ws]', msg.message); break;
|
| 35 |
}
|
| 36 |
} else if (ev.data instanceof ArrayBuffer && ev.data.byteLength > HDR) {
|
| 37 |
const dv = new DataView(ev.data);
|
| 38 |
+
if (dv.getUint8(0) !== AUDIO) return;
|
| 39 |
this.handlers.onAudio?.({
|
| 40 |
groupId: dv.getUint32(1),
|
| 41 |
memberId: dv.getUint32(5),
|
|
|
|
| 43 |
});
|
| 44 |
}
|
| 45 |
};
|
|
|
|
| 46 |
this.ws.onclose = () => {
|
| 47 |
this.handlers.onStatus?.(false);
|
| 48 |
if (this.closed) return;
|
|
|
|
| 56 |
if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify(obj));
|
| 57 |
}
|
| 58 |
|
| 59 |
+
sendAudio(groupId, memberId, int16buffer) {
|
| 60 |
if (this.ws?.readyState !== WebSocket.OPEN) return;
|
| 61 |
const head = new Uint8Array(HDR);
|
| 62 |
const dv = new DataView(head.buffer);
|
| 63 |
+
dv.setUint8(0, AUDIO);
|
| 64 |
dv.setUint32(1, groupId);
|
| 65 |
dv.setUint32(5, memberId);
|
| 66 |
+
const body = new Uint8Array(int16buffer);
|
| 67 |
const out = new Uint8Array(HDR + body.byteLength);
|
| 68 |
out.set(head);
|
| 69 |
out.set(body, HDR);
|