Skip to content

Commit

Permalink
[minor] Add ability to reconnect the clients when calling `primus.des…
Browse files Browse the repository at this point in the history
…troy`

Closes #318
  • Loading branch information
lpinca committed Jan 20, 2015
1 parent 7b52464 commit 2f0a49b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -1039,11 +1039,11 @@ of the active `Spark` ids on the server.

In rare cases you might need to destroy the Primus instance you've created. You
can use the `primus.destroy()` or `primus.end()` method for this. This method
accepts an Object which allows you to configure how you want the connections to
be destroyed:
accepts an Object which allows you to configure the destruction process:

- `close` Close the HTTP server that Primus received. Defaults to `true`.
- `timeout` Clean up the server and optionally, it's active connections after
- `reconnect` Automatically reconnect the clients. Defaults to `false`.
- `timeout` Close all active connections and clean up the Primus instance after
the specified amount of timeout. Defaults to `0`.

The timeout is especially useful if you want gracefully shutdown your server but
Expand Down
8 changes: 5 additions & 3 deletions index.js
Expand Up @@ -888,8 +888,9 @@ Primus.readable('indexOfLayer', function indexOfLayer(name) {
* Destroy the created Primus instance.
*
* Options:
* - close (boolean) Close the given server.
* - timeout (number) Forcefully close all connections after a given x MS.
* - close (boolean) Close the given server.
* - reconnect (boolean) Trigger a client-side reconnect.
* - timeout (number) Close all active connections after x milliseconds.
*
* @param {Object} options Destruction instructions.
* @param {Function} fn Callback.
Expand All @@ -903,6 +904,7 @@ Primus.readable('destroy', function destroy(options, fn) {
}

options = options || {};
if (options.reconnect) options.close = true;

var primus = this;

Expand All @@ -918,7 +920,7 @@ Primus.readable('destroy', function destroy(options, fn) {
// Close the connections that are left open.
//
primus.forEach(function shutdown(spark) {
spark.end();
spark.end(undefined, { reconnect: options.reconnect });
});

if (options.close !== false) {
Expand Down
13 changes: 13 additions & 0 deletions test/primus.test.js
Expand Up @@ -753,5 +753,18 @@ describe('Primus', function () {

server.listen(common.port);
});

it('can trigger a client-side reconnect', function (done) {
var socket = new primus.Socket('http://localhost:'+ server.portnumber);

socket.on('reconnect scheduled', function () {
socket.end();
done();
});

socket.on('open', function () {
primus.destroy({ reconnect: true });
});
});
});
});

0 comments on commit 2f0a49b

Please sign in to comment.