-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
103 lines (87 loc) · 2.82 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(function (nx) {
/**
* define application
*/
var Shell = nx.define(nx.ui.Application, {
methods: {
start: function () {
// initialize a topology
var topo = new nx.graphic.Topology({
// set the topology view's with and height
width: window.innerWidth,
height: window.innerHeight,
// node config
nodeConfig: {
label: 'model.name',
iconType: 'model.device_type',
color: 'model.color',
},
// link config
linkConfig: {
linkType: 'parallel'
},
// show node's icon, could change to false to show dot
showIcon: true,
//autoLayout: true,
//dataProcessor: "force",
showNavigation: false,
});
topo.data(topologyData); //set data to topology
topo.attach(this); //attach topology to document
function handleMessage(message) {
var node = topologyData.nodes.find(function(node) {
return node.id === message.id;
});
if (node) {
switch (message.message) {
case "error":
topo.getNode(message.id).color("red");
break;
case "active":
topo.getNode(message.id).color("#1884d4");
break;
default:
topo.getNode(message.id).color("grey");
break;
}
}
}
var sock = new WebSocket('ws://127.0.0.1:8081');
sock.addEventListener('open',function(e){
console.log('Socket 接続成功');
});
sock.addEventListener('message',function(e){
console.log(e.data);
var receivedData = JSON.parse(e.data);
handleMessage(receivedData);
//topo.data(topologyData);
});
// WebSocketの接続状態を監視する
sock.addEventListener('open', function (event) {
// 接続成功時の処理
displayStatus('connected', 'green');
});
sock.addEventListener('close', function (event) {
// 接続切断時の処理
displayStatus('disconnected', 'red');
});
// 接続状態を定期的にチェックする
setInterval(function() {
if (sock.readyState === sock.OPEN) {
displayStatus('connected', 'green');
} else {
displayStatus('disconnected', 'red');
}
}, 1000);
// メッセージを画面に表示する関数
function displayStatus(message, color) {
const statusElement = document.getElementById('status');
statusElement.textContent = message;
statusElement.style.color = color;
}
}
}
});
var shell = new Shell();
shell.start();
})(nx);