realtime.js 3.6 KB

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