卫星通信服务平台

This commit is contained in:
2026-03-27 17:57:44 +08:00
commit 21bcbf78f8
35 changed files with 1915 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
// socket事件监听器
const EventEmitter = require('events');
const socketEmitter = new EventEmitter();
module.exports = () => {
return socketEmitter;
};

View File

@@ -0,0 +1,68 @@
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');
});
}

View File

@@ -0,0 +1,72 @@
// 错误日志
const fs = require('fs');
const os = require('os');
const path = require('path');
const dirCheck = (dirPath) => {
// 检查文件夹是否存在
fs.access(dirPath, fs.constants.F_OK, (err) => {
if (err) {
// 文件夹不存在
console.log('文件夹不存在,正在创建...');
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) throw err;
console.log('文件夹已创建');
});
} else {
// 文件夹存在
console.log('文件夹已存在');
}
});
}
module.exports = (filePath, msg) => {
// 获取时间
let now = new Date(),
times = {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
second: now.getSeconds()
};
// 获取操作系统类型
const type = os.type();
console.log(`操作系统类型: ${type}`);
filePath = path.join(__dirname, filePath);
dirCheck(filePath);
const yearPath = filePath + '/' + times.year;
dirCheck(yearPath);
const monthPath = yearPath + '/' + times.month;
dirCheck(monthPath);
const dayPath = monthPath + '/' + times.day + '.txt';
const logTime = '[' + times.year + '-' + times.month + '-' + times.day + '>>>' + times.hour + ':' + times.minute + ':' + times.second + ']' + '>>>';
let crlf;
// 判断操作系统类型,设置换行符
if (type === 'Windows_NT') {
crlf = '\n';
} else {
crlf = '\r\n';
}
// 读取文件内容
fs.readFile(dayPath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// 文件不存在,直接写入新内容(这里实际上还是使用了覆盖的方式)
fs.writeFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('文件已创建并写入新内容');
});
} else {
throw err;
}
} else {
// 文件存在,追加新内容
fs.appendFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('新内容已追加到文件');
});
}
});
};

View File

@@ -0,0 +1,47 @@
// webSocket服务端
const http = require('http');
const { Server } = require('socket.io');
module.exports = (app) => {
// 创建HTTP服务器并监听8110端口
const server = http.createServer(function (req, res) {
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
}).listen(app.config.webSocketServer.port);
let io = new Server(server, {
cors: {
origin: app.config.webSocketServer.allowedOrigins, // 生产环境应指定具体域名
methods: ["GET", "POST"]
},
// path: app.config.webSocketServer.path,
transports: ['websocket', 'polling'],
connectionStateRecovery: {}
});
console.log(`websocket: ${ app.config.webSocketServer.ip }服务器已启动,监听端口:${ app.config.webSocketServer.port }`);
const socketEmitter = app.middleware.shunt.socketEmitter();
const utils = app.middleware.utils;
io.on('connection', (socket) => {
socket.on('message', (snNumber) => {
if(socketEmitter.listeners(snNumber).length !== 0) {
socketEmitter.removeAllListeners(snNumber);
};
socketEmitter.on(snNumber, (msg) => {
socket.emit(snNumber, msg);
});
});
});
io.on('error', (err) => {
utils.log('../../webSocketLog', err);
io = new Server(app.config.webSocketServer.port, {
cors: true,
connectionStateRecovery: {}
});
});
};