fixed module standart comparability, now ws sends initial data in message format
This commit is contained in:
parent
8be147c3b8
commit
f0df9d9233
@ -2,11 +2,15 @@
|
||||
"name": "@roomruler/back",
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"build": "tsc --build",
|
||||
"build": "rollup --config rollup.config.js",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-node-resolve": "^13.0.4",
|
||||
"@rollup/plugin-typescript": "^8.2.5",
|
||||
"@types/ws": "^7.4.7",
|
||||
"rollup": "^2.56.3",
|
||||
"tslib": "^2.3.1",
|
||||
"typescript": "^4.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
|
11
apps/back/rollup.config.js
Normal file
11
apps/back/rollup.config.js
Normal file
@ -0,0 +1,11 @@
|
||||
const typescript = require("@rollup/plugin-typescript");
|
||||
const { nodeResolve } = require("@rollup/plugin-node-resolve");
|
||||
|
||||
export default {
|
||||
input: "src/index.ts",
|
||||
output: {
|
||||
dir: "dist",
|
||||
format: "cjs",
|
||||
},
|
||||
plugins: [typescript(), nodeResolve({ resolveOnly: [/^@roomruler\/.*$/] })],
|
||||
};
|
@ -1,7 +1,12 @@
|
||||
import "reflect-metadata";
|
||||
import { Server, OPEN } from "ws";
|
||||
import { connect, getRoomList, updateFree } from "./db";
|
||||
import { isMessage, isUpdateMessage } from "@roomruler/messages";
|
||||
import {
|
||||
composeMessage,
|
||||
isMessage,
|
||||
isUpdateMessage,
|
||||
ListMessage,
|
||||
} from "@roomruler/messages";
|
||||
|
||||
const main = async () => {
|
||||
const connection = await connect();
|
||||
@ -15,7 +20,11 @@ const main = async () => {
|
||||
|
||||
wss.on("connection", async (wsc, req) => {
|
||||
console.log("New user connected from " + req.socket.remoteAddress);
|
||||
wsc.send(JSON.stringify(await getRoomList(connection)));
|
||||
wsc.send(
|
||||
JSON.stringify(
|
||||
composeMessage<ListMessage>("list", await getRoomList(connection))
|
||||
)
|
||||
);
|
||||
|
||||
wsc.on("message", async (data) => {
|
||||
console.log("Got message from " + req.socket.remoteAddress);
|
||||
|
@ -5,5 +5,6 @@
|
||||
"module": "commonjs",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "../../packages/messages/tsconfig.json" }]
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
"main": "dist/src/index.js",
|
||||
"version": "0.0.0",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
|
@ -11,6 +11,21 @@ export type UpdateMessage = Message & {
|
||||
};
|
||||
};
|
||||
|
||||
export type Room = {
|
||||
id: number;
|
||||
title: string;
|
||||
free: boolean;
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
|
||||
export type ListMessage = Message & {
|
||||
type: "list";
|
||||
args: Room[];
|
||||
};
|
||||
|
||||
const isObjLike = (obj: unknown): obj is object =>
|
||||
Boolean(obj) && typeof obj === "object";
|
||||
|
||||
@ -31,3 +46,40 @@ export const isUpdateMessage = (message: Message): message is UpdateMessage =>
|
||||
typeof message.args.id === "number" &&
|
||||
hasProperty(message.args, "value") &&
|
||||
typeof message.args.value === "boolean";
|
||||
|
||||
export const isRoom = (obj: unknown): obj is Room => {
|
||||
if (!(typeof obj === "object") || !obj) return false;
|
||||
|
||||
const ns = ["id", "x", "y", "width", "height"] as const;
|
||||
for (const key of ns) {
|
||||
if (!hasProperty(obj, key) || typeof obj[key] !== "number") return false;
|
||||
}
|
||||
|
||||
if (
|
||||
!hasProperty(obj, "title") ||
|
||||
typeof obj.title !== "string" ||
|
||||
!hasProperty(obj, "free") ||
|
||||
typeof obj.free !== "boolean"
|
||||
)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
export const isArrayOf = <T extends unknown>(
|
||||
obj: T,
|
||||
itemCheck: (arg: T) => boolean
|
||||
) => {
|
||||
if (Array.isArray(obj)) {
|
||||
for (const el of obj) if (!itemCheck(el)) return false;
|
||||
return true;
|
||||
} else return false;
|
||||
};
|
||||
|
||||
export const composeMessage = <T extends Message>(
|
||||
type: T["type"],
|
||||
args: T["args"]
|
||||
): Message => ({
|
||||
type,
|
||||
args,
|
||||
});
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"module": "es6",
|
||||
"target": "es6",
|
||||
"outDir": "dist",
|
||||
"composite": true
|
||||
},
|
||||
"include": ["src"]
|
||||
"include": ["src"],
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user