mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 12:46:28 +00:00
update
This commit is contained in:
@@ -17,44 +17,69 @@ let url = "wss://gateway04.cyberware.co.jp/feliqserver/v1/wsapi";
|
||||
class WebSocketClient {
|
||||
ws = null;
|
||||
clientAuthenticated = false;
|
||||
openWebSocketStatus = false;
|
||||
|
||||
// Connection of WebSocket
|
||||
open = () => {
|
||||
this.ws = new WebSocket(url, [], options);
|
||||
this.ws.on("open", this.onOpen);
|
||||
this.ws.on("close", this.onClose);
|
||||
try {
|
||||
if (this.ws == null) {
|
||||
this.ws = new WebSocket(url, [], options);
|
||||
|
||||
this.ws.on("open", this.onOpen);
|
||||
this.ws.on("close", this.onClose);
|
||||
this.ws.on("error", this.onError);
|
||||
}
|
||||
|
||||
this.openWebSocketStatus = true;
|
||||
} catch (error) {
|
||||
this.openWebSocketStatus = false;
|
||||
}
|
||||
};
|
||||
// Disconnection of WebSocket
|
||||
close = () => {
|
||||
this.ws.close();
|
||||
// console.log('WebSocket Close');
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`websocket close`);
|
||||
// log info
|
||||
winston.logger.info(`websocket close`);
|
||||
};
|
||||
|
||||
// Open event
|
||||
onOpen = () => {
|
||||
// console.log("WebSocket Open");
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`websocket open`);
|
||||
|
||||
this.isOpened();
|
||||
if (!this.clientAuthenticated) {
|
||||
this.clientAuth();
|
||||
// log debug
|
||||
winston.logger.debug(`opening websocket`);
|
||||
|
||||
if (this.isOpened()) {
|
||||
// log info
|
||||
winston.logger.info(`websocket open successfully`);
|
||||
|
||||
if (!this.clientAuthenticated) {
|
||||
this.clientAuth();
|
||||
}
|
||||
} else {
|
||||
// log error
|
||||
winston.logger.error(`websocket open failed`);
|
||||
}
|
||||
};
|
||||
|
||||
onClose = () => {
|
||||
// console.log("WebSocket Close");
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`websocket close`);
|
||||
|
||||
// log info
|
||||
winston.logger.info(`websocket close`);
|
||||
|
||||
this.clientAuthenticated = false;
|
||||
};
|
||||
|
||||
onError = () => {
|
||||
// console.log("WebSocket Error");
|
||||
|
||||
// log info
|
||||
winston.logger.info(`websocket error`);
|
||||
|
||||
this.clientAuthenticated = false;
|
||||
this.isOpened();
|
||||
this.open();
|
||||
};
|
||||
|
||||
// Connection check
|
||||
@@ -139,8 +164,9 @@ class WebSocketClient {
|
||||
// log debug
|
||||
winston.logger.debug(`websocket resp data tenant: ${respCode}`);
|
||||
|
||||
if (respCode == "0000" || respCode == "0502")
|
||||
if (respCode == "0000" || respCode == "0502") {
|
||||
this.clientAuthenticated = true;
|
||||
}
|
||||
// console.log(`clientAuthenticated = ${this.clientAuthenticated}`);
|
||||
// log debug
|
||||
winston.logger.debug(
|
||||
@@ -155,10 +181,19 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
const client = new WebSocketClient();
|
||||
client.open();
|
||||
if (!client.openWebSocketStatus) {
|
||||
// log info
|
||||
winston.logger.info(`first open websocket`);
|
||||
client.open();
|
||||
}
|
||||
|
||||
setInterval(function () {
|
||||
// let statusConnection = client.clientAuthenticated;
|
||||
// if (!statusConnection) {
|
||||
// log debug
|
||||
winston.logger.debug(`retry open websocket`);
|
||||
client.clientAuth();
|
||||
// }
|
||||
}, 59000);
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user