/
jquery.client.js
68 lines (60 loc) · 2.21 KB
/
jquery.client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// In case we leave a console.*** in the code without native support
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info, log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c;})(window.console=window.console||{});
(function ($) {
// Shorthand jQuery selector cache. Only use on selectors for the DOM that won't change.
var $$ = (function() {
var cache = {};
return function(selector) {
if (!cache[selector]) {
cache[selector] = $(selector);
}
return cache[selector];
};
})();
var socketIoClient = io.connect(null, {
'port': '#socketIoPort#'
, 'rememberTransport': true
, 'transports': ['xhr-polling']
});
socketIoClient.on('connect', function () {
$$('#connected').addClass('on').find('strong').text('Online');
});
socketIoClient.on('message', function(json) {
var doc = JSON.parse(json);
if (doc) {
var msg = doc.actor.displayName + " " + doc.title + " " + doc.object.displayName;
if (doc.target) {
msg+= " in " + doc.target.displayName;
}
if (doc.generator) {
msg+= " via " + doc.generator.displayName;
}
if (doc.object && doc.object.content) {
msg+= ": " + doc.object.content;
}
var $li = $('<li>').text(msg).append($('<img class="avatar">').attr('src', doc.actor.image.url));
if (doc.provider && doc.provider.icon && doc.provider.icon.url) {
$li.append($('<img class="service">').attr('src', doc.provider.icon.url));
}
$$('#stream ul').prepend($li);
$$('#bubble').scrollTop(98).stop().animate({
'scrollTop': '0'
}, 5000);
setTimeout(function() {
$li.remove();
}, 5000);
if (doc.verb == "connect") {
setTimeout(function() {
socketIoClient.send('Ok great news !');
}, 1000);
} else {
setTimeout(function() {
socketIoClient.send('I am still here');
}, 10000);
}
}
});
socketIoClient.on('disconnect', function() {
$$('#connected').removeClass('on').find('strong').text('Offline');
});
})(jQuery);