-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
102 lines (81 loc) · 2.58 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*'use strict';*/
////
// CONFIGURATION SETTINGS
////
var GET_INTERVAL = 4000;
var PRINT_JSON = true;
////
// START
////
var tickers = "NSE:DABUR,ACC,ABB,MARUTI";//"AAPL,GOOG"; /*,GOOGL,YHOO,TSLA,INTC,AMZN,BIDU,ORCL,MSFT,ORCL,ATVI,NVDA,GME,LNKD,NFLX";*/
var express = require('express');
var http = require('http');
var https = require('https');
var io = require('socket.io');
var cors = require('cors');
function getQuote(socket, tickers) {
https.get({
port: 443,
method: 'GET',
hostname: 'www.google.com',
path: '/finance/info?client=ig&q=' + tickers,
timeout: 1000
}, function(response) {
response.setEncoding('utf8');
var data = '';
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
if(data.length > 0) {
var dataObj;
try {
dataObj = JSON.parse(data.substring(3));
} catch(e) {
return false;
}
var i =0;
var quote = [];
for(i in dataObj){
var key = i;
quote.push( {
ticker : dataObj[i].t,
exchange : dataObj[i].e,
price : dataObj[i].l_cur, // jshint ignore:line
change : dataObj[i].c,
change_percent : dataObj[i].cp, // jshint ignore:line
//last_trade_time : dataObj[i].lt, // jshint ignore:line
//dividend : dataObj[i].div,
//yield : dataObj[i].yld
});}
socket.emit(tickers, PRINT_JSON ? JSON.stringify(quote, null, 4) : JSON.stringify(quote.ticker));
}
});
});
}
function trackTicker(socket, tickers) {
// run the first time immediately
getQuote(socket, tickers);
// every N seconds
var timer = setInterval(function() {
getQuote(socket, tickers);
}, GET_INTERVAL);
socket.on('disconnect', function () {
clearInterval(timer);
});
}
var app = express();
app.use(cors());
var server = http.createServer(app);
var io = io.listen(server);
io.set('origins', '*:*');
app.get('/', function(req, res) {
res.sendfile(__dirname + '/Views/index.html');
});
io.sockets.on('connection', function(socket) {
socket.on('ticker', function(tickers) {
console.log("A person connected");
trackTicker(socket, tickers);
});
});
server.listen(process.env.PORT || 3000);