realtime.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.bar',
  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. if($('#messagesChart').length ) {
  44. window.messagesChart = $('#messagesChart').epoch({
  45. type: 'time.area',
  46. axes: ['bottom', 'left'],
  47. height: 250,
  48. data: [
  49. {values: defaultData},
  50. {values: defaultData}
  51. ]
  52. });
  53. }
  54. }
  55. function StartSSE(roomid) {
  56. if (!window.EventSource) {
  57. alert("EventSource is not enabled in this browser");
  58. return;
  59. }
  60. var source = new EventSource('/stream/'+roomid);
  61. source.addEventListener('message', newChatMessage, false);
  62. source.addEventListener('stats', stats, false);
  63. }
  64. function stats(e) {
  65. var data = parseJSONStats(e.data)
  66. heapChart.push(data.heap)
  67. mallocsChart.push(data.mallocs)
  68. goroutinesChart.push(data.goroutines)
  69. if (typeof messagesChart !== 'undefined') {
  70. messagesChart.push(data.messages)
  71. }
  72. }
  73. function parseJSONStats(e) {
  74. var data = jQuery.parseJSON(e);
  75. var timestamp = data.timestamp;
  76. var heap = [
  77. {time: timestamp, y: data.HeapInuse},
  78. {time: timestamp, y: data.StackInuse}
  79. ];
  80. var mallocs = [
  81. {time: timestamp, y: data.Mallocs},
  82. {time: timestamp, y: data.Frees}
  83. ];
  84. var messages = [
  85. {time: timestamp, y: data.Inbound},
  86. {time: timestamp, y: data.Outbound}
  87. ];
  88. var goroutines = [
  89. {time: timestamp, y: data.NuGoroutines},
  90. ]
  91. return {
  92. heap: heap,
  93. mallocs: mallocs,
  94. goroutines: goroutines,
  95. messages: messages
  96. }
  97. }
  98. function newChatMessage(e) {
  99. var data = jQuery.parseJSON(e.data);
  100. var nick = escapeHtml(data.nick);
  101. var message = escapeHtml(data.message);
  102. var html = "<tr><td>"+nick+"</td><td>"+message+"</td></tr>";
  103. $('#chat').append(html);
  104. $("#chat-scroll").scrollTop($("#chat-scroll")[0].scrollHeight);
  105. }
  106. function histogram(windowSize, timestamp) {
  107. var entries = new Array(windowSize);
  108. for(var i = 0; i < windowSize; i++) {
  109. entries[i] = {time: (timestamp-windowSize+i-1), y:0};
  110. }
  111. return entries;
  112. }
  113. var entityMap = {
  114. "&": "&amp;",
  115. "<": "&lt;",
  116. ">": "&gt;",
  117. '"': '&quot;',
  118. "'": '&#39;',
  119. "/": '&#x2F;'
  120. };
  121. function escapeHtml(string) {
  122. return String(string).replace(/[&<>"'\/]/g, function (s) {
  123. return entityMap[s];
  124. });
  125. }
  126. window.StartRealtime = StartRealtime