replace pubnub with centrifuge
This commit is contained in:
parent
7d00a77f8b
commit
50b1956937
@ -1,8 +1,9 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const Vehicle = require('../models/vehicle');
|
const Vehicle = require('../models/vehicle');
|
||||||
const PubNub = require('pubnub');
|
|
||||||
const DebugInfo = require('../models/DebugInfo');
|
const DebugInfo = require('../models/DebugInfo');
|
||||||
|
const Centrifuge = require('centrifuge');
|
||||||
|
const SockJS = require('sockjs-client');
|
||||||
|
|
||||||
const baseUrl = 'https://avtocod.ru/api/v3';
|
const baseUrl = 'https://avtocod.ru/api/v3';
|
||||||
let deviceToken = crypto.createHash('sha256').update(Date.now().toString()).digest().toString('hex');
|
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());
|
return JSON.parse(decrypted.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForReport(pubnubConfig, channel) {
|
function waitForReport(centrifugoConfig, channel) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let report = null;
|
let report = null;
|
||||||
let retry = 0;
|
let centrifuge = new Centrifuge(centrifugoConfig.uri, { sockjs: SockJS });
|
||||||
|
|
||||||
let timeout = setTimeout(() => {
|
let timeout = setTimeout(() => {
|
||||||
pubnub.unsubscribeAll();
|
centrifuge.disconnect();
|
||||||
if(report != null) {
|
if(report != null) {
|
||||||
resolve(report);
|
resolve(report);
|
||||||
} else {
|
} else {
|
||||||
@ -50,26 +51,18 @@ function waitForReport(pubnubConfig, channel) {
|
|||||||
}
|
}
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
let pubnub = new PubNub(pubnubConfig);
|
centrifuge.subscribe(channel, message => {
|
||||||
pubnub.addListener({
|
if(message.data.event == 'report-ready') {
|
||||||
status: function(event) {
|
clearTimeout(timeout);
|
||||||
if(event.error == true && event.operation == 'PNSubscribeOperation' && event.category == 'PNAccessDeniedCategory' && ++retry < 5) {
|
centrifuge.disconnect();
|
||||||
setTimeout(() => {
|
resolve(message.data);
|
||||||
event.errorData.payload.channels.forEach(c => pubnub.subscribe({ channels: [c] }));
|
} else if(message.data.event == 'report-updated') {
|
||||||
}, 1000);
|
report = message.data;
|
||||||
}
|
|
||||||
},
|
|
||||||
message: function(message) {
|
|
||||||
if(message.message.event == 'report-ready') {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
pubnub.unsubscribeAll();
|
|
||||||
resolve(message.message.data);
|
|
||||||
} else if(message.message.event == 'report-updated') {
|
|
||||||
report = message.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 mainData = JSON.parse(result[1]);
|
||||||
let report = decryptReport(mainData.report_container.report, hash);
|
let report = decryptReport(mainData.report_container.report, hash);
|
||||||
if(report == null) {
|
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);
|
let vehicle = Vehicle.fromAvtocod(report);
|
||||||
console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original);
|
console.log('Avtocod found vehicle: ', vehicle?.brand?.name?.original);
|
||||||
|
|||||||
1959
package-lock.json
generated
1959
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -13,6 +13,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"abort-controller": "^3.0.0",
|
"abort-controller": "^3.0.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
|
"centrifuge": "^2.8.3",
|
||||||
"compress": "^0.99.0",
|
"compress": "^0.99.0",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@ -24,8 +25,8 @@
|
|||||||
"mongodb": "^3.6.1",
|
"mongodb": "^3.6.1",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"node-html-parser": "^2.0.0",
|
"node-html-parser": "^2.0.0",
|
||||||
"pubnub": "^4.29.6",
|
|
||||||
"response-time": "^2.3.2",
|
"response-time": "^2.3.2",
|
||||||
|
"sockjs-client": "^1.5.2",
|
||||||
"utf8": "^3.0.0",
|
"utf8": "^3.0.0",
|
||||||
"uuid": "^8.3.0"
|
"uuid": "^8.3.0"
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user