Updates
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendData(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
data: WebSocketDataType,
|
||||
) {
|
||||
ws.send(String(EJSON.stringify(data)));
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendError(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message?: String,
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:error",
|
||||
message: message,
|
||||
} as WebSocketDataType),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendMessage(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String,
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:message",
|
||||
message: message,
|
||||
} as WebSocketDataType),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendReady(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message?: String,
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:ready",
|
||||
message: message,
|
||||
} as WebSocketDataType),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendSuccess(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String,
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:success",
|
||||
message: message,
|
||||
} as WebSocketDataType),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "@/src/types";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
const EJSON = datasquirel.client.utils.EJSON;
|
||||
|
||||
export default function sendUpdate(
|
||||
ws: ServerWebSocket<WebSocketData>,
|
||||
message: String,
|
||||
) {
|
||||
ws.send(
|
||||
String(
|
||||
EJSON.stringify({
|
||||
event: "server:update",
|
||||
message: message,
|
||||
} as WebSocketDataType),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import sendData from "../(utils)/send-data";
|
||||
import sendError from "../(utils)/send-error";
|
||||
import { WebSocketMessageParam } from "../socket-message";
|
||||
|
||||
export default async function socketClientPing({
|
||||
ws,
|
||||
data,
|
||||
}: WebSocketMessageParam) {
|
||||
try {
|
||||
sendData(ws, {
|
||||
event: "server:ping",
|
||||
});
|
||||
} catch (error: any) {
|
||||
sendError(ws, "Client Ping Error! " + error.message);
|
||||
}
|
||||
}
|
||||
+10
-2
@@ -1,5 +1,6 @@
|
||||
import { WebSocketData } from "../types";
|
||||
import socketInit from "./socket-init";
|
||||
import socketMessage from "./socket-message";
|
||||
|
||||
const server = Bun.serve<WebSocketData>({
|
||||
async fetch(req, server) {
|
||||
@@ -22,10 +23,17 @@ const server = Bun.serve<WebSocketData>({
|
||||
websocket: {
|
||||
async message(ws, message) {
|
||||
if (typeof message == "string") {
|
||||
await socketMessage({ ws, message });
|
||||
}
|
||||
},
|
||||
async open(ws) {},
|
||||
async close(ws, code, message) {},
|
||||
async open(ws) {
|
||||
const user = ws.data.user;
|
||||
console.log(`Web Socket Opened by ${user.first_name}`);
|
||||
},
|
||||
async close(ws, code, message) {
|
||||
const user = ws.data.user;
|
||||
console.log(`Socket Closed by ${user.first_name}`);
|
||||
},
|
||||
idleTimeout: 600,
|
||||
maxPayloadLength: 1024 * 1024 * 10,
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import { User } from "../types";
|
||||
import userAuth from "../utils/user-auth";
|
||||
|
||||
type Param = {
|
||||
req: Request;
|
||||
@@ -7,7 +8,7 @@ type Param = {
|
||||
};
|
||||
|
||||
type Return = {
|
||||
user: User | null;
|
||||
user?: User | null;
|
||||
};
|
||||
|
||||
export default async function socketInit({
|
||||
@@ -25,15 +26,11 @@ export default async function socketInit({
|
||||
user: null,
|
||||
};
|
||||
|
||||
const user = datasquirel.user.auth.auth({
|
||||
cookieString,
|
||||
database: process.env.DSQL_DB_NAME || "",
|
||||
skipFileCheck: true,
|
||||
});
|
||||
const { singleRes: user } = await userAuth({ bun_req: req });
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::socketInit:user", user);
|
||||
}
|
||||
|
||||
return { user: user.payload as User | null };
|
||||
return { user };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { ServerWebSocket } from "bun";
|
||||
import { WebSocketData, WebSocketDataType } from "../types";
|
||||
import { EJSON } from "../exports/client-exports";
|
||||
import socketClientPing from "./events/client-ping";
|
||||
import debugLog from "@moduletrace/datasquirel/dist/package-shared/utils/logging/debug-log";
|
||||
|
||||
type Param = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
message: string | Buffer;
|
||||
};
|
||||
|
||||
export type WebSocketMessageParam = {
|
||||
ws: ServerWebSocket<WebSocketData>;
|
||||
message?: string | Buffer;
|
||||
data?: WebSocketDataType;
|
||||
};
|
||||
|
||||
export default async function socketMessage({ ws, message }: Param) {
|
||||
const user = ws.data.user;
|
||||
const data = EJSON.parse(message.toString()) as
|
||||
| WebSocketDataType
|
||||
| undefined;
|
||||
|
||||
const websocketMessageParams: WebSocketMessageParam = {
|
||||
ws,
|
||||
data,
|
||||
message,
|
||||
};
|
||||
|
||||
const userRef = `User ${user.first_name} ${user.last_name} [#${user.id}]`;
|
||||
const label = "Web Socket Message";
|
||||
|
||||
switch (data?.event) {
|
||||
case "client:ping":
|
||||
debugLog({
|
||||
log: `${userRef} Pinging Server ...`,
|
||||
addTime: true,
|
||||
label,
|
||||
});
|
||||
await socketClientPing(websocketMessageParams);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user