having a static class to drive socket comms is dumb

why did i do this
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-09-14 16:00:24 -05:00
parent 4d281dac5a
commit 6026593287
6 changed files with 52 additions and 49 deletions

View file

@ -5,10 +5,6 @@
<link rel="stylesheet" type="text/css" href="style.css" /> <link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<!-- LOGIN AND REGISTER SCRIPT -->
<script type="text/javascript" src="lib/loginstration.js"></script>
<!-- END -->
<script type="text/javascript" src="bin/lib.js"></script> <script type="text/javascript" src="bin/lib.js"></script>
<script type="text/javascript" src="bin/scape.js"></script> <script type="text/javascript" src="bin/scape.js"></script>
@ -25,7 +21,7 @@
</div> </div>
<!-- THUGGINOMICS LOGIN CODE VERY FRAGILE --> <!-- THUGGINOMICS LOGIN CODE VERY FRAGILE
<div id="loginstration-wrap"> <div id="loginstration-wrap">
<h1 id="loginstration-header" class="loginstration-header"> <h1 id="loginstration-header" class="loginstration-header">

View file

@ -27,8 +27,6 @@ class Entrypoint {
CriticalStop.redirect(error); CriticalStop.redirect(error);
} }
); );
Connection.open();
} }
private static ready(): void { private static ready(): void {

View file

@ -69,6 +69,10 @@ class FileCache {
var store = query.objectStore("files"); var store = query.objectStore("files");
store.put({name: fileName, data: data}); store.put({name: fileName, data: data});
} }
public static deleteFile(fileName: string) {
}
} }
class FileMeta { class FileMeta {

View file

@ -1,38 +1,38 @@
class Connection { class Connection {
private static sock: WebSocket = null; private sock: WebSocket = null;
private static _isOpen: boolean = false; private _isOpen: boolean = false;
private static onOpenFunc: () => void = null; private onOpenFunc: () => void = null;
private static onCloseFunc: () => void = null; private onCloseFunc: () => void = null;
public static get isOpen(): boolean { public get isOpen(): boolean {
return Connection._isOpen; return this._isOpen;
} }
public static open(onOpen: () => void = null): void { public open(onOpen: () => void = null): void {
if(Connection._isOpen) if(this._isOpen)
return; return;
// FLAG replace hard coded url with one loaded from a config file // FLAG replace hard coded url with one loaded from a config file
Connection.sock = new WebSocket("ws://localhost:6770"); this.sock = new WebSocket("ws://localhost:6770");
Connection.sock.binaryType = "arraybuffer"; this.sock.binaryType = "arraybuffer";
Connection.onOpenFunc = onOpen; this.onOpenFunc = onOpen;
Connection.sock.onopen = Connection.onOpen; this.sock.onopen = this.onOpen;
Connection.sock.onmessage = Connection.onMessage; this.sock.onmessage = this.onMessage;
Connection.sock.onclose = Connection.onClose; this.sock.onclose = this.onClose;
} }
public static send(msg: Packet) { public send(msg: Packet) {
Connection.sock.send(msg.getBytes()); this.sock.send(msg.getBytes());
} }
private static onOpen(event: any): void { private onOpen(event: any): void {
Connection._isOpen = true; this._isOpen = true;
if(Connection.onOpenFunc) if(this.onOpenFunc)
Connection.onOpenFunc(); this.onOpenFunc();
} }
private static onMessage(event: any): void { private onMessage(event: any): void {
var raw = new Uint8Array(event.data); var raw = new Uint8Array(event.data);
var msg: Packet; var msg: Packet;
try { try {
@ -45,36 +45,36 @@ class Connection {
console.log(msg); console.log(msg);
switch(msg.id) { switch(msg.id) {
case kPacketId.KeyExchange: case kMasterId.KeyExchange:
var response = Key.generateResponsePacket(msg); var response = Key.generateResponsePacket(msg);
if(Key.succeeded) { if(Key.succeeded) {
Cipher.init(Key.privateKey); Cipher.init(Key.privateKey);
Connection.send(response); this.send(response);
} else } else
CriticalStop.redirect("Could not establish an encrypted connection with the server."); CriticalStop.redirect("Could not establish an encrypted connection with the server.");
break; break;
case kPacketId.LoginAttempt: case kMasterId.LoginAttempt:
break; break;
case kPacketId.RegistrationAttempt: case kMasterId.RegistrationAttempt:
break; break;
} }
} }
private static onClose(event: any): void { private onClose(event: any): void {
Connection._isOpen = false; this._isOpen = false;
Cipher.close(); Cipher.close();
if(Connection.onCloseFunc) if(this.onCloseFunc)
Connection.onCloseFunc(); this.onCloseFunc();
} }
public static close(onClose: () => void = null): void { public close(onClose: () => void = null): void {
if(!Connection._isOpen) if(!this._isOpen)
return; return;
Connection.onCloseFunc = onClose; this.onCloseFunc = onClose;
Connection.sock.close(); this.sock.close();
} }
} }

View file

@ -20,7 +20,7 @@ class Key {
var clientKey = generator.modPow(Key.secret, modulus); var clientKey = generator.modPow(Key.secret, modulus);
Key._privateKey = serverKey.modPow(Key.secret, modulus); Key._privateKey = serverKey.modPow(Key.secret, modulus);
return Packet.create(kPacketId.KeyExchange, [clientKey.toString(16)]); return Packet.create(kMasterId.KeyExchange, [clientKey.toString(16)]);
} }
} }

View file

@ -1,14 +1,19 @@
const enum kPacketId { const enum kMasterId {
KeyExchange = 0, KeyExchange = 1,
LoginAttempt, LoginAttempt,
RegistrationAttempt RegistrationAttempt,
ServerListing
}
const enum kSlaveId {
} }
class Packet { class Packet {
private static magicNumber: Uint8Array = new Uint8Array([0xF0, 0x9F, 0xA6, 0x91]); private static magicNumber: Uint8Array = new Uint8Array([0xF0, 0x9F, 0xA6, 0x91]);
private _id: kPacketId; private _id: number;
public get id(): kPacketId { public get id(): number {
return this._id; return this._id;
} }
@ -37,7 +42,7 @@ class Packet {
private constructor() {} private constructor() {}
public static create(id: kPacketId, regions: any[]): Packet { public static create(id: number, regions: any[]): Packet {
var packet = new Packet; var packet = new Packet;
packet._id = id; packet._id = id;
regions.forEach(region => { regions.forEach(region => {