-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlazyClientServer.js
64 lines (55 loc) · 1.67 KB
/
lazyClientServer.js
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
'use strict'
var express = require('express');
var app = express();
var serv = require('http').Server(app);
var path = require('path');
app.use(express.static(path.join(__dirname,'public')));
var redis = require('redis');
var client = redis.createClient();
var fs = require('fs');
app.use(express.static(path.join(__dirname,'public')));
var io = require('socket.io')(serv,{});
client.on("error", function (err) {
console.log("Error " + err);
});
client.on("ready", function (msg) {
console.log('redis client is ready');
});
serv.listen(5000, function() {
console.log("listening on port 5000");
});
function getBlocks(socket) {
client.keys('chain.block.*', function (err, keys) {
console.log('printing keys');
console.log(keys);
var arr = [];
if (err) return console.log(err);
var counter = 0;
for(var i = 0, len = keys.length; i < len; i++) {
var block = client.get(keys[i], function(err, reply) {
if(err) {
console.log('error occured');
console.log(err);
}
block = JSON.parse(reply);
console.log(block);
arr.push(block);
counter += 1;
if(counter === keys.length) {
console.log('emitting event');
socket.emit('data', {'data': arr});
}
});
}
});
}
io.on('connection', function(socket) {
console.log('A client connected');
setInterval(function() {
getBlocks(socket);
}, 3000);
});
io.on('error', function(err) {
console.log('error occured');
console.log(err);
});