add debug

This commit is contained in:
Rizky 2023-10-21 16:51:21 +07:00
parent 214ffd84a8
commit 885bf0e7fc
1 changed files with 12 additions and 2 deletions

View File

@ -13,6 +13,9 @@ import { SyncType } from "../../../../srv/ws/sync/type";
import { w } from "../types/general"; import { w } from "../types/general";
import { ESite } from "../../render/ed/logic/ed-global"; import { ESite } from "../../render/ed/logic/ed-global";
const packr = new Packr({ structuredClone: true }); const packr = new Packr({ structuredClone: true });
const WS_DEBUG = false;
const conf = { const conf = {
ws: null as null | WebSocket, ws: null as null | WebSocket,
client_id: "", client_id: "",
@ -37,6 +40,11 @@ type User = {
export type ClientEventObject = Parameters<typeof clientStartSync>[0]["events"]; export type ClientEventObject = Parameters<typeof clientStartSync>[0]["events"];
export type ClientEvent = keyof ClientEventObject; export type ClientEvent = keyof ClientEventObject;
const sendWs = (ws: WebSocket, msg: any) => {
if (WS_DEBUG) console.log(`%c⬆`, "color:blue", msg);
ws.send(packr.pack(msg));
};
export const clientStartSync = async (arg: { export const clientStartSync = async (arg: {
user_id: string; user_id: string;
events: { events: {
@ -103,7 +111,7 @@ const connect = (user_id: string, event: ClientEventObject) => {
const ws = new WebSocket(url.toString()); const ws = new WebSocket(url.toString());
ws.onopen = () => { ws.onopen = () => {
ws.send(packr.pack({ type: SyncType.UserID, user_id })); sendWs(ws, { type: SyncType.UserID, user_id });
conf.ws = ws; conf.ws = ws;
}; };
ws.onclose = async () => { ws.onclose = async () => {
@ -116,6 +124,8 @@ const connect = (user_id: string, event: ClientEventObject) => {
ws.onmessage = async (e) => { ws.onmessage = async (e) => {
const raw = e.data as Blob; const raw = e.data as Blob;
const msg = packr.unpack(Buffer.from(await raw.arrayBuffer())); const msg = packr.unpack(Buffer.from(await raw.arrayBuffer()));
if (WS_DEBUG) console.log(`%c⬇`, `color:red`, msg);
if (msg.type === SyncType.ClientID) { if (msg.type === SyncType.ClientID) {
conf.client_id = msg.client_id; conf.client_id = msg.client_id;
resolve(); resolve();
@ -182,7 +192,7 @@ const doAction = async <T>(arg: {
resolve, resolve,
}; };
ws.send(packr.pack({ type: SyncType.Action, code, args, argid })); sendWs(ws, { type: SyncType.Action, code, args, argid });
} else { } else {
// offline // offline
const cache = await get(argid, idb); const cache = await get(argid, idb);