-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slight organization conserns, moving a couple curly braces to the same line to improve readability, etc.
- Loading branch information
1 parent
a50ee07
commit 02bc6e6
Showing
4 changed files
with
96 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Please edit this file | ||
import { Socket } from 'net'; | ||
import { Data, packet } from './networking' | ||
import { clients } from './globals' | ||
|
||
export function handlePacket(c: Client, data: Data):void { | ||
var cmd = data.cmd.toLowerCase(); | ||
|
||
switch(cmd) { | ||
case 'hello': | ||
console.log("Hello from client: " + data.kappa); | ||
c.sendHello(); | ||
break; | ||
case 'message': | ||
console.log('Message from client: ' + data.msg); | ||
c.sendMessage(data.msg + ' indeed'); | ||
break; | ||
// Add your commands here: | ||
// case 'yourcommand': | ||
// // some code | ||
// break; | ||
} | ||
} | ||
|
||
|
||
export class Client { | ||
socket: Socket; | ||
username: string; // todo: implement actual usernames | ||
|
||
constructor(socket: Socket) { | ||
this.socket = socket; | ||
|
||
this.username = undefined; | ||
this.socket = socket; | ||
// you can add more variables here | ||
} | ||
|
||
// these 2 functions are internal, | ||
write(data: Data):void { | ||
this.socket.write(packet.build(data)); | ||
} | ||
|
||
broadcastAll(data: Data):void { | ||
clients.forEach(c => { | ||
c.write(data); | ||
}); | ||
} | ||
// you might want to implement a broadcastLobby() if you have those | ||
|
||
|
||
// Custom Wrappers | ||
sendHello():void { | ||
this.write({ cmd: 'hello', str: 'Hello, client' }); | ||
} | ||
|
||
sendMessage(msg: string):void { | ||
this.write({ cmd: 'message', msg: msg }); | ||
} | ||
// feel free to add more below | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// here you can put any global variables to later import | ||
// (for access across files) | ||
import { Client } from './custom_stuff' | ||
|
||
export var clients:Client[] = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters