Skip to content

Commit

Permalink
exported users/channels outside from plugin - example updated
Browse files Browse the repository at this point in the history
  • Loading branch information
mainiak committed Oct 3, 2013
1 parent 68709f2 commit 6bc62f6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* http://www.html5rocks.com/en/tutorials/eventsource/basics/
* https://gist.github.com/alaingilbert/1466875
* http://spumko.github.io/resource/api/#plugin-interface
* https://github.com/spumko/hapi/issues/1008
31 changes: 26 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Hapi, hostname, permissions, options, port, server, staticRoute;
var Hapi, hostname, permissions, options, port, server, staticRoute, handler;

Hapi = require('hapi');

Expand All @@ -12,14 +12,15 @@ permissions = {};
options = {
sse: '/sse-events/'
};

server.pack.allow(permissions).require(__dirname + '/..', options, function(err) {
if (err) {
console.err("Failed to load plugin.");
throw err;
}
});

staticRoute = {
server.route({
method: 'GET',
path: '/{path*}',
handler: {
Expand All @@ -29,10 +30,30 @@ staticRoute = {
index: true
}
}
};

server.route([staticRoute]);
});

server.start(function() {
return console.log("# Started at " + server.info.uri);
});

var handler = {
num: 0,
hapiSSE: server.plugins['hapi-sse']['channels']
};

function hailUsers() {
var users = this.hapiSSE._users;
var count = 0;
for (user in users) {
//console.log(user); // XXX
//console.log(users[user]); // XXX
//users[user].write('data: ' + this.num + "\n\n"); // XXX
this.hapiSSE.sendMsgTo(user, this.num);
count++;
}
console.log('i: ' + this.num + ', users: ' + count); // XXX
this.num++;
}

// every 10 secs
setInterval(hailUsers.bind(handler), 10000);
30 changes: 7 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,34 @@ internals.defaults = {
sse: '/sse'
};

hapiSSE = new HapiSSE();

// SSE
exports.register = function (plugin, options, next) {

var hapiSSE = null;
var settings = Hoek.applyToDefaults(internals.defaults, options || {});

if (settings.sse) {

hapiSSE = new HapiSSE();
plugin.api('channels', hapiSSE)

plugin.route({
method: 'GET',
path: settings.sse,
handler: function (request) {
var channel = new PassThrough();
channel.write(''); // to start reading at any time

var remoteIP = request.raw.req.socket.remoteAddress;
//console.log(util.inspect(remoteIP)); // XXX
var channel = new PassThrough();

channel.write(''); // to start reading at any time
hapiSSE._addUser(remoteIP, channel);

// detect remote side has closed connection
request.raw.req.on('close', function() {
console.log(remote, arguments); // XXX
hapiSSE._delUser(remoteIP);
});

hapiSSE.sendMsgTo(remoteIP, 'Welcome!'); // XXX

request.reply(channel).type('text/event-stream').header('Cache-Control','no-cache').header('Connection','keep-alive');

// XXX
setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 1);
}, 5000); // 5 secs

setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 2);
}, 10000); // 10 secs

setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 3, true);
hapiSSE._delUser(remoteIP);
}, 20000); // 20 secs
// XXX
},
config: {
description: "ServerSideEvent plugin for Hapi.js"
Expand Down

0 comments on commit 6bc62f6

Please sign in to comment.