This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·72 lines (55 loc) · 1.73 KB
/
index.js
File metadata and controls
executable file
·72 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var protocol = require('koding-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();
console.log(params);
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);
};