This commit is contained in:
2022-06-23 22:31:08 +07:00
parent 5dc698b60c
commit ad85a23b1d
8 changed files with 371 additions and 54 deletions
+24 -10
View File
@@ -86,16 +86,30 @@ const controller = async (req, res) => {
respCode = generate.getResponseCode(respData);
respData1 = generate.getResponseData(respData);
result = {
code: "00",
message: "Request Authenticate 1 Success",
data: {
trxID: generate.buf2hex(transactionId),
responseCode: respCode,
responseData: respData1,
responseLength: respData1.length / 2,
},
};
if (respData1) {
result = {
code: "00",
message: "Request Authenticate 1 Success",
data: {
trxID: generate.buf2hex(transactionId),
responseCode: respCode,
responseData: respData1,
responseLength: respData1.length / 2,
},
};
} else{
result = {
code: "5004",
message: "Empty Data from HSM",
data: {
trxID: generate.buf2hex(transactionId),
responseCode: respCode,
responseData: respData1,
responseLength: respData1.length / 2,
},
};
}
// log debug
winston.logger.debug(
+53 -18
View File
@@ -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 = {