Files
bd-short-message-platform/app/middleware/socket/socketClient.js

135 lines
5.2 KiB
JavaScript
Raw Normal View History

2026-03-27 17:51:58 +08:00
// socket客户端
const net = require('net');
module.exports = (app) => {
const { middleware } = app;
const packageBuild = middleware.packageBuild;
const utils = middleware.utils;
const parseDataPackage = middleware.parseDataPackage;
const shunt = middleware.shunt;
const client = new net.Socket();
let times, connectSwitch = true, reconnectSwitch = false;
setTimeout(() => {
reconnectSwitch = true;
}, 66000);
const createClient = () => {
if(connectSwitch) {
connectSwitch = false;
client.connect(app.config.socketClient.port, app.config.socketClient.ip, () => {
console.log(`socket: 连接到服务端: ${app.config.socketClient.ip + ':' + app.config.socketClient.port}`);
client.setKeepAlive(true, 60000);
reconnectSwitch = true;
});
}
};
createClient();
// 重连服务端
const reconnect = () => {
if (reconnectSwitch) {
reconnectSwitch = false;
setTimeout(() => {
connectSwitch = true;
createClient();
}, 66000);
}
};
client.on('connectTimeout', () => {
reconnect();
});
client.on('timeout', () => {
reconnect();
});
client.on('end', () => {
reconnect();
});
client.on('close', () => {
reconnect();
});
client.on('error', (err) => {
utils.log('../../../socketLog', err);
reconnect();
});
client.on('connect', () => {
// 连接成功后的逻辑...
client.write(packageBuild.packageBuild(app, [utils.packageItem(2, 100) + utils.packageItem(2, packageBuild.buildXFType100(app.config.socketClient.token).length / 2) + packageBuild.buildXFType100(app.config.socketClient.token)]));
});
client.on('data', async (data) => {
// 在这里可以处理从服务器接收到的数据
// utils.log('../../../socketLog', data);
let msg = parseDataPackage.parseDataPackage(middleware, data.toString('hex').slice(0, data.toString('hex').length - 16));
if (msg == utils.intrinsic.heart) {
clearTimeout(times);
times = setTimeout(() => {
client.write(Buffer.from(msg, 'hex'));
}, 10000);
} else {
let nowResult = shunt.socketResult(msg.dataContent.nowData);
let resendResult = shunt.socketResult(msg.dataContent.resendData);
// 当前消息处理
if (nowResult.type == 99) {
// token验证处理程序
if (nowResult.data == '1') {
client.write(Buffer.from(utils.intrinsic.heart, 'hex'));
} else {
reconnect();
}
} else if (nowResult.type == 4) {
const { msgContent } = nowResult.data;
const { outboundMsg, receiverAddress, originalMsg } = msgContent;
const { senderAddress, msgData, encodingCategory } = outboundMsg;
// socket接收消息后触发事件监听器,将消息发送给websocket客户端并存储在数据库中
shunt.socketEmitter().emit(senderAddress.data, { encode_type: parseInt(encodingCategory.data), msg_data: msgData.data, created_at: new Date() });
await app.curl('http://127.0.0.1:' + app.server.address().port + '/shortMsg', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
card_id: senderAddress.data,
receiver_address: receiverAddress.data,
original_msg: originalMsg,
send_type: 0,
encode_type: parseInt(encodingCategory.data),
msg_data: msgData.data,
created_at: new Date()
},
});
}
// 重发消息处理
if (resendResult && resendResult.type == 4) {
const { msgContent } = resendResult.data;
const { outboundMsg, receiverAddress } = msgContent;
const { senderAddress, msgData, encodingCategory } = outboundMsg;
// socket接收消息后触发事件监听器,将消息发送给websocket客户端并存储在数据库中
shunt.socketEmitter().emit(senderAddress.data, msgData.data);
await app.curl('http://127.0.0.1:' + app.server.address().port + '/shortMsg', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
card_id: senderAddress.data,
receiver_address: receiverAddress.data,
msg: data.toString(),
send_type: 1,
encode_type: parseInt(encodingCategory.data),
msg_data: msgData.data,
},
});
}
}
});
};