Skip to content

Commit

Permalink
Add missing type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
davawen committed Feb 17, 2021
1 parent 979b366 commit a8a94c4
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions TypescriptServer/src/networking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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 =>
Expand All @@ -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);
}
}
};

0 comments on commit a8a94c4

Please sign in to comment.