68 lines
2.6 KiB
JavaScript
68 lines
2.6 KiB
JavaScript
const net = require('net');
|
||
|
||
module.exports = (app) => {
|
||
const { middleware } = app;
|
||
const shunt = middleware.shunt;
|
||
|
||
net.createServer(function (socket) {
|
||
socket.on('lookup', function (data) {
|
||
console.log(data);
|
||
console.log('lookup')
|
||
});
|
||
socket.on('connection', function (data) {
|
||
console.log(data);
|
||
console.log('socket客户端已连接');
|
||
});
|
||
socket.on('data', async function (data) {
|
||
let value = data.toString('hex');
|
||
console.log(value);
|
||
let response = "000000068100";
|
||
socket.write(Buffer.from(response, 'hex'));
|
||
// 00000043010000000000000000000000000000000000000000 18 31353037365f32303236303330333135313835315f323832 00 0f 0102030405060708090a0b0c0d0e0f
|
||
let sn_value = value.slice(52, 100),
|
||
snBuffer = Buffer.from(sn_value, 'hex'),
|
||
snNumber = snBuffer.toString('ascii').split("_")[0],
|
||
msg_value = value.slice(104, value.length),
|
||
msgBuffer = Buffer.from(msg_value, 'hex'),
|
||
msgData = msgBuffer.toString('ascii');
|
||
console.log(msgData)
|
||
// socket接收消息后触发事件监听器,将消息发送给websocket客户端并存储在数据库中
|
||
shunt.socketEmitter().emit(snNumber, { msg_data: msgData, 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: {
|
||
sn_number: snNumber,
|
||
original_msg: value,
|
||
msg_data: msgData,
|
||
created_at: new Date()
|
||
},
|
||
});
|
||
});
|
||
socket.on('end', function (data) {
|
||
console.log(data);
|
||
console.log('end');
|
||
});
|
||
socket.on('timeout', function (data) {
|
||
console.log(data);
|
||
console.log('空闲超时');
|
||
});
|
||
socket.on('drain', function (data) {
|
||
console.log(data);
|
||
console.log('写缓存为空');
|
||
});
|
||
socket.on('error', function (data) {
|
||
console.log(data);
|
||
console.log('发生错误');
|
||
});
|
||
socket.on('close', function (data) {
|
||
console.log(data);
|
||
console.log('socket客户端已断开连接');
|
||
});
|
||
socket.pipe(socket);
|
||
}).listen(8100, function () {
|
||
console.log('socket服务器已启动,监听端口:8100');
|
||
});
|
||
} |