feat: some of the server

This commit is contained in:
1 2022-12-01 18:40:39 -05:00
commit 6c135bf91a
19 changed files with 247 additions and 0 deletions

0
client/OUTLINE.md Normal file
View file

2
server/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
package-lock.json
node_modules/

0
server/OUTLINE.md Normal file
View file

15
server/package.json Normal file
View file

@ -0,0 +1,15 @@
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"socket.io": "^4.5.4"
}
}

12
server/src/IRC.ts Normal file
View file

@ -0,0 +1,12 @@
import { PacketManager } from "./packets/PacketManager";
import { RoomManager } from "./rooms/RoomManager";
export class IRC {
public readonly _packetManager: PacketManager = new PacketManager();
public readonly _roomManager: RoomManager = new RoomManager();
static _packetManager: any;
public constructor() {
this._packetManager = this._packetManager;
}
}

View file

@ -0,0 +1,14 @@
/**
* @description A base Message
*/
export class Message {
public messageId: number;
public message: string;
public sentBy: number; // The id of the user who sent the message
public constructor(messageId: number, message: string, sentBy: number) {
this.messageId = messageId;
this.message = message;
this.sentBy = sentBy;
}
}

21
server/src/base/Packet.ts Normal file
View file

@ -0,0 +1,21 @@
import { PacketType } from "../types/PacketType";
/**
* @description The base packet a server will send and recieve
*/
export class Packet {
public id: number;
public data: any;
public cancelled: boolean = false;
public type: PacketType;
public constructor(id: number, data: any, type: PacketType) {
this.id = id;
this.data = data;
this.type = type;
}
set cancel(cancel: boolean) {
this.cancelled = cancel;
}
}

17
server/src/base/Room.ts Normal file
View file

@ -0,0 +1,17 @@
import { Message } from "./Message";
import { User } from "./User";
/**
* @description The base room for all rooms
*/
export class Room {
public id: number;
public name: string;
public connected: User[] = [];
public messages: Message[] = [];
public constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}

14
server/src/base/User.ts Normal file
View file

@ -0,0 +1,14 @@
import { Room } from "./Room";
/**
* @description A user in a room
*/
export class User {
public id: number;
public name: string;
public constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}

11
server/src/index.ts Normal file
View file

@ -0,0 +1,11 @@
import { Server } from "socket.io";
import { IRC } from "./IRC";
const io: Server = new Server();
const irc: IRC = new IRC();
io.on("connection", (socket) => {
IRC._packetManager.handleConnection(socket);
});
io.listen(3000);

View file

@ -0,0 +1,27 @@
import { Packet } from "../base/Packet";
import { User } from "../base/User";
/**
* @description This handles all ingoing/outgoing packets in a event based system
*/
export class PacketManager {
public static packetMap: Map<number, Packet> = new Map();
public static packetMapOut: Map<number, Packet> = new Map();
public static packetMapServer: Map<User, Packet> = new Map();
public static registerPacket(packet: Packet): void {
this.packetMap.set(packet.id, packet);
}
public static registerPacketOut(packet: Packet): void {
this.packetMapOut.set(packet.id, packet);
}
public static getPacket(id: number): Packet | undefined {
return this.packetMap.get(id);
}
public static registerServerPacket(packet: Packet, to: User): void {
this.packetMapServer.set(to, packet);
}
};

View file

@ -0,0 +1,26 @@
import { Packet } from "../../base/Packet";
import { Room } from "../../base/Room";
import { User } from "../../base/User";
/**
* @description The packet sent by the client to signify an initial connection
* @notice If the packet is not sent within 1 second of the connection, the connection will be closed
* This also starts the keep alive timer
*/
export class C01InitialConnectPacket extends Packet {
public constructor(user: User, room: Room) {
super(1, null, 0);
/*
Expected data:
{
user: {
name: string,
},
room: {
name: string, // The expected room name to join
}
}
*/
this.data = { user, room };
}
}

View file

@ -0,0 +1,10 @@
import { Packet } from "../../base/Packet";
/**
* @description The packet sent by the client to keep the connection alive
*/
export class C02KeepAlivePacket extends Packet {
public constructor() {
super(2, null, 0);
}
}

View file

@ -0,0 +1,10 @@
import { Packet } from "../../base/Packet";
/**
* @description The packet sent by the client to join a room
*/
export class C03JoinRoomPacket extends Packet {
public constructor() {
super(3, null, 0);
}
}

View file

@ -0,0 +1,11 @@
import { Packet } from "../../base/Packet";
/**
* @description The packet sent by the server to broadcast a message to all clients
*/
export class S01BroadcastMessagePacket extends Packet {
public constructor(message: string) {
super(1, null, 1);
this.data = message;
}
}

View file

@ -0,0 +1,11 @@
import { Packet } from "../../base/Packet";
/**
* @description The packet sent by the server to send a error message
*/
export class S02ErrPacket extends Packet {
public constructor(message: string) {
super(2, null, 1);
this.data = message;
}
}

View file

@ -0,0 +1,12 @@
import { Packet } from "../../base/Packet";
import { User } from "../../base/User";
/**
* @description The packet sent by the server signify the connection was accepted
*/
export class S03ConnectionAcceptedPacket extends Packet {
public constructor(user: User) {
super(3, null, 1);
this.data = user;
}
}

View file

@ -0,0 +1,30 @@
import { Room } from "../base/Room";
import { User } from "../base/User";
import { IRC } from "../IRC";
import { S01BroadcastMessagePacket } from "../packets/server/S01BroadcastMessagePacket";
import { S02ErrPacket } from "../packets/server/S02ErrPacket";
export class RoomManager {
private rooms: Room[] = [];
public addRoom(room: Room): void {
this.rooms.push(room);
}
public handleJoin(joined: Room, user: User) {
this.rooms.forEach(room => {
if(room.id === joined.id) {
room.connected.push(user);
IRC._packetManager.registerServerPacket(user, new S01BroadcastMessagePacket(`${user.name} joined the room`));
room.connected.forEach(user => {
IRC._packetManager.registerServerPacket(user, new S01BroadcastMessagePacket(`${user.name} joined the room`));
});
return;
}
});
IRC._packetManager.registerServerPacket(user, new S02ErrPacket(`Failed joining that room.`));
return;
}
}

View file

@ -0,0 +1,4 @@
export enum PacketType {
CLIENT = 0,
SERVER = 1
}