From a8a94c438b93cf4ee06f3a1b094f8a5a78871d2c Mon Sep 17 00:00:00 2001 From: davawen <56738724+davawen@users.noreply.github.com> Date: Wed, 17 Feb 2021 23:36:23 +0100 Subject: [PATCH] Add missing type declaration --- TypescriptServer/src/networking.ts | 63 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/TypescriptServer/src/networking.ts b/TypescriptServer/src/networking.ts index dbfaf66..c9f8edd 100644 --- a/TypescriptServer/src/networking.ts +++ b/TypescriptServer/src/networking.ts @@ -3,7 +3,7 @@ import { encode, decode } from '@msgpack/msgpack' export type Data = { [key: string]: string; }; -export let handlePacket = (c: Client, data: Data) => +export function handlePacket(c: Client, data: Data): void { var cmd = data.cmd.toLowerCase(); @@ -21,36 +21,6 @@ export let handlePacket = (c: Client, data: Data) => } } -export let packet = -{ - build: (data: Data) => - { - var dataBuff = encode(data); - var sizeBuff = Buffer.alloc(2, dataBuff.length); - - var buff = Buffer.concat([sizeBuff, dataBuff], dataBuff.length + 2); - return buff; - }, - - parse: (c: Client, data: any) => - { - var dataSize = data.length; - for(var i = 0; i < dataSize;) - { - var packSize = data.readUInt16LE(i); // unpack the size - i += 2; - - var dataPack = Buffer.alloc(packSize); // unpack the data - data.copy(dataPack, 0, i, i + packSize); - i += packSize; - - - // pass the decoded data to handlePacket() - handlePacket(c, decode(dataPack) as Data); - } - } -}; - export class Client { socket: Socket; @@ -72,7 +42,7 @@ export class Client this.socket.write(packet.build(data)); } - broadcastAll(data: Data, clients: Client[]) + broadcastAll(data: Data, clients: Client[]): void { clients.forEach( c => @@ -96,3 +66,32 @@ export class Client //#endregion } +export let packet = +{ + build: (data: Data): Buffer => + { + var dataBuff = encode(data); + var sizeBuff = Buffer.alloc(2, dataBuff.length); + + var buff = Buffer.concat([sizeBuff, dataBuff], dataBuff.length + 2); + return buff; + }, + + parse: (c: Client, data: any): void => + { + var dataSize = data.length; + for(var i = 0; i < dataSize;) + { + var packSize = data.readUInt16LE(i); // unpack the size + i += 2; + + var dataPack = Buffer.alloc(packSize); // unpack the data + data.copy(dataPack, 0, i, i + packSize); + i += packSize; + + + // pass the decoded data to handlePacket() + handlePacket(c, decode(dataPack) as Data); + } + } +}; \ No newline at end of file