aboutsummaryrefslogtreecommitdiff
path: root/webAO
diff options
context:
space:
mode:
Diffstat (limited to 'webAO')
-rw-r--r--webAO/client.ts67
1 files changed, 58 insertions, 9 deletions
diff --git a/webAO/client.ts b/webAO/client.ts
index 0cc89cf..e5c5e2b 100644
--- a/webAO/client.ts
+++ b/webAO/client.ts
@@ -38,6 +38,7 @@ export const UPDATE_INTERVAL = 60;
*/
export let oldLoading = false;
export const setOldLoading = (val: boolean) => {
+ console.warn("old loading set to "+val)
oldLoading = val
}
@@ -105,6 +106,8 @@ class Client extends EventEmitter {
_lastTimeICReceived: any;
manifest: string[];
viewport: Viewport;
+ partial_packet: boolean;
+ temp_packet: String;
connect: () => void;
loadResources: () => void
isLowMemory: () => void
@@ -150,6 +153,8 @@ class Client extends EventEmitter {
this.sender = sender
this.viewport = masterViewport();
this._lastTimeICReceived = new Date(0);
+ this.partial_packet = false;
+ this.temp_packet = "";
loadResources
isLowMemory
}
@@ -229,19 +234,63 @@ class Client extends EventEmitter {
const msg = e.data;
console.debug(`S: ${msg}`);
- const packets = Array(msg.split("%"));
+ this.handle_server_packet(msg);
+
+ }
- packets.forEach(function(data: String){
- const splitPacket = String(data).split('#')
- const packetHeader = splitPacket[0];
+ /**
+ * Decode the packet
+ * @param {MessageEvent} e
+ */
+ handle_server_packet(p_data: string)
+{
+ let in_data = p_data;
+
+ if (!p_data.endsWith("%")) {
+ this.partial_packet = true;
+ this.temp_packet = this.temp_packet + in_data
+ console.log("Partial packet")
+ return;
+ }
- packetHandler.has(packetHeader)
- ? packetHandler.get(packetHeader)(splitPacket)
- : console.warn(`Invalid packet header ${packetHeader}`);
- });
-
+ else {
+ if (this.partial_packet) {
+ in_data = this.temp_packet + in_data
+ this.temp_packet = "";
+ this.partial_packet = false;
+ }
}
+ const packet_list = in_data.split("%");
+
+ for (var packet of packet_list) {
+ let f_contents;
+ // Packet should *always* end with #
+ if (packet.endsWith("#")) {
+ f_contents = packet.slice(0, -1).split("#");
+ }
+ // But, if it somehow doesn't, we should still be able to handle it
+ else {
+ f_contents = packet.split("#");
+ }
+ // Empty packets are suspicious!
+ if (f_contents.length == 0) {
+ console.warn("WARNING: Empty packet received from server, skipping...");
+ continue;
+ }
+ // Take the first arg as the command
+ const command = f_contents[0];
+ if(command!=="")
+ {
+ // The rest is contents of the packet
+ packetHandler.has(command)
+ ? packetHandler.get(command)(f_contents)
+ : console.warn(`Invalid packet header ${command}`);
+ };
+ }
+ }
+
+
/**
* Triggered when an network error occurs.
* @param {ErrorEvent} e