function connect var ws new WebSocket wss document domain 8888 portsta

 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
function connect(){
var ws = new WebSocket("wss://"+document.domain+":8888/portstate");
return ws;
}
$(document).ready(function() {
var ws = connect();
ws.onopen = function() {
}
ws.onclose = function() {
}
ws.onerror = function(evt) {
alert('Ошибка Websocket-а');
}
ws.onmessage = function(message) {
var json = jQuery.parseJSON(message.data);
var port = $('#port'+json.id+' td.portstate');
var html = ''
if (json.oper==1 && json.admin == 1){
html = '<span class="text-success">Up</span>'
}
else if (json.oper==2 && json.admin == 1){
html = '<span class="text-error">Down</span>'
}
else if (json.admin == 2){
html = '<span class="text-warning">Disabled</span>'
}
if (port.length > 0 && html.length > 0){
port.html(html);
}
};
});