-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
51 lines (40 loc) · 1.73 KB
/
map.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
// set up ======================================================================
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
console.log('map running on ' + 3000);
var WebSocket = require('ws');
var geoip = require('geoip-lite');
// configuration ===============================================================
app.configure(function() {
app.set('view engine', 'ejs'); // set up ejs for templating
app.use(express.static(__dirname + '/public')); //to server static files
app.set('views', __dirname + '/views');
});
// blockchain websocket api ====================================================
// when a client is connected via web socket, start pinging the blockchain api
io.sockets.on('connection', function (socket) {
var ws = new WebSocket('ws://ws.blockchain.info:8335/inv');
ws.on('open', function() {
ws.send('{"op":"unconfirmed_sub"}'); // subscribe to all new btc txs from blockchain.info
});
ws.on('message', function(message) {
var msg = JSON.parse(message);
if(msg.x["relayed_by"] !== '127.0.0.1'){
console.log(msg);
// console.log(msg.x.out[0].value + " BTC sent from IP address " + msg.x["relayed_by"]);
var geo = geoip.lookup(msg.x["relayed_by"]);
if (geo) {
console.log(geo.city+", "+geo.country+" ["+geo.ll+"] ");
socket.emit('tx', { longitude: geo.ll[0], latitude: geo.ll[1], btc: msg.x.out[0].value/100000000, usd: (msg.x.out[0].value/100000000)*450.91, country: geo.country});
}
}
});
});
// main route =================================================================
app.get('/', function(req, res) {
res.render('map.ejs', {});
});