websocket.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- idea and code swiped from
  5. http://assorted.svn.sourceforge.net/viewvc/assorted/real-time-plotter/trunk/src/rtp.html?view=markup -->
  6. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
  7. <script src="http://people.iola.dk/olau/flot/jquery.flot.js"></script>
  8. <script>
  9. window.onload = function() {
  10. var data = {};
  11. var s = new WebSocket("ws://127.0.0.1:7000/data");
  12. s.onopen = function() {
  13. //alert('open');
  14. s.send('hi');
  15. };
  16. s.onmessage = function(e) {
  17. //alert('got ' + e.data);
  18. var lines = e.data.split('\n');
  19. for (var i = 0; i < lines.length - 1; i++) {
  20. var parts = lines[i].split(' ');
  21. var d = parts[0], x = parseFloat(parts[1]), y = parseFloat(parts[2]);
  22. if (!(d in data)) data[d] = [];
  23. data[d].push([x,y]);
  24. }
  25. var plots = [];
  26. for (var d in data) plots.push( { data: data[d].slice(data[d].length - 200) } );
  27. $.plot( $("#holder"), plots,
  28. {
  29. series: {
  30. lines: { show: true, fill: true },
  31. },
  32. yaxis: { min: 0 },
  33. } );
  34. s.send('');
  35. };
  36. };
  37. </script>
  38. </head>
  39. <body>
  40. <h3>Plot</h3>
  41. <p>(Only tested in Chrome)</p>
  42. <div id="holder" style="width:600px;height:300px"></div>
  43. </body>
  44. </html>