replace pubnub with centrifuge

This commit is contained in:
Selim Mustafaev 2021-11-15 23:04:33 +03:00
parent 7d00a77f8b
commit 50b1956937
3 changed files with 391 additions and 1612 deletions

View File

@ -1,8 +1,9 @@
const crypto = require('crypto');
const fetch = require('node-fetch');
const Vehicle = require('../models/vehicle');
const PubNub = require('pubnub');
const DebugInfo = require('../models/DebugInfo');
const Centrifuge = require('centrifuge');
const SockJS = require('sockjs-client');
const baseUrl = 'https://avtocod.ru/api/v3';
let deviceToken = crypto.createHash('sha256').update(Date.now().toString()).digest().toString('hex');
@ -36,13 +37,13 @@ function decryptReport(report, hash) {
return JSON.parse(decrypted.toString());
}
function waitForReport(pubnubConfig, channel) {
function waitForReport(centrifugoConfig, channel) {
return new Promise((resolve, reject) => {
let report = null;
let retry = 0;
let centrifuge = new Centrifuge(centrifugoConfig.uri, { sockjs: SockJS });
let timeout = setTimeout(() => {
pubnub.unsubscribeAll();
centrifuge.disconnect();
if(report != null) {
resolve(report);
} else {
@ -50,26 +51,18 @@ function waitForReport(pubnubConfig, channel) {
}
}, 10000);
let pubnub = new PubNub(pubnubConfig);
pubnub.addListener({
status: function(event) {
if(event.error == true && event.operation == 'PNSubscribeOperation' && event.category == 'PNAccessDeniedCategory' && ++retry < 5) {
setTimeout(() => {
event.errorData.payload.channels.forEach(c => pubnub.subscribe({ channels: [c] }));
}, 1000);
}
},
message: function(message) {
if(message.message.event == 'report-ready') {
centrifuge.subscribe(channel, message => {
if(message.data.event == 'report-ready') {
clearTimeout(timeout);
pubnub.unsubscribeAll();
resolve(message.message.data);
} else if(message.message.event == 'report-updated') {
report = message.message.data;
}
centrifuge.disconnect();
resolve(message.data);
} else if(message.data.event == 'report-updated') {
report = message.data;
}
});
pubnub.subscribe({ channels: [channel] });
centrifuge.setToken(centrifugoConfig.token);
centrifuge.connect();
});
}
@ -93,7 +86,7 @@ class AvtocodProvider {
let mainData = JSON.parse(result[1]);
let report = decryptReport(mainData.report_container.report, hash);
if(report == null) {
report = await waitForReport(mainData.pubnub, mainData.report_container.channel);
report = await waitForReport(mainData.centrifugo, mainData.report_container.channel);
}
let vehicle = Vehicle.fromAvtocod(report);
console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original);

1953
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,7 @@
"dependencies": {
"abort-controller": "^3.0.0",
"body-parser": "^1.19.0",
"centrifuge": "^2.8.3",
"compress": "^0.99.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
@ -24,8 +25,8 @@
"mongodb": "^3.6.1",
"node-fetch": "^2.6.1",
"node-html-parser": "^2.0.0",
"pubnub": "^4.29.6",
"response-time": "^2.3.2",
"sockjs-client": "^1.5.2",
"utf8": "^3.0.0",
"uuid": "^8.3.0"
},