forked from protovist/ccminer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.htm
130 lines (122 loc) · 2.89 KB
/
websocket.htm
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
<!DOCTYPE HTML>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script type="text/javascript">
var hashrates = [];
function drawChart(ip) {
$('#container').highcharts({
chart: {
type: 'spline'
},
title: {
text: 'ccMiner WebSocket Sample - ' + ip,
x: -20 //center
},
subtitle: {
text: 'By <a href="http://github.com/tpruvot/ccminer">tpruvot@github</a> 2014',
x: -20
},
xAxis: {
type: 'datetime',
},
yAxis: {
title: {
text: 'Hash Rate (KH/s)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}],
min: 0
},
tooltip: {
valueSuffix: ' KH'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [
{
name: 'GPU0',
data: hashrates[0]
},
{
name: 'GPU1',
data: hashrates[1]
}
]
});
}
function getData(ip, port) {
if ("WebSocket" in window) {
var ws = new WebSocket('ws://'+ip+':'+port+'/histo','text');
var rates = [];
for (var gpu=0; gpu<8; gpu++) {
hashrates[gpu] = [];
rates[gpu] = [];
}
ws.onmessage = function (evt) {
var now = new Date();
var ts = Math.round(now/1000);
var data = evt.data.split('|');
for (n in data) {
var map = data[n].split(';');
var gpu = 0;
var uid = 0;
var plot = {};
for (k in map) {
var kv = map[k].split('=');
if (kv.length == 1)
continue;
if (kv[0] === 'GPU')
gpu = parseInt(kv[1], 10);
else if (kv[0] === 'ID')
uid = parseInt(kv[1], 10);
else if (kv[0] === 'TS')
plot.timestamp = parseInt(kv[1], 10);
else if (kv[0] === 'KHS')
plot.hashrate = parseInt(kv[1], 10) / 1000.0;
console.log('Data received: #GPU'+gpu+': '+kv[0]+' = '+kv[1]);
}
if (uid == 0)
continue;
rates[gpu][uid] = [+new Date(plot.timestamp*1000), plot.hashrate];
}
// sort values with id
for (gpu in rates) {
for (uid in rates[gpu])
hashrates[gpu].push(rates[gpu][uid]);
}
drawChart(ip);
};
ws.onerror = function (evt) {
var w = evt.target;
console.log('Error! readyState=' + w.readyState); //log errors
$('#container').html('Error! Unable to get WebSocket data from '+ip); //log errors
return false;
};
ws.onclose = function() {
// websocket is closed.
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
$(function () {
//getData('192.168.0.110', 4068);
getData('localhost', 4068);
});
</script>
</body>
</html>