Skip to content

Commit

Permalink
[feature] browser events now broadcast to all clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjennings committed Jan 9, 2012
1 parent 8205901 commit 42065f8
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 631 deletions.
21 changes: 21 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

a basic [hook.io](http://hook.io) enabled webserver with a [socket.io](http://socket.io) based browser bridge

# EXPERIMENTAL BROADCAST VERSION

```
git clone git@github.com:ianjennings/webserver.git
cd webserver
./bin/webserver
```
Then visit http://localhost:8000 in two browser tabs. The pings seen are coming from the server and should appear in both browser tabs at the same time.

## Client Timeouts

This solution keeps a reference to browser clients in the webserver hook. Since the client won't have the opportunity to emit a disconnection notice, we will disconnect the user if they haven't contacted the webserver within two minutes (by default). Clients that get disconnected get the event:
```
webserver::timeout
```
Remember, timeout is only relative to the last time a client emited an event. If a client first timesout and then emits an additional event, the client will continue to recieve messages and the tmieout will reset.

## Events

All events that are broadcast by the webserver hook are prepended with ```webserver::```. This makes it easy to distinguish from local events and broadcasted events.

## Features

- Provides a cross-browser bridge to [hook.io](http://hook.io) through [socket.io](http://socket.io)
Expand Down
1 change: 1 addition & 0 deletions bin/browserify
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#! /usr/bin/env node
//
// A simple CLI tool for helping to browserify /browser/hook-shim.j
//
Expand Down
18 changes: 2 additions & 16 deletions bin/webserver
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,5 @@ var webserver = new Webserver({
webserver.start();

webserver.on('ping', function (data, cb) {
var place = function () {

var item = places.shift();
places.push(item);

return item;
}

cb(null, place());
})

var places = [
"world",
"computer",
"hook.io"
];
cb(null, null);
})
7 changes: 5 additions & 2 deletions lib/browser/hook-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ Hook.prototype.connect = function () {

//
// Create a dnode / socketio client which exposes,
// a `message` function
// a `message` function as well as assigns a
// hopeful unique id for this client
//
date = new Date();
var client = dnode({
message: function(event, data){
self.emit(event, data, function(){}, false);
},
report: function(message) {
}
},
ident: String(date.getTime()) + String(Math.floor(Math.random() * 99999))
});

//
Expand Down
39 changes: 35 additions & 4 deletions lib/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ var Webserver = exports.Webserver = function(options, callback){

Hook.call(self, options);

self.host = self.host || '127.0.0.1';
self.webroot = self.webroot || '.'
self.host = self.host || '127.0.0.1';
self.webroot = self.webroot || '.'
self.clientTimeout = self.clientTimeout || 60000;

self.on('hook::started', function(){
self.findPort({ port: self.port }, function(err, port){
Expand All @@ -26,9 +27,39 @@ var Webserver = exports.Webserver = function(options, callback){
})
});

self.on("**", function(data){
//
// A list of currently connected clients
//
var clients = {};

//
// Broadcast every message to all clients and
// Check how long it's been since we've heard from each client
// If it's been more than clientTimeout, remove the client and send it a timeout message
//
self.on("**", function(data, callback){

if(self.client && self.client.message){
self.client.message(this.event, data, callback);

if(this.event == "hook::ready"){
clients[self.client.ident] = self.client;
}

for(c in clients){

This comment has been minimized.

Copy link
@Marak

Marak Jan 26, 2012

Contributor

This doesn't seem right. Most of this logic should already be defined in socket.io

// if we find the same client that sent the message update their lastHeard time and callback
if(self.client.ident == clients[c].ident){
clients[c].lastHeard = new Date().getTime();
self.client.message("webserver::" + this.event, data, callback);
} else {
if((new Date().getTime() - clients[c].lastHeard) > self.clientTimeout){
clients[c].message("webserver::timeout", {lastHeard: clients[c].lastHeard, timeout: self.clientTimeout});
delete clients[c];
} else {
clients[c].message("webserver::" + this.event, data);
}
}
}

}
});

Expand Down
Loading

0 comments on commit 42065f8

Please sign in to comment.