-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.js
executable file
·106 lines (88 loc) · 3.31 KB
/
frontend.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
$(function () {
"use strict";
// for better performance - to avoid searching in DOM
var contentLayer = $('#content');
var orderLayer = $('#order');
var statusLayer = $('#status');
var currentCurrency = 'USD';
var fee = 0.0015;
var totalTrading = 0.0;
var bestBid = 0.0;
var bestAccumulatedBid = 0.0;
var orderBooks = {};
const dataTypes = {
CURRENCY: 0,
ORDERPATH: 1
}
// if user is running mozilla then use it's built-in WebSocket
window.WebSocket = window.WebSocket || window.MozWebSocket;
// if browser doesn't support WebSocket, just show some notification and exit
if (!window.WebSocket) {
addMessage(statusLayer, 'Sorry, but your browser doesn\'t '
+ 'support WebSockets.', 'red');
return;
}
// open connection
var connection = new WebSocket('ws://127.0.0.1:1337');
connection.onopen = function () {
addMessage(statusLayer, 'WebSocket connection opened', 'green');
};
connection.onerror = function (error) {
// just in there were some problems with conenction...
addMessage(statusLayer, 'Sorry, but there\'s some problem with your '
+ 'connection or the server is down.', 'red');
};
// most important part - incoming messages
connection.onmessage = function (message) {
let data = JSON.parse(message.data);
switch (data.type) {
case dataTypes.CURRENCY:
manageCurrencies(data.payload);
break;
case dataTypes.ORDERPATH:
console.log(data);
manageOrderPath(data.payload);
break;
}
};
/**
* This method is optional. If the server wasn't able to respond to the
* in 3 seconds then show some error message to notify the user that
* something is wrong.
*/
setInterval(function() {
if (connection.readyState !== 1) {
addMessage(statusLayer, 'ERROR: Unable to communicate with the websocket server', 'red', new Date());
}
}, 3000);
function manageCurrencies(currencies) {
let output = '';
for (let key in currencies) {
for (let key2 in currencies[key]) {
output += '<span class="">'+key+'/'
+key2+':</span><span class="">'
+currencies[key][key2].price+'</span><br>';
}
}
addMessage(contentLayer, output, 'black');
}
function manageOrderPath(pathInfo) {
let profitability = parseFloat(Math.round(pathInfo.profitability * 10000) / 100).toFixed(2);
let output = '<span class="">' + pathInfo.path.join(' -> ') + ': ' + profitability + '</span>';
addMessage(orderLayer, output, 'black');
}
function manageOrderbook(orderBook) {
orderBooks[orderBook.pair] = 'Bid: ' + orderBook.bid + ' Ask: ' + orderBook.ask;
let output = '';
for (let key in orderBooks) {
output += '<span style="blue">' + key + ':</span> ' + orderBooks[key] + '<br>';
}
addMessage(contentLayer, output, 'black');
}
/**
* Add message to document
*/
function addMessage(layer, message, color) {
layer.html('<p style="color:' + color + '">' + message + '</p>');
}
});