realtime.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function StartRealtime(roomid, timestamp) {
  2. StartEpoch(timestamp);
  3. StartSSE(roomid);
  4. StartForm();
  5. }
  6. function StartForm() {
  7. $('#chat-message').focus();
  8. $('#chat-form').ajaxForm(function() {
  9. $('#chat-message').val('');
  10. $('#chat-message').focus();
  11. });
  12. }
  13. function StartEpoch(timestamp) {
  14. var windowSize = 60;
  15. var height = 200;
  16. var defaultData = histogram(windowSize, timestamp);
  17. window.goroutinesChart = $('#goroutinesChart').epoch({
  18. type: 'time.area',
  19. axes: ['bottom', 'left'],
  20. height: height,
  21. data: [
  22. {values: defaultData}
  23. ]
  24. });
  25. window.heapChart = $('#heapChart').epoch({
  26. type: 'time.area',
  27. axes: ['bottom', 'left'],
  28. height: height,
  29. data: [
  30. {values: defaultData},
  31. {values: defaultData}
  32. ]
  33. });
  34. window.mallocsChart = $('#mallocsChart').epoch({
  35. type: 'time.area',
  36. axes: ['bottom', 'left'],
  37. height: height,
  38. data: [
  39. {values: defaultData},
  40. {values: defaultData}
  41. ]
  42. });
  43. }
  44. function StartSSE(roomid) {
  45. if (!window.EventSource) {
  46. alert("EventSource is not enabled in this browser");
  47. return;
  48. }
  49. var source = new EventSource('/stream/'+roomid);
  50. source.addEventListener('message', newChatMessage, false);
  51. source.addEventListener('stats', stats, false);
  52. }
  53. function stats(e) {
  54. var data = parseJSONStats(e.data)
  55. heapChart.push(data.heap)
  56. mallocsChart.push(data.mallocs)
  57. goroutinesChart.push(data.goroutines)
  58. }
  59. function parseJSONStats(e) {
  60. var data = jQuery.parseJSON(e);
  61. var timestamp = data.timestamp;
  62. var heap = [
  63. {time: timestamp, y: data.HeapInuse},
  64. {time: timestamp, y: data.StackInuse}
  65. ];
  66. var mallocs = [
  67. {time: timestamp, y: data.Mallocs},
  68. {time: timestamp, y: data.Frees}
  69. ];
  70. var goroutines = [
  71. {time: timestamp, y: data.NuGoroutines},
  72. ]
  73. return {
  74. heap: heap,
  75. mallocs: mallocs,
  76. goroutines: goroutines
  77. }
  78. }
  79. function newChatMessage(e) {
  80. var data = jQuery.parseJSON(e.data);
  81. var nick = escapeHtml(data.nick);
  82. var message = escapeHtml(data.message);
  83. var html = "<tr><td>"+nick+"</td><td>"+message+"</td></tr>";
  84. $('#chat').append(html);
  85. $("#chat-scroll").scrollTop($("#chat-scroll")[0].scrollHeight);
  86. }
  87. function histogram(windowSize, timestamp) {
  88. var entries = new Array(windowSize);
  89. for(var i = 0; i < windowSize; i++) {
  90. entries[i] = {time: (timestamp-windowSize+i-1), y:0};
  91. }
  92. return entries;
  93. }
  94. var entityMap = {
  95. "&": "&amp;",
  96. "<": "&lt;",
  97. ">": "&gt;",
  98. '"': '&quot;',
  99. "'": '&#39;',
  100. "/": '&#x2F;'
  101. };
  102. function escapeHtml(string) {
  103. return String(string).replace(/[&<>"'\/]/g, function (s) {
  104. return entityMap[s];
  105. });
  106. }
  107. window.StartRealtime = StartRealtime