From 9ac8cc5eec23c3b511cc92b6b318f67fd998930b Mon Sep 17 00:00:00 2001 From: WarstekHUN Date: Wed, 27 Dec 2023 16:46:28 +0100 Subject: [PATCH] Support for extending the ZilaServer class | 2.1.0 --- .github/workflows/publish.yml | 2 ++ .github/workflows/test.yml | 6 ----- CHANGELOG.md | 6 ++--- README.md | 51 ++++++++++++++++++++++++++++++++++- package-lock.json | 4 +-- package.json | 2 +- src/index.ts | 23 +++------------- 7 files changed, 61 insertions(+), 33 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index aad2f49..dd45397 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,6 +4,8 @@ on: push: tags: - 'v*' + branches: + - main jobs: release: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d3f1e77..abb7547 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,18 +7,12 @@ on: - 'README.md' - 'LICENSE' - '.editorconfig' - branches: - - main - - master pull_request: paths-ignore: - 'docs/**' - 'README.md' - 'LICENSE' - '.editorconfig' - branches: - - main - - master jobs: test: diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e6e96a..f0f621c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -# v2.0.5 - -* README fix +# Additions +* More types are exported to make extending the ZilaServer class much easier. +* README typo fix \ No newline at end of file diff --git a/README.md b/README.md index 00d9a41..60a08f2 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ZilaWS is a blazingly fast and very lightweight library that provides an extreme ## Looking for the [zilaws-client](https://www.npmjs.com/package/zilaws-client) package? -The ZilaWS Server can accept WS connections from non ZilaWS clients but won't work as expected. +The ZilaWS Server can accept WS connections from non-ZilaWS clients but won't work as expected. ## Waiters @@ -129,6 +129,55 @@ server.onceMessageHandler("Anything", (socket) => { }); ``` +## Extending the ZilaServer class + +You also have the ability to extend the ZilaServer class if you need to. This comes in handy if for example you need to convert data automatically. + +```ts +import { IServerSettings, ZilaClient, ZilaServer, ZilaWSCallback } from "zilaws-server"; + +enum MessageHandlers { + Register, + Login, + //... +} + +class MyServer extends ZilaServer { + constructor(settings: IServerSettings) { + super(settings); + } + + setMessageHandler(identifier: MessageHandlers | string, callback: ZilaWSCallback): void { + super.setMessageHandler(identifier.toString(), callback); + } +} + +const server = new MyServer({ + port: 80, + clientClass: MyClient +}); + +server.setMessageHandler(MessageHandlers.Login, async (socket: MyClient, username: string, password: string) => { + //Logging in a user + const dbUser = await CheckLoginCredentials(username, password); + + if(dbUser) { + const loginToken = generateToken(); + socket.setCookie({ + name: "LoginToken", + value: loginToken, + expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30) + }); + + socket.clientData = dbUser; + + return "SUCCESS"; + }else{ + return "BAD_CREDENTIALS"; + } +}); +``` + ## More ZilaWS offers much more. Check out the [documentation](https://zilaws.com/)! diff --git a/package-lock.json b/package-lock.json index 675b7f4..8a715e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zilaws-server", - "version": "2.0.5", + "version": "2.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "zilaws-server", - "version": "2.0.5", + "version": "2.1.0", "license": "MIT", "dependencies": { "@types/node": "^20.10.5", diff --git a/package.json b/package.json index 4592947..3cc97c7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "zilaws-server", - "version": "2.0.5", + "version": "2.1.0", "description": "ZilaWS is a blazingly fast and very lightweight library that provides an extremely easy-to-use way to transmit data via websockets between client-side and server-side using eventhandlers and async waiters.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/src/index.ts b/src/index.ts index 295c7b5..918155e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import { IWSMessage } from "./IWSMessage"; import { ZilaWSCallback } from "./ZilaWSCallback"; import { parse as parseCookie } from "cookie"; -interface IServerSettings { +export interface IServerSettings { /** * The port of the WebSocket server */ @@ -81,7 +81,7 @@ interface IServerSettings { ) => ZilaClient; } -interface IServerEvents { +export interface IServerEvents { /** * Runs every time a client connects. * @param socket @@ -193,23 +193,6 @@ export class ZilaServer { return data["version"]; } - /* istanbul ignore next */ - private compareVersions(version1: string, version2: string) { - let v1 = version1.split(".").map(Number); - let v2 = version2.split(".").map(Number); - let len = Math.max(v1.length, v2.length); - - for (let i = 0; i < len; i++) { - let part1 = v1[i] || 0; - let part2 = v2[i] || 0; - - if (part1 > part2) return 1; - if (part1 < part2) return 2; - } - - return 0; - } - public constructor(settings: IServerSettings) { this.settings = settings; this.hasrequested = false; @@ -722,4 +705,4 @@ function getIPAndPort(req: IncomingMessage): string { return `${req.socket.remoteAddress}:${req.socket.remotePort}`; } -export { ZilaClient, CloseCodes, WSStatus, IncomingHttpHeaders, WebSocketClient }; +export { ZilaClient, CloseCodes, WSStatus, IncomingHttpHeaders, WebSocketClient, ZilaWSCallback };