Skip to content

Commit

Permalink
Merge pull request #15 from hapijs/connect-event
Browse files Browse the repository at this point in the history
Add onConnect callback option
  • Loading branch information
Eran Hammer committed Sep 5, 2015
2 parents 26a012f + 264a403 commit 02b9d69
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var internals = {


internals.schema = Joi.object({
onConnect: Joi.func(), // function (ws) {}
onUnknownMessage: Joi.func(), // function (message, ws) { ws.send('string'); }
auth: Joi.object({
endpoint: Joi.string().required(),
Expand Down
4 changes: 3 additions & 1 deletion lib/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ internals.Listener.prototype._add = function (ws) {

socket.authenticate(function () {


if (self._settings.onConnect) {
self._settings.onConnect(ws);
}
});
};

Expand Down
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,38 @@ describe('register()', function () {
});
});
});

it('calls onConnect callback', function (done) {

var client;

var onConnect = function (ws) {

expect(ws).to.exist();
client.disconnect();
server.stop(done);
};

var server = new Hapi.Server();
server.connection();
server.register({ register: Nes, options: { onConnect: onConnect } }, function (err) {

expect(err).to.not.exist();

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {

return reply('hello');
}
});

server.start(function (err) {

client = new Nes.Client('http://localhost:' + server.info.port);
client.connect(function () { });
});
});
});
});

0 comments on commit 02b9d69

Please sign in to comment.