From 3ff67710e740d415038d3cef9df49ccf33fc8dca Mon Sep 17 00:00:00 2001 From: WarstekHUN Date: Sun, 24 Dec 2023 02:32:30 +0100 Subject: [PATCH] Bug fixed with event listeners | 2.0.2 --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 41 +++++++++++++++++++++-------------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index bd4bd69..c35f167 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zilaws-server", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "zilaws-server", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "dependencies": { "cookie": "^0.6.0", diff --git a/package.json b/package.json index 0923cd4..7a79871 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "zilaws-server", - "version": "2.0.1", + "version": "2.0.2", "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 917ceae..2d6cdad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -81,14 +81,14 @@ interface IServerSettings { ) => ZilaClient; } -interface IServerEvents { +interface IServerEvents { /** * Runs every time a client connects. * @param socket * @param req * @returns */ - onClientConnect: (socket: ZilaClient) => void; + onClientConnect: (socket: T) => void; /** * Runs every time a client disconnects @@ -97,7 +97,7 @@ interface IServerEvents { * @param reason * @returns */ - onClientDisconnect: (socket: ZilaClient, code: number, reason: string) => void; + onClientDisconnect: (socket: T, code: number, reason: string) => void; /** * Runs every time after a the server processes a message from the client. @@ -106,7 +106,7 @@ interface IServerEvents { * @param message If the message object is instance of T, this param will be T, undefined if not. * @returns */ - onClientMessage: (socket: ZilaClient, eventHandlerName: string, messageDataObject: T | undefined) => void; + onClientMessage: (socket: T, eventHandlerName: string, messageDataObject: T | undefined) => void; /** * Runs every time a server recieves a message from the client before any registered callback could run @@ -115,10 +115,10 @@ interface IServerEvents { * @param message If the message object is instance of T, this param will be T, undefined if not. * @returns */ - onClientMessageBeforeCallback: ( - socket: ZilaClient, + onClientMessageBeforeCallback: ( + socket: T, eventHandlerName: string, - messageDataObject: T | undefined + messageDataObject: U | undefined ) => void; /** @@ -127,7 +127,7 @@ interface IServerEvents { * @param rawMessage Not processed, raw message from the client. (Hopefully JSON) * @returns */ - onClientRawMessageBeforeCallback: (socket: ZilaClient, rawMessage: string) => void; + onClientRawMessageBeforeCallback: (socket: T, rawMessage: string) => void; /** * Runs when a client calls `syncCookies`. @@ -135,7 +135,7 @@ interface IServerEvents { * @param cookiesBeforeSync The cookies before syncing * @returns */ - onCookieSync: (socket: ZilaClient, cookiesBeforeSync: Map) => void; + onCookieSync: (socket: T, cookiesBeforeSync: Map) => void; } export class ZilaServer { @@ -147,7 +147,7 @@ export class ZilaServer { private clientClass: new ( socket: WebSocketClient, ip: string | undefined, - server: ZilaServer, + server: ZilaServer, isBrowser: boolean, headers: { [name: string]: string }, cookies?: Map @@ -158,7 +158,7 @@ export class ZilaServer { private baseServer: ServerHTTP | ServerHTTPS; private serverEvents: { - [K in keyof IServerEvents]?: Array | undefined; + [K in keyof IServerEvents]?: Array[K]> | undefined; } = {}; private readonly callbacks: { [id: string]: ZilaWSCallback | undefined } = {}; @@ -213,6 +213,7 @@ export class ZilaServer { public constructor(settings: IServerSettings) { this.settings = settings; this.hasrequested = false; + // @ts-ignore this.clientClass = settings.clientClass ?? ZilaClient; if (settings.maxWaiterTime) this.maxWaiterTime = settings.maxWaiterTime; @@ -332,7 +333,7 @@ export class ZilaServer { if (this.serverEvents.onClientConnect) { for (const cb of this.serverEvents.onClientConnect) { - cb(zilaSocket); + cb(zilaSocket as T); } } @@ -340,7 +341,7 @@ export class ZilaServer { const datastring = data.toString(); if (this.serverEvents.onClientRawMessageBeforeCallback) { for (const cb of this.serverEvents.onClientRawMessageBeforeCallback) { - cb(zilaSocket, datastring); + cb(zilaSocket as T, datastring); } } @@ -351,7 +352,7 @@ export class ZilaServer { zilaSocket.socket.addEventListener("close", (event) => { if (this.serverEvents.onClientDisconnect) { for (const cb of this.serverEvents.onClientDisconnect) { - cb(zilaSocket, event.code, event.reason); + cb(zilaSocket as T, event.code, event.reason); } } @@ -381,7 +382,7 @@ export class ZilaServer { * @param eventType * @param callback */ - public addEventListener(eventType: K, callback: IServerEvents[K]) { + public addEventListener>(eventType: K, callback: IServerEvents[K]) { let arr = this.serverEvents[eventType]; if (!arr) { arr = []; @@ -399,7 +400,7 @@ export class ZilaServer { * @param callback * @returns */ - public removeEventListener(eventType: K, callback: IServerEvents[K]) { + public removeEventListener>(eventType: K, callback: IServerEvents[K]) { let arr = this.serverEvents[eventType]; if (!arr) return; @@ -417,7 +418,7 @@ export class ZilaServer { * @param eventType * @param callback */ - public onceEventListener(eventType: K, callback: IServerEvents[K]) { + public onceEventListener>(eventType: K, callback: IServerEvents[K]) { const that = this; function onceCallback(...args: any[]) { @@ -673,7 +674,7 @@ export class ZilaServer { if (beforeCookies) { for (const cb of this.serverEvents.onCookieSync!) { - cb(socket, beforeCookies); + cb(socket as T, beforeCookies); } } } catch { @@ -689,13 +690,13 @@ export class ZilaServer { if (this.serverEvents.onClientRawMessageBeforeCallback) { for (const cb of this.serverEvents.onClientRawMessageBeforeCallback) { - cb(socket, msg); + cb(socket as T, msg); } } if (this.serverEvents.onClientMessageBeforeCallback) { for (const cb of this.serverEvents.onClientMessageBeforeCallback) { - cb(socket, msgObj.identifier, msgObj.message); + cb(socket as T, msgObj.identifier, msgObj.message); } }