-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
53 lines (50 loc) · 1.45 KB
/
example.js
File metadata and controls
53 lines (50 loc) · 1.45 KB
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
// User data
const wsUrl = 'ws://localhost:9274/api';
const userdata = {
login: 'test',
password: 'test'
};
// Log helper
const log = {
el: document.getElementById('log'),
append(message) {
this.el.append(`${message}\n`);
},
appendData(data) {
this.append(JSON.stringify(data));
}
}
// Api usage example
const api = new GravitApi();
api.onOpen = () => {
log.append('Соединение установлено');
}
api.onClose = (e) => {
if (e.wasClean) return log.append('Соединение закрыто');
if (e.code === 1006) log.append('Разрыв соединения');
}
api.onError = () => {
log.append('Ошибка при подключеннии!');
}
api.connect(wsUrl)
.then(() => {
api.send('getAvailabilityAuth').then(auth => {
log.appendData(auth);
auth = auth.list.pop();
log.append(`Выбран первый профиль авторизации: ${auth.displayName}`);
api.send('auth', {
login: userdata.login,
password: {
password: userdata.password,
type: "plain"
},
auth_id: auth.name,
getSession: false,
authType: "API",
initProxy: false
}).then(res => {
log.appendData(res);
api.close();
}).catch(error => log.appendData(error));
}).catch(error => log.appendData(error));
}).catch(console.error);