Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created April 17, 2014 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3rd-Eden/10980106 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/10980106 to your computer and use it in GitHub Desktop.
diff --git a/primus.js b/primus.js
index e63e6ff..83ee3e2 100644
--- a/primus.js
+++ b/primus.js
@@ -277,6 +277,7 @@ function Primus(url, options) {
primus.attempt = null; // Current back off attempt.
primus.socket = null; // Reference to the internal connection.
primus.latency = 0; // Latency between messages.
+ primus.disconnect = false; // We didn't receive a disconnect packet.
primus.transport = options.transport; // Transport options.
primus.transformers = { // Message transformers.
outgoing: [],
@@ -674,6 +675,17 @@ Primus.prototype.initialise = function initialise(options) {
var readyState = primus.readyState;
//
+ // This `end` started with the receiving of a primus::server::close packet
+ // which indicated that the user/developer on the server closed the
+ // connection and it was not a result of a network disruption. So we should
+ // kill the connection without doing a reconnect.
+ //
+ if (primus.disconnect) {
+ primus.disconnect = false;
+ return primus.end();
+ }
+
+ //
// Always set the readyState to closed, and if we're still connecting, close
// the connection so we're sure that everything after this if statement block
// is only executed because our readyState is set to `open`.
@@ -798,7 +810,9 @@ Primus.prototype.protocol = function protocol(msg) {
// The server is closing the connection, forcefully disconnect so we don't
// reconnect again.
//
- if ('close' === value) this.end();
+ if ('close' === value) {
+ this.disconnect = true;
+ }
break;
case 'id':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment