forked from Soundofdarkness/RuneBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcu-api.js
97 lines (84 loc) · 1.95 KB
/
lcu-api.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
const WebSocket = require('ws');
const request = require('request');
var ws = null;
var conn_data = null;
// messages we listen to
var message_filter = [
'/lol-champ-select/v1/session:Delete',
'/lol-champ-select/v1/session:Update',
'/lol-perks/v1/currentpage:Update',
'/lol-perks/v1/perks:Update',
'/lol-summoner/v1/current-summoner:Update'
];
function bind(data) {
conn_data = data;
ws = new WebSocket(`wss://${data.username}:${data.password}@${data.address}:${data.port}/`, "wamp", {
rejectUnauthorized: false,
});
ws.on('error', (err) => {
console.log(err);
if (err.message.includes('ECONNREFUSED')) {
destroy();
setTimeout(function () {
bind(data);
}, 1000);
}
});
ws.on('message', (msg) => {
var res;
try {
res = JSON.parse(msg);
} catch(e) {
console.log(e);
}
if(res[0] === 0) {
console.log("connected", res);
freezer.emit(`api:connected`);
}
if(res[1] == "OnJsonApiEvent") {
var evt = res[2];
var url = `${evt.uri}:${evt.eventType}`;
if (message_filter.includes(url)){
//console.log(url);
freezer.emit(url, evt.data);
}
}
});
ws.on('open', () => {
ws.send('[5, "OnJsonApiEvent"]');
});
}
function destroy() {
ws.removeEventListener()
ws = null;
}
var methods = {};
["post", "put", "get", "del"].forEach(function(method) {
methods[method] = function(endpoint, body) {
return new Promise(resolve => {
if(ws == null)
return;
var options = {
url: `${conn_data.protocol}://${conn_data.address}:${conn_data.port}${endpoint}`,
auth: {
"user": conn_data.username,
"pass": conn_data.password
},
headers: {
'Accept': 'application/json'
},
json: true,
body: body,
rejectUnauthorized: false
};
request[method](options, (error, response, data) => {
if (error || response.statusCode != 200) {
resolve();
return;
}
resolve(data);
});
});
};
});
module.exports = Object.assign({bind, destroy}, methods);