/
client.js
33 lines (30 loc) · 1009 Bytes
/
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
var hostname = document.location.hostname,
connection = new WebSocket(location.origin.replace(/^http/, 'ws')),
count,
msg = {
console: $('#event-console'),
log: function(message) {
var log = $('<div class="event-msg"></div>');
log.html(message).prependTo(msg.console);
},
installed_list: $('#installed-modules'),
install: function(name, url) {
msg.installed_list.append('<li><a href="' + url + '">' + name + '</a></li>');
}
};
// When the connection is open, send some data to the server
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e) {
var response = JSON.parse(e.data);
//console.log('Server: ' + e.data);
if (typeof(msg[response.type]) === 'function') {
msg[response.type](response);
}
};