-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add support for websocket calls
Allow to directly get api data in HTML5 Tested on Chrome... IE>=10 required, not tested IE11 seems buggy on connection close... todo Signed-off-by: Tanguy Pruvot <[email protected]>
- Loading branch information
Showing
2 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<script src="//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 = []; | ||
var timestamps = []; | ||
var timeline = []; | ||
|
||
function drawChart(ip) { | ||
|
||
$('#container').highcharts({ | ||
title: { | ||
text: 'CCMiner WebSocket Sample - ' + ip, | ||
x: -20 //center | ||
}, | ||
subtitle: { | ||
text: 'By tpruvot@gibhub 2014', | ||
x: -20 | ||
}, | ||
xAxis: { | ||
title: { | ||
text: 'Age' | ||
}, | ||
valueSuffix: ' s', | ||
categories: timestamps[0] | ||
}, | ||
yAxis: { | ||
title: { | ||
text: 'Hash Rate (KH/s)' | ||
}, | ||
plotLines: [{ | ||
value: 0, | ||
width: 1, | ||
color: '#808080' | ||
}] | ||
}, | ||
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'); | ||
for (var gpu=0; gpu<8; gpu++) { | ||
timestamps[gpu] = []; | ||
hashrates[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 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] === 'TS') | ||
plot.timestamp = ts - 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]); | ||
} | ||
timeline[n] = plot.timestamp; | ||
timestamps[gpu][n] = plot.timestamp; | ||
hashrates[gpu][n] = plot.hashrate; | ||
} | ||
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> |