Skip to content
This repository
Browse code

minimal browser version

  • Loading branch information...
commit 89399dc1c613bae095fa65367fc54c1e7338908f 1 parent 4d9aecd
James Halliday authored
5 browser.js
... ... @@ -0,0 +1,5 @@
  1 +var dnode = require('./lib/dnode');
  2 +
  3 +module.exports = function (cons, opts) {
  4 + return new dnode(cons, opts);
  5 +};
69 browser/index.js
... ... @@ -1,69 +0,0 @@
1   -var protocol = require('dnode-protocol');
2   -var EventEmitter = require('events').EventEmitter;
3   -var io = require('socket.io-client');
4   -var json = typeof JSON === 'object' ? JSON : require('jsonify');
5   -
6   -var exports = module.exports = dnode;
7   -
8   -function dnode (wrapper) {
9   - if (!(this instanceof dnode)) return new dnode(wrapper);
10   - this.proto = protocol(wrapper);
11   - this.stack = [];
12   - return this;
13   -}
14   -
15   -dnode.prototype = new EventEmitter;
16   -
17   -dnode.prototype.use = function (middleware) {
18   - this.stack.push(middleware);
19   - return this;
20   -};
21   -
22   -dnode.prototype.connect = function () {
23   - var self = this;
24   - var params = protocol.parseArgs(arguments);
25   - var client = self.proto.create();
26   -
27   - var proto = (params.proto || window.location.protocol)
28   - .replace(/:.*/, '') + '://';
29   -
30   - var sock = client.socketio = io.connect(
31   - proto + (params.host || window.location.host),
32   - params
33   - );
34   -
35   - client.end = function () {
36   - sock.disconnect();
37   - };
38   -
39   - sock.on('connect', function () {
40   - client.start();
41   - self.emit('connect');
42   - });
43   -
44   - sock.on('disconnect', function () {
45   - client.emit('end');
46   - self.emit('end');
47   - });
48   -
49   - sock.on('message', client.parse);
50   -
51   - client.on('request', function (req) {
52   - sock.send(json.stringify(req) + '\n');
53   - });
54   -
55   - if (params.block) {
56   - client.on('remote', function () {
57   - params.block.call(client.instance, client.remote, client);
58   - });
59   - }
60   -
61   - this.stack.forEach(function (middleware) {
62   - middleware.call(client.instance, client.remote, client);
63   - });
64   -};
65   -
66   -exports.connect = function () {
67   - var d = exports();
68   - return d.connect.apply(d, arguments);
69   -};
83 example/saturate/index.html
... ... @@ -1,83 +0,0 @@
1   -<html>
2   -<head>
3   -<script type="text/javascript" src="/dnode.js"></script>
4   -<script type="text/javascript">
5   -
6   - window.onload = function () {
7   - var stats = document.getElementById("stats");
8   - var unit = document.getElementById("unit");
9   - var ping = document.getElementById("ping");
10   -
11   - var connections = document.getElementById("connections");
12   - connections.innerHTML = (Number(connections.innerHTML) + 1).toString();
13   -
14   - var bytes = 0;
15   -
16   - var em = new EventEmitter;
17   - em.on('data', function (buf) {
18   - bytes += buf.length;
19   - if (bytes > 1e6) u = 1e6;
20   - else if (bytes > 1e3) u = 1e3;
21   - else u = 1;
22   -
23   - var x = bytes / u;
24   - var d = (x - Math.floor(x)).toString().replace(/^0\./, '');
25   - stats.innerHTML = Math.floor(x) + '.'
26   - + d.slice(0,2) + '00'.slice(d.length);
27   - unit.innerHTML = { 1e6 : 'M', 1e3 : 'K', 1 : 'B' }[u];
28   - });
29   -
30   - DNode({
31   - emit : function () { em.emit.apply(em, arguments) }
32   - }).connect({ ping : 5000, timeout : 2000 }, function (remote, conn) {
33   - conn.on('ping', function (elapsed) {
34   - ping.innerHTML = elapsed;
35   - });
36   -
37   - function reconnect () {
38   - conn.reconnect(1000, function (err) {
39   - if (err) {
40   - if (window.console) {
41   - console.error(err);
42   - }
43   - else {
44   - throw err;
45   - }
46   - reconnect();
47   - }
48   - else {
49   - ping.style.color = 'black';
50   - stats.style.color = 'black';
51   - }
52   - });
53   - }
54   -
55   - conn.on('timeout', function () {
56   - ping.style.color = 'red';
57   - reconnect();
58   - });
59   -
60   - conn.on('end', function () {
61   - stats.style.color = 'rgb(200,200,200)';
62   - reconnect();
63   - });
64   - });
65   - };
66   -</script>
67   -</head>
68   -<body>
69   - <div>
70   - <span id="stats">0</span>
71   - <span id="unit">bytes</span>
72   - transferred
73   - </div>
74   -
75   - <div>
76   - ping <span id="ping">X</span> milliseconds
77   - </div>
78   -
79   - <div>
80   - <span id="connections">0</span> connections
81   - </div>
82   -</body>
83   -</html>
36 example/saturate/saturate.js
... ... @@ -1,36 +0,0 @@
1   -#!/usr/bin/env node
2   -
3   -var connect = require('connect');
4   -var server = connect.createServer(
5   - connect.static(__dirname)
6   -);
7   -
8   -var clients = {};
9   -
10   -var Hash = require('traverse/hash');
11   -function publish () {
12   - var args = [].slice.call(arguments);
13   - Hash(clients).forEach(
14   - function (emit) { emit.apply({}, args) }
15   - );
16   -}
17   -
18   -var DNode = require('dnode');
19   -DNode(function (client, conn) {
20   - conn.on('ready', function () {
21   - clients[conn.id] = client.emit;
22   - });
23   -
24   - conn.on('end', function () {
25   - delete clients[conn.id];
26   - });
27   -}).listen(server);
28   -
29   -console.log('http://localhost:6061/');
30   -server.listen(6061);
31   -
32   -setInterval(function () {
33   - var n = Math.floor(Math.random() * 3e4);
34   - var buf = new Buffer(n);
35   - publish('data', buf.toString());
36   -}, 50);
2  package.json
@@ -27,7 +27,7 @@
27 27 "scripts" : {
28 28 "test" : "tap test/*.js"
29 29 },
30   - "browserify" : "browser/index.js",
  30 + "browserify" : "browser.js",
31 31 "engine" : {
32 32 "node" : ">=0.6.0"
33 33 },

0 comments on commit 89399dc

Please sign in to comment.
Something went wrong with that request. Please try again.