Skip to content

Commit 23e5bb6

Browse files
committed
merge
2 parents 9cac2d4 + a0fab19 commit 23e5bb6

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

client.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ var WriteOperation = function (ws, key, value) {
1313
'success': [],
1414
'fail': []
1515
};
16-
17-
ws.on('confirm', function (d) {
16+
var cb = function (d) {
1817
if (d.id !== id) {
1918
return;
2019
}
2120

2221
this.callbacks['success'].forEach(function (cb) {
2322
cb();
2423
});
25-
}.bind(this));
24+
25+
ws.removeListener(cb);
26+
27+
}.bind(this);
28+
29+
ws.on('confirm', cb);
2630
};
2731

2832
WriteOperation.prototype.commit = function () {
@@ -97,7 +101,7 @@ KeePee.prototype.read = function (key) {
97101
};
98102

99103

100-
var kp = new KeePee();
104+
var kp = new KeePee(ws);
101105

102106
var writeOperation = kp.write('Foo', 'Bar');
103107

lib/index.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var internals = {};
1212
internals.defaults = {
1313
endpoint: '/',
1414
host: 'localhost',
15-
port: 0
15+
port: 0,
16+
data: {}
1617
};
1718

1819

1920
exports.register = function (plugin, options, next) {
2021

2122
var settings = Hoek.applyToDefaults(internals.defaults, options || {});
22-
2323
var server = new plugin.hapi.Server(settings.host, settings.port);
2424
server.start(function () {
2525

@@ -32,28 +32,31 @@ exports.register = function (plugin, options, next) {
3232
handler: internals.handler(template)
3333
});
3434

35+
plugin.route({
36+
method: 'GET',
37+
path: '/client.js',
38+
handler: {
39+
file: __dirname + '/../client.js'
40+
}
41+
});
42+
43+
plugin.route({
44+
method: 'GET',
45+
path: '/socket.io.min.js',
46+
handler: {
47+
file: __dirname + '/../socket.io.min.js'
48+
}
49+
});
50+
3551
var ws = new Ws.Server({ server: server.listener });
3652
ws.on('connection', function (socket) {
3753

3854
subscribers.push(socket);
3955
socket.send('Welcome');
56+
socket.on('message', internals.message(socket));
4057
});
4158

42-
var oldStdout = process.stdout.write.bind(process.stdout);
43-
process.stdout.write = function (chunk, encoding) {
44-
45-
oldStdout(chunk, encoding);
46-
transmit(chunk);
47-
};
48-
49-
var oldStderr = process.stderr.write.bind(process.stderr);
50-
process.stderr.write = function (chunk, encoding) {
51-
52-
oldStderr(chunk, encoding);
53-
transmit(chunk);
54-
};
55-
56-
var transmit = function (data) {
59+
internals.transmit = function (data) {
5760

5861
for (var i = 0, il = subscribers.length; i < il; ++i) {
5962
try {
@@ -70,6 +73,15 @@ exports.register = function (plugin, options, next) {
7073
};
7174

7275

76+
internals.message = function (socket) {
77+
78+
return function (data) {
79+
80+
internals.transmit(data);
81+
};
82+
};
83+
84+
7385
internals.handler = function (template) {
7486

7587
return function (request, reply) {
@@ -84,6 +96,8 @@ internals.template = function (host, port) {
8496
return '<!DOCTYPE html><html lang="en"><head><title>Debug Terminal</title>' +
8597
'<meta http-equiv="Content-Language" content="en-us">' +
8698
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
99+
'<script type="application/json" src="/socket.io.min.js"></script>' +
100+
'<script type="application/json" src="/client.js"></script>' +
87101
'</head><body>' +
88102
'<script language="javascript">' +
89103
'var ws = new WebSocket("ws://' + host + ':' + port + '");' +

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"homepage": "https://github.com/nodekc/keepee",
2222
"dependencies": {
23+
"socket.io": "0.9.x",
2324
"ws": "0.4.x",
2425
"hoek": "1.x.x",
2526
"hapi": "4.x.x"

socket.io.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)