-
Notifications
You must be signed in to change notification settings - Fork 3
Make CloudConnection an EventEmitter #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
d990631
4c2786b
ab44f35
e510d5a
32d0c1f
adeaac9
30db13f
7a909e4
b368930
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ | |
|
|
||
| import { WebSocket } from "ws"; | ||
| import { Session } from "../Consts"; | ||
| import events from "events"; | ||
|
|
||
| class CloudConnection { | ||
| class CloudConnection extends events.EventEmitter { | ||
| super(); | ||
| creator: string; | ||
| id: number; | ||
| session: Session; | ||
|
|
@@ -35,10 +37,13 @@ class CloudConnection { | |
| } | ||
| }); | ||
| this.connection.on("message", (e) => { | ||
| this.emit("message-may-be-empty", e); | ||
|
||
| if (!e) return; | ||
| this.emit("message", e); | ||
|
||
| 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; | ||
| } | ||
| } | ||
|
|
@@ -49,19 +54,25 @@ class CloudConnection { | |
| user: this.session.sessionJSON.user.username, | ||
| project_id: this.id.toString() | ||
| }); | ||
| this.emit("connect", null); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For debugging |
||
| this.connection.send(`${JSON.stringify(data)}\n`); | ||
| } | ||
|
|
||
|
|
@@ -100,6 +111,7 @@ class CloudConnection { | |
| * Closes the cloud connection | ||
| */ | ||
| close() { | ||
| this.emit("close", null); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super in constructor
There was a problem hiding this comment.
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?