Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/classes/CloudConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import { WebSocket } from "ws";
import { Session } from "../Consts";
import events from "events";

class CloudConnection {
class CloudConnection extends events.EventEmitter {
super();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super in constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webdev03 Could you change it?

creator: string;
id: number;
session: Session;
Expand Down Expand Up @@ -35,10 +37,13 @@ class CloudConnection {
}
});
this.connection.on("message", (e) => {
this.emit("message-may-be-empty", e);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly for debugging.

if (!e) return;
this.emit("message", e);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For situations when the raw responses need to be handled. (e.g. additional metadata sent by custom servers)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Custom servers don't have support right now if I remember correctly. That's something I would add in maybe 2.3.0 (this will go 2.2.0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webdev03: A server might send data about user joins/disconnects in some cases.

for (const message of e.toString().split("\n")) {
const obj = JSON.parse(message || '{"method": "err"}');
if (obj.method == "set") {
this.emit("set", {name: obj.name, value: obj.value});
this.variables[obj.name] = obj.value;
}
}
Expand All @@ -49,19 +54,25 @@ class CloudConnection {
user: this.session.sessionJSON.user.username,
project_id: this.id.toString()
});
this.emit("connect", null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So command-line applications can show UI for the connection status.

});
this.connection.on("error", (err) => {
this.emit("error", err);
throw err;
});
this.connection.on("close", () => {
if (!this.disconnected) this.connect();
if (!this.disconnected) {
this.emit("reconnect", null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging

this.connect();
}
});
}

/**
* Sends a packet through cloud
*/
private send(data) {
this.emit("internal-send", data);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging

this.connection.send(`${JSON.stringify(data)}\n`);
}

Expand Down Expand Up @@ -100,6 +111,7 @@ class CloudConnection {
* Closes the cloud connection
*/
close() {
this.emit("close", null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's easy to implement UI for connection status.

this.disconnected = true;
this.connection.close();
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts", "dist"],
"compilerOptions": {
"moduleResolution": "node"
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}