Skip to content
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

Rewrite WebSocket internals #1410

Merged
merged 31 commits into from
Apr 28, 2017
Merged

Rewrite WebSocket internals #1410

merged 31 commits into from
Apr 28, 2017

Conversation

amishshah
Copy link
Member

@amishshah amishshah commented Apr 24, 2017

Please describe the changes this PR makes and why it should be merged:
Rewrites the internal WebSocket stuff so it's not as crappy and easier to use for internal sharding in the future

Semantic versioning classification:

  • This PR changes the library's interface (methods or parameters added)
    • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
  • This PR only includes non-code changes, like changes to documentation, README, etc.

Todo:

  • Fancy error messages
  • something else i forgot

@amishshah amishshah changed the title [WIP [WIP] Rewrite WebSocket internals Apr 24, 2017
@@ -13,6 +13,8 @@ class ResumedHandler extends AbstractHandler {
client.emit('debug', `RESUMED ${ws._trace.join(' -> ')} | replayed ${replayed} events. `);
client.emit('resume', replayed);

ws.connection.triggerReady();
Copy link
Member

Choose a reason for hiding this comment

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

NOOOOOOOOOOOOOOOOOOOOOO


if (this.ws.disabledEvents[packet.t] !== undefined) return false;
// #if (this.ws.disabledEvents[packet.t] !== undefined) return false;
Copy link
Member

Choose a reason for hiding this comment

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

no more disabled events?

this._emitReady();
}
switch (this.connection.status) {
case Constants.Status.IDLE:
Copy link
Member

Choose a reason for hiding this comment

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

these can be compounded

case Constants.Status.IDLE:
case Constants.Status.DISCONNECTED:
  this.connection.connect(gateway, 5500);
  return true;

if ([4004, 4010, 4011].includes(event.code)) return;
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
send(packet) {
if (!this.connection) return this.debug('No connection to websocket');
Copy link
Member

Choose a reason for hiding this comment

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

same as above

});
this.sequence = -1;
destroy() {
if (!this.connection) return this.debug('Attempted to destroy WebSocket but no connection exists!');
Copy link
Member

Choose a reason for hiding this comment

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

return this.debug returns this.client.emit which means you return based on if the debug event has listeners

*/
send(data) {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
return this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
Copy link
Member

Choose a reason for hiding this comment

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

AAAAAAAAAAA

get readyState() {
return this.ws.readyState;
setSequence(s) {
this.sequence = s > this.sequence ? s : this.sequence;
Copy link
Member

Choose a reason for hiding this comment

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

this could result in this.sequence = this.sequence which is kinda useless

*/
reconnect() {
this.client.emit(Constants.Events.RECONNECTING);
this.connect(this.gateway, 5500, true);
Copy link
Member

Choose a reason for hiding this comment

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

this 5500 is used a lot, it should probably be moved to a variable somewhere

* @param {Error} error Error that occurred
*/
onError(error) {
this.debug(error);
Copy link
Member

Choose a reason for hiding this comment

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

client error event???

this.emit('close', event);
this.heartbeat(-1);
// Should we reconnect?
if (![1000, 4004, 4010, 4011].includes(event.code)) this.reconnect();
Copy link
Member

Choose a reason for hiding this comment

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

constants with map of code => error message like we talked about earlier

@JMTK
Copy link
Contributor

JMTK commented Apr 27, 2017

Just wanted to comment to say I'm leaving a test script up with multiple shards on for a couple days and I'll report back when it's done. It's currently reporting idle(3) for all shards though.
image
It happens when I run without sharding as well. Currently running on aec0ec3

@amishshah
Copy link
Member Author

@JMTK could you add client.on('debug', console.log); and send me the debug logs?

@JMTK
Copy link
Contributor

JMTK commented Apr 27, 2017

@hydrabolt 1st line is the Authenticated using token line so i just took it out. Everything else is what it stopped at(including my own logs) and then you can see me logging the .status of 3
image

The latency is fairly high but all the heartbeats afterwards were short:
image

@amishshah amishshah merged commit 195fcfa into master Apr 28, 2017
@amishshah amishshah deleted the wsrewrite branch April 29, 2017 07:25
@amishshah amishshah changed the title [WIP] Rewrite WebSocket internals Rewrite WebSocket internals Apr 29, 2017
devsnek pushed a commit to devsnek/discord.js that referenced this pull request May 14, 2017
* Start rewriting Manager and Connection

* more stuff

* stuff

* Fix ready bug

* some stuff i forgot

* fix some stuff

* add stupid heartbeat ack like seriously who cares

* woo!

* fix a bug

* rate limit the dumb websocket

* stuff

* hdocs

* Docs

* Remove ClientManager#setupKeepAlive as it is now redundant

* Change Client._pingTimestamp to a getter that fetches the timestamp from the WebSocketConnection

* are you happy now eslint smh

* make gus happy

* Add CloseEvent external doc

* Make sure to emit 'reconnecting' when actually reconnecting

* ffs

* Fix RESUME logic

* Add heartbeat ack debug messages, including latency data

* Dumb stuff for Gus

* thx eslint

* more dumb stuff

* more dumb crap smh gus i h8 u

* moar messages

* fix for using wrong status, causing certain events not to be fired (discordjs#1422)
devsnek pushed a commit to devsnek/discord.js that referenced this pull request May 14, 2017
* Start rewriting Manager and Connection

* more stuff

* stuff

* Fix ready bug

* some stuff i forgot

* fix some stuff

* add stupid heartbeat ack like seriously who cares

* woo!

* fix a bug

* rate limit the dumb websocket

* stuff

* hdocs

* Docs

* Remove ClientManager#setupKeepAlive as it is now redundant

* Change Client._pingTimestamp to a getter that fetches the timestamp from the WebSocketConnection

* are you happy now eslint smh

* make gus happy

* Add CloseEvent external doc

* Make sure to emit 'reconnecting' when actually reconnecting

* ffs

* Fix RESUME logic

* Add heartbeat ack debug messages, including latency data

* Dumb stuff for Gus

* thx eslint

* more dumb stuff

* more dumb crap smh gus i h8 u

* moar messages

* fix for using wrong status, causing certain events not to be fired (discordjs#1422)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants