Permalink
Browse files

[feature] Add ability to trigger a client-side reconnection from the …

…server #150
  • Loading branch information...
1 parent 006698f commit b03f2d8d757ef7825ecbb67b2411fe584857f334 @lpinca lpinca committed Feb 14, 2014
Showing with 15 additions and 19 deletions.
  1. +3 −13 primus.js
  2. +11 −3 spark.js
  3. +1 −3 transformers/socket.io/client.js
View
@@ -644,7 +644,7 @@ Primus.prototype.initialise = function initialise(options) {
});
});
- primus.on('incoming::end', function end(intentional) {
+ primus.on('incoming::end', function end() {
var readyState = primus.readyState;
//
@@ -668,18 +668,8 @@ Primus.prototype.initialise = function initialise(options) {
}
//
- // Some transformers emit garbage when they close the connection. Like the
- // reason why it closed etc. we should explicitly check if WE send an
- // intentional message.
- //
- if ('primus::server::close' === intentional) {
- return primus.emit('end');
- }
-
- //
- // Always, call the `close` event as an indication of connection disruption.
- // This also emitted by `primus#end` so for all cases above, it's still
- // emitted.
+ // Fire the `close` event as an indication of connection disruption.
+ // This is also fired by `primus#end` so it is emitted in all cases.
//
primus.emit('close');
View
@@ -396,20 +396,28 @@ Spark.readable('_write', function _write(data) {
/**
* End the connection.
*
+ * Options:
+ * - reconnect (boolean) Trigger client-side reconnect.
+ *
* @param {Mixed} data Optional closing data.
+ * @param {Object} options End instructions.
* @api public
*/
-Spark.readable('end', function end(data) {
+Spark.readable('end', function end(data, options) {
if (Spark.CLOSED === this.readyState) return this;
+ options = options || {};
var spark = this;
if (data) spark.write(data);
//
- // Bypass the .write method as this message should not be transformed.
+ // If we want to trigger a reconnect do not send
+ // `primus::server::close`, otherwise bypass the .write method
+ // as this message should not be transformed.
//
- spark._write('primus::server::close');
+ if (!options.reconnect) spark._write('primus::server::close');
+
spark.readyState = Spark.CLOSED;
process.nextTick(function tick() {
@@ -62,9 +62,7 @@ module.exports = function client() {
socket.on('connect_failed', primus.emits('error'));
socket.on('error', primus.emits('error'));
socket.on('message', primus.emits('data'));
- socket.on('disconnect', primus.emits('end', function parser(kind) {
- return kind === 'booted' ? 'primus::server::close' : false;
- }));
+ socket.on('disconnect', primus.emits('end'));
});
//

0 comments on commit b03f2d8

Please sign in to comment.