gin/examples/realtime-advanced/resources/static/realtime.js

145 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-05-13 00:35:16 +00:00
function StartRealtime(roomid, timestamp) {
StartEpoch(timestamp);
StartSSE(roomid);
StartForm();
}
function StartForm() {
$('#chat-message').focus();
$('#chat-form').ajaxForm(function() {
$('#chat-message').val('');
$('#chat-message').focus();
});
}
function StartEpoch(timestamp) {
var windowSize = 60;
var height = 200;
var defaultData = histogram(windowSize, timestamp);
window.heapChart = $('#heapChart').epoch({
type: 'time.area',
axes: ['bottom', 'left'],
height: height,
2015-05-14 16:16:00 +00:00
historySize: 10,
2015-05-13 00:35:16 +00:00
data: [
{values: defaultData},
{values: defaultData}
]
});
window.mallocsChart = $('#mallocsChart').epoch({
type: 'time.area',
axes: ['bottom', 'left'],
height: height,
2015-05-14 16:16:00 +00:00
historySize: 10,
2015-05-13 00:35:16 +00:00
data: [
{values: defaultData},
{values: defaultData}
]
});
2015-05-13 14:44:44 +00:00
2015-05-14 15:01:02 +00:00
window.messagesChart = $('#messagesChart').epoch({
2015-05-14 16:16:00 +00:00
type: 'time.line',
2015-05-14 15:01:02 +00:00
axes: ['bottom', 'left'],
2015-05-14 16:16:00 +00:00
height: 240,
historySize: 10,
2015-05-14 15:01:02 +00:00
data: [
2015-05-14 16:16:00 +00:00
{values: defaultData},
2015-05-14 15:01:02 +00:00
{values: defaultData},
{values: defaultData}
]
});
2015-05-13 00:35:16 +00:00
}
function StartSSE(roomid) {
if (!window.EventSource) {
alert("EventSource is not enabled in this browser");
return;
}
var source = new EventSource('/stream/'+roomid);
source.addEventListener('message', newChatMessage, false);
source.addEventListener('stats', stats, false);
}
function stats(e) {
2015-05-14 16:16:00 +00:00
var data = parseJSONStats(e.data);
heapChart.push(data.heap);
mallocsChart.push(data.mallocs);
messagesChart.push(data.messages);
2015-05-13 00:35:16 +00:00
}
function parseJSONStats(e) {
var data = jQuery.parseJSON(e);
var timestamp = data.timestamp;
var heap = [
{time: timestamp, y: data.HeapInuse},
{time: timestamp, y: data.StackInuse}
];
var mallocs = [
{time: timestamp, y: data.Mallocs},
{time: timestamp, y: data.Frees}
];
2015-05-13 14:44:44 +00:00
var messages = [
2015-05-14 16:16:00 +00:00
{time: timestamp, y: data.Connected},
2015-05-13 14:44:44 +00:00
{time: timestamp, y: data.Inbound},
{time: timestamp, y: data.Outbound}
];
2015-05-14 16:16:00 +00:00
2015-05-13 00:35:16 +00:00
return {
heap: heap,
mallocs: mallocs,
2015-05-13 14:44:44 +00:00
messages: messages
2015-05-13 00:35:16 +00:00
}
}
function newChatMessage(e) {
var data = jQuery.parseJSON(e.data);
2015-05-14 02:39:17 +00:00
var nick = data.nick;
var message = data.message;
var style = rowStyle(nick);
var html = "<tr class=\""+style+"\"><td>"+nick+"</td><td>"+message+"</td></tr>";
2015-05-13 00:35:16 +00:00
$('#chat').append(html);
$("#chat-scroll").scrollTop($("#chat-scroll")[0].scrollHeight);
}
function histogram(windowSize, timestamp) {
var entries = new Array(windowSize);
for(var i = 0; i < windowSize; i++) {
entries[i] = {time: (timestamp-windowSize+i-1), y:0};
}
return entries;
}
var entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': '&quot;',
"'": '&#39;',
"/": '&#x2F;'
};
2015-05-14 02:39:17 +00:00
function rowStyle(nick) {
var classes = ['active', 'success', 'info', 'warning', 'danger'];
var index = hashCode(nick)%5;
return classes[index];
}
function hashCode(s){
return Math.abs(s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0));
}
2015-05-13 00:35:16 +00:00
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
window.StartRealtime = StartRealtime