Refactored App component in front. Implemented pnpm workspaces. Separated messages types into package
This commit is contained in:
1
apps/back/src/constants.ts
Normal file
1
apps/back/src/constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const idMsgTypes = ["freed", "occupied"] as const;
|
6
apps/back/src/db/config.example.ts
Normal file
6
apps/back/src/db/config.example.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export const config = {
|
||||
host: "",
|
||||
username: "",
|
||||
password: "",
|
||||
database: "",
|
||||
};
|
29
apps/back/src/db/index.ts
Normal file
29
apps/back/src/db/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Connection, createConnection } from "typeorm";
|
||||
import fs from "fs";
|
||||
|
||||
import { Room } from "./model";
|
||||
import { config } from "./config";
|
||||
|
||||
export { Room };
|
||||
|
||||
export const connect = () =>
|
||||
createConnection({
|
||||
...config,
|
||||
type: "cockroachdb",
|
||||
port: 26257,
|
||||
ssl: {
|
||||
ca: fs.readFileSync("certs/cc-ca.crt").toString(),
|
||||
},
|
||||
synchronize: true,
|
||||
logging: false,
|
||||
entities: [Room],
|
||||
});
|
||||
|
||||
export const getRoomList = (connection: Connection) =>
|
||||
connection.manager.find(Room);
|
||||
|
||||
export const updateFree = (
|
||||
connection: Connection,
|
||||
id: number,
|
||||
value: boolean
|
||||
) => connection.manager.update(Room, id, { free: value });
|
27
apps/back/src/db/model.ts
Normal file
27
apps/back/src/db/model.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export abstract class Room {
|
||||
@PrimaryGeneratedColumn()
|
||||
abstract id: number;
|
||||
|
||||
@Column({
|
||||
length: 100,
|
||||
})
|
||||
abstract title: string;
|
||||
|
||||
@Column()
|
||||
abstract free: boolean;
|
||||
|
||||
@Column()
|
||||
abstract x: number;
|
||||
|
||||
@Column()
|
||||
abstract y: number;
|
||||
|
||||
@Column()
|
||||
abstract width: number;
|
||||
|
||||
@Column()
|
||||
abstract height: number;
|
||||
}
|
46
apps/back/src/index.ts
Normal file
46
apps/back/src/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import "reflect-metadata";
|
||||
import { Server, OPEN } from "ws";
|
||||
import { connect, getRoomList, updateFree } from "./db";
|
||||
import { isMessage, isUpdateMessage } from "@roomruler/messages";
|
||||
|
||||
const main = async () => {
|
||||
const connection = await connect();
|
||||
|
||||
const wss = new Server(
|
||||
{
|
||||
port: Number.parseInt(process.env.PORT || "") || 8081,
|
||||
},
|
||||
() => console.log(`Started server on ${process.env.PORT || 8081}`)
|
||||
);
|
||||
|
||||
wss.on("connection", async (wsc, req) => {
|
||||
console.log("New user connected from " + req.socket.remoteAddress);
|
||||
wsc.send(JSON.stringify(await getRoomList(connection)));
|
||||
|
||||
wsc.on("message", async (data) => {
|
||||
console.log("Got message from " + req.socket.remoteAddress);
|
||||
try {
|
||||
const message: unknown = JSON.parse(data.toString());
|
||||
if (!isMessage(message)) throw new Error("Message corrupted");
|
||||
|
||||
if (isUpdateMessage(message)) {
|
||||
console.log(
|
||||
`Processing message of \"${message.type}\" type from ${req.socket.remoteAddress}`
|
||||
);
|
||||
|
||||
const { id, value } = message.args;
|
||||
await updateFree(connection, id, value);
|
||||
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState === OPEN)
|
||||
client.send(JSON.stringify(message));
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Error processing message", err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
main();
|
Reference in New Issue
Block a user