realtime.js 3.4 KB

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