-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
249 lines (206 loc) · 8.53 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
*
* Financial Server - a node.js example of a simplified financial markets real-time data server
* Typical use case: Allow clients of an financial services firm to see the current bid and ask prices of
* Stocks in their portfolio in real time.
*
* Author : John Greenan
* Date : 30th September 2014
*
* Author : John Greenan
* Date : 16th October 2014
* Note : Replace socket.io with primus.io (wrapper for socket.io)
*
*
* Author : John Greenan
* Date : 16th April 2015
* Note : add in capability for mixed processing - primus(socket.io) and Solace Systems.
*
*/
var fs = require('fs');
var math = require('mathjs');
var primus = require('primus');
var http = require('http');
var express = require('express');
var path = require('path');
var app = express();
var TargetPort = 1337;
var events = require('events');
var assert = require('assert');
//Why use eval here?
//Simply put, we do this to get the code into this index.js.
//There are other ways to do this such as using require.js but this way is clean and simple
//clearly using eval on scripts is potentially dangerous on the client but on the server, under our control
//this is not so bad...
//Get the clientlibrary code into here...
eval(fs.readFileSync(path.join(__dirname, '/Public/Scripts/clientlibrary.js')) + ' ');
//Get the serverlibrary code into here...
eval(fs.readFileSync(path.join(__dirname, '/Public/Scripts/serverlibrary.js')) + ' ');
//Create webserver
var http = http.createServer(app);
//Create primus server instance...
var primus = new primus(http, { transformer: 'socket.io', parser: 'JSON' });
var clientConnectionCount = 0;
var Check = new Boolean(false);
//primus.save(__dirname + '/Public/Scripts/primus.js');
//use express to serve up the scripts needed...
//primus
app.get('/public/Scripts/primus.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/primus.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//then sortable.js
app.get('/public/Scripts/sorttable.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/sorttable.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//then clientlibrary.js
app.get('/public/Scripts/clientlibrary.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/clientlibrary.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//then tickdata
app.get('/public/Pages/TickData.html', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Pages/TickData.html');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//then css...
app.get('/public/css/FinancialServer.css', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/css/FinancialServer.css');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//then angular...
app.get('/public/scripts/Angular.min.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/angular.min.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//Add in the solace Systems files...
app.get('/public/Scripts/solclient-debug.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/solclient-debug.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
app.get('/public/Scripts/solclientjs.js', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Scripts/solclientjs.js');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
app.get('/public/pages/SolaceTickData.html', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Pages/SolaceTickData.html');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//now serve up index.html...
app.get('/', function (req, res) {
var FileToServe = path.join(__dirname, '/Public/Pages/Index.html');
res.sendFile(FileToServe);
console.log('Serving up: ' + FileToServe);
});
//now listen...
http.listen(TargetPort, function () {
console.log('listening on:' + TargetPort);
});
//This is the part to generate false/simulator data for the application...
var Inc = function (startnumber) {
this.count = startnumber;
};
Inc.prototype = new events.EventEmitter;
Inc.prototype.increment = function () {
var self = this;
setInterval(function () {
var StockSwitcher = math.random();
if (StockSwitcher > 0.0) {
RealTimePriceTick.Side = 'B';
}
if (StockSwitcher > 0.5) {
RealTimePriceTick.Side = 'S';
}
var StockSwitcher = math.random();
if (StockSwitcher > 0.0) {
RealTimePriceTick.Stock = 'JKL';
}
if (StockSwitcher > 0.25) {
RealTimePriceTick.Stock = 'GHI';
}
if (StockSwitcher > 0.50) {
RealTimePriceTick.Stock = 'DEF';
}
if (StockSwitcher > 0.75) {
RealTimePriceTick.Stock = 'ABC';
}
var Bid = math.floor((100 * math.random()) + 1)
var Ask = Bid * 1.023;
var LastTrade = (Ask + Bid) / 2;
RealTimePriceTick.Ask = Ask.toFixed(2);
RealTimePriceTick.Bid = Bid.toFixed(2);
RealTimePriceTick.LastTrade = LastTrade.toFixed(2);
RealTimePriceTick.Timestamp = new Date().toJSON();
RealTimePriceTick.Volume = self.count;
self.emit('Tick', RealTimePriceTick);
self.count++;
}, 300);
};
var Pusher = new Inc(1);
//So, we are now generating some nice looking JSON which includes some random numbers,
//just so it does not look
//too much like something from "example 101"...
Pusher.on('Tick', function () {
primus.write(JSON.stringify(RealTimePriceTick))
}).increment();
primus.on('initialised', function () {
console.log('server initialised');
});
primus.on('connection', function (spark) {
++clientConnectionCount;
//console.log('a user connected, count is ' + clientConnectionCount);
//console.log('connection has the following headers', spark.headers);
//console.log('connection was made from', spark.address);
//console.log('connection id', spark.id);
spark.on('data', function message(data) {
// we have received data from one of the connected sparks.
//Yet, we don't know where the spark is located. We would like to know where
//the spark is, so we can ask the StockGraph iframe to show the specifics for this...
//Note that within this section of spark.on, if we call primus.write we write back to the spark
//that called spark.on...
//This way we know that we are talking to one client...
//
// console.log('connection was made from', spark.address);
//console.log('connection id', spark.id);
console.log('data received: ', data);
var MessageFromClient = JSON.parse(data);
switch (MessageFromClient.Verb) {
case VERBCLOSING:
console.log('Switch to Closing');
break;
case VERBOPENING:
console.log('Switch to Opening');
break;
case VERBSHOWCHART:
var ReceivedInstrumentId = MessageFromClient.ObjectId ;
console.log('Switch to ShowChart ' + ReceivedInstrumentId);
MessageBetweenClientAndServer.ClientSessionGuid = '';
MessageBetweenClientAndServer.comalignmentsystemsjsontype = NAMEMESSAGEBETWEENCLIENTANDSERVER;
MessageBetweenClientAndServer.ObjectId = ReceivedInstrumentId;
MessageBetweenClientAndServer.Payload = GetTextOnThisInstrument(ReceivedInstrumentId);
primus.write(JSON.stringify(MessageBetweenClientAndServer));
break;
default:
console.log('Switch to default');
break;
};
//Here we really need to think of a way to create a key/value collection. so we can manage the case of multiple client connections
//each of multiple iframes within a group. This is something to think about...
//This is the heart of the matter of the relationship between the clients and the server...
//The server must understand the state...
});
});
primus.on('disconnection', function () {
--clientConnectionCount;
console.log('a user disconnected, count is ' + clientConnectionCount);
});