tianji/src/server/udp/server.ts

30 lines
726 B
TypeScript
Raw Normal View History

import dgram from 'dgram';
import { recordServerStatus } from '../model/serverStatus';
export function initUdpServer(port: number) {
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.log(`Init error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
try {
const raw = String(msg);
const json = JSON.parse(String(msg));
2023-10-03 12:45:00 +00:00
console.log('[UDP] recevice tianji report:', raw, 'info', rinfo);
2023-10-03 12:45:00 +00:00
recordServerStatus(json);
} catch (err) {}
});
server.on('listening', () => {
const address = server.address();
console.log(`UDP Server is listening: ${address.address}:${address.port}`);
});
server.bind(port);
}