-
Notifications
You must be signed in to change notification settings - Fork 8
/
demoBrowserClient.html
96 lines (85 loc) · 3.41 KB
/
demoBrowserClient.html
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
<html>
<head>
<script src="longPolling.js"></script>
</head>
<body>
<h2>This is client example how to work with long polling and REST-API.</h2>
<div id="connected"></div>
<div id="output"></div>
<script>
const IOBROKER_SWAGGER = 'http://localhost:8093/';
// Demo long polling
const output = [];
let subscribed = false;
let lastID = false;
let lastTimeout = 0;
let connectionInterval = null;
let password = '';
function addOutput(data) {
console.log(data);
output.push(data);
if (output.length > 40) {
output.splice(0, output.length - 40);
}
document.getElementById('output').innerHTML = output.join('<br/>');
}
function setConnectionStatus(isConnected) {
if (isConnected) {
connectionInterval && clearInterval(connectionInterval);
connectionInterval = null;
document.getElementById('connected').innerHTML = '<span style="color: green"><b>CONNECTED</b></span>';
connection.getState('system.adapter.rest-api.0.alive')
.then(data => addOutput('system.adapter.rest-api.0.alive: ' + JSON.stringify(data)))
// subscribe must be done only one time by first connection
if (!subscribed) {
subscribed = true;
connection.subscribeStates('system.adapter.rest-api.0.*')
.then(data => addOutput('<span style="color: green">Connected: </span>' + JSON.stringify(data)));
}
} else {
document.getElementById('connected').innerHTML = '<span style="color: red"><b>No connection with ' + IOBROKER_SWAGGER + '</b></span>';
if (isConnected !== undefined) {
addOutput('<span style="color: red">Disconnected. Try to connect every 5 seconds</span>');
}
}
}
setConnectionStatus();
let connection;
function createConnection() {
connection = new LongPolling(IOBROKER_SWAGGER, {
onConnection: setConnectionStatus,
onError: error => {
addOutput('ERROR: ' + JSON.stringify(error));
if (error.toString().includes('Invalid password or user name') || error.toString().includes('User is required')) {
connection.close();
connection = null;
do {
password = window.prompt('Please enter password for "admin":');
} while (!password);
createConnection();
}
},
onEvent: event => {
addOutput('Event: ' + JSON.stringify(event));
},
onConnectionAttempt: timeout => {
lastID = Date.now();
lastTimeout = timeout / 1000;
connectionInterval = connectionInterval || setInterval(() => {
const el = document.getElementById(lastID);
if (el) {
lastTimeout && lastTimeout--;
el.innerText = lastTimeout;
}
}, 1000);
addOutput(`${new Date().toLocaleString()} Try to connect...<span id="${lastID}">${lastTimeout}</span>`);
},
autoConnect: true, // else you must call connection.connect()
user: 'admin',
password,
});
}
createConnection();
</script>
</body>
</html>