Permalink
Browse files

minimal browser version

  • Loading branch information...
1 parent 4d9aecd commit 89399dc1c613bae095fa65367fc54c1e7338908f @substack committed Jun 20, 2012
Showing with 6 additions and 189 deletions.
  1. +5 −0 browser.js
  2. +0 −69 browser/index.js
  3. +0 −83 example/saturate/index.html
  4. +0 −36 example/saturate/saturate.js
  5. +1 −1 package.json
View
@@ -0,0 +1,5 @@
+var dnode = require('./lib/dnode');
+
+module.exports = function (cons, opts) {
+ return new dnode(cons, opts);
+};
View
@@ -1,69 +0,0 @@
-var protocol = require('dnode-protocol');
-var EventEmitter = require('events').EventEmitter;
-var io = require('socket.io-client');
-var json = typeof JSON === 'object' ? JSON : require('jsonify');
-
-var exports = module.exports = dnode;
-
-function dnode (wrapper) {
- if (!(this instanceof dnode)) return new dnode(wrapper);
- this.proto = protocol(wrapper);
- this.stack = [];
- return this;
-}
-
-dnode.prototype = new EventEmitter;
-
-dnode.prototype.use = function (middleware) {
- this.stack.push(middleware);
- return this;
-};
-
-dnode.prototype.connect = function () {
- var self = this;
- var params = protocol.parseArgs(arguments);
- var client = self.proto.create();
-
- var proto = (params.proto || window.location.protocol)
- .replace(/:.*/, '') + '://';
-
- var sock = client.socketio = io.connect(
- proto + (params.host || window.location.host),
- params
- );
-
- client.end = function () {
- sock.disconnect();
- };
-
- sock.on('connect', function () {
- client.start();
- self.emit('connect');
- });
-
- sock.on('disconnect', function () {
- client.emit('end');
- self.emit('end');
- });
-
- sock.on('message', client.parse);
-
- client.on('request', function (req) {
- sock.send(json.stringify(req) + '\n');
- });
-
- if (params.block) {
- client.on('remote', function () {
- params.block.call(client.instance, client.remote, client);
- });
- }
-
- this.stack.forEach(function (middleware) {
- middleware.call(client.instance, client.remote, client);
- });
-};
-
-exports.connect = function () {
- var d = exports();
- return d.connect.apply(d, arguments);
-};
@@ -1,83 +0,0 @@
-<html>
-<head>
-<script type="text/javascript" src="/dnode.js"></script>
-<script type="text/javascript">
-
- window.onload = function () {
- var stats = document.getElementById("stats");
- var unit = document.getElementById("unit");
- var ping = document.getElementById("ping");
-
- var connections = document.getElementById("connections");
- connections.innerHTML = (Number(connections.innerHTML) + 1).toString();
-
- var bytes = 0;
-
- var em = new EventEmitter;
- em.on('data', function (buf) {
- bytes += buf.length;
- if (bytes > 1e6) u = 1e6;
- else if (bytes > 1e3) u = 1e3;
- else u = 1;
-
- var x = bytes / u;
- var d = (x - Math.floor(x)).toString().replace(/^0\./, '');
- stats.innerHTML = Math.floor(x) + '.'
- + d.slice(0,2) + '00'.slice(d.length);
- unit.innerHTML = { 1e6 : 'M', 1e3 : 'K', 1 : 'B' }[u];
- });
-
- DNode({
- emit : function () { em.emit.apply(em, arguments) }
- }).connect({ ping : 5000, timeout : 2000 }, function (remote, conn) {
- conn.on('ping', function (elapsed) {
- ping.innerHTML = elapsed;
- });
-
- function reconnect () {
- conn.reconnect(1000, function (err) {
- if (err) {
- if (window.console) {
- console.error(err);
- }
- else {
- throw err;
- }
- reconnect();
- }
- else {
- ping.style.color = 'black';
- stats.style.color = 'black';
- }
- });
- }
-
- conn.on('timeout', function () {
- ping.style.color = 'red';
- reconnect();
- });
-
- conn.on('end', function () {
- stats.style.color = 'rgb(200,200,200)';
- reconnect();
- });
- });
- };
-</script>
-</head>
-<body>
- <div>
- <span id="stats">0</span>
- <span id="unit">bytes</span>
- transferred
- </div>
-
- <div>
- ping <span id="ping">X</span> milliseconds
- </div>
-
- <div>
- <span id="connections">0</span> connections
- </div>
-</body>
-</html>
@@ -1,36 +0,0 @@
-#!/usr/bin/env node
-
-var connect = require('connect');
-var server = connect.createServer(
- connect.static(__dirname)
-);
-
-var clients = {};
-
-var Hash = require('traverse/hash');
-function publish () {
- var args = [].slice.call(arguments);
- Hash(clients).forEach(
- function (emit) { emit.apply({}, args) }
- );
-}
-
-var DNode = require('dnode');
-DNode(function (client, conn) {
- conn.on('ready', function () {
- clients[conn.id] = client.emit;
- });
-
- conn.on('end', function () {
- delete clients[conn.id];
- });
-}).listen(server);
-
-console.log('http://localhost:6061/');
-server.listen(6061);
-
-setInterval(function () {
- var n = Math.floor(Math.random() * 3e4);
- var buf = new Buffer(n);
- publish('data', buf.toString());
-}, 50);
View
@@ -27,7 +27,7 @@
"scripts" : {
"test" : "tap test/*.js"
},
- "browserify" : "browser/index.js",
+ "browserify" : "browser.js",
"engine" : {
"node" : ">=0.6.0"
},

0 comments on commit 89399dc

Please sign in to comment.