Skip to content

Commit

Permalink
Support for extending the ZilaServer class | 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WarstekHUN committed Dec 27, 2023
1 parent 78bb719 commit 9ac8cc5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
tags:
- 'v*'
branches:
- main

jobs:
release:
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?</h2>

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

Expand Down Expand Up @@ -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<T extends ZilaClient> extends ZilaServer<T> {
constructor(settings: IServerSettings) {
super(settings);
}

setMessageHandler(identifier: MessageHandlers | string, callback: ZilaWSCallback<T>): void {
super.setMessageHandler(identifier.toString(), callback);
}
}

const server = new MyServer<MyClient>({
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/)!
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 3 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -81,7 +81,7 @@ interface IServerSettings {
) => ZilaClient;
}

interface IServerEvents<T extends ZilaClient> {
export interface IServerEvents<T extends ZilaClient> {
/**
* Runs every time a client connects.
* @param socket
Expand Down Expand Up @@ -193,23 +193,6 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
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;
Expand Down Expand Up @@ -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 };

0 comments on commit 9ac8cc5

Please sign in to comment.