Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 12 additions & 2 deletions src/classes/CloudConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

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

class CloudConnection {
class CloudConnection extends events.EventEmitter {
creator: string;
id: number;
session: Session;
Expand All @@ -20,6 +21,7 @@ class CloudConnection {
session: Session;
server?: string;
}) {
super();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does it build now?

this.id = id;
this.session = session;
this.server = server;
Expand All @@ -39,6 +41,7 @@ class CloudConnection {
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 +52,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 +109,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
}
}