From cb3e2cc0cee4289af0ba71cc7c83691bbd25d620 Mon Sep 17 00:00:00 2001 From: Frost-ZX Date: Wed, 11 Jun 2025 09:36:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20UDP=20=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.js | 5 ++ src/main/udp-client.js | 102 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/main/udp-client.js diff --git a/src/main/index.js b/src/main/index.js index 7d234f3..46b6185 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -14,6 +14,10 @@ import { initTrayItems, } from './tray'; +import { + initUdpClient, +} from './udp-client'; + import icon from '../../resources/icon.png?asset'; import process from 'process'; @@ -71,6 +75,7 @@ app.whenReady().then(() => { createWindow(); initTrayItems(); + initUdpClient(); app.on('activate', function () { // On macOS it's common to re-create a window in the app when the diff --git a/src/main/udp-client.js b/src/main/udp-client.js new file mode 100644 index 0000000..38dad67 --- /dev/null +++ b/src/main/udp-client.js @@ -0,0 +1,102 @@ +import { createSocket } from 'dgram'; + +/** 模块名称 */ +const MODULE_NAME = '[udp-client]'; + +/** + * @desc Socket 对象 + * @type { import('dgram').Socket } + */ +let instance = null; + +/** 初始化 UDP 客户端 */ +export function initUdpClient() { + + // 创建对象 + instance = createSocket('udp4'); + + // 监听事件:异常 + instance.on('error', function (error) { + console.error('-'.repeat(32)); + console.error(MODULE_NAME, '出现异常:'); + console.error(String(error)); + console.error('-'.repeat(32)); + }); + + // 监听事件:端口监听 + instance.on('listening', function () { + let info = instance.address(); + console.info(MODULE_NAME, `端口监听成功,地址 ${info.address},端口 ${info.port}`); + }); + + // 监听事件:消息 + instance.on('message', function (message, remote) { + + let { address: rAddress, port: rPort } = remote; + + let msgStr = String(message); + + console.info('-'.repeat(32)); + console.info(MODULE_NAME, `收到消息 | 地址:${rAddress} | 端口:${rPort}`); + console.info(msgStr); + console.info('-'.repeat(32)); + + }); + + // 绑定端口,初始化 + instance.bind(0, function () { + try { + instance.setBroadcast(false); + instance.setMulticastTTL(128); + // instance.addMembership(''); + // instance.setMulticastInterface(''); + } catch (error) { + console.error(MODULE_NAME, '更新配置失败:'); + console.error(error); + } + }); + +} + +/** + * @description 发送消息(ASCII) + * @param {object} options + * @param {string} options.data 消息内容 + * @param {string} options.ip 目标地址 + * @param {number} options.port 目标端口 + */ +export function sendMessage(options = {}) { + try { + + if (!instance) { + console.error(MODULE_NAME, '消息发送失败:未初始化'); + return; + } + + let { data, ip, port } = options; + + let buffer = Buffer.from(data, 'ascii'); + + console.info('-'.repeat(32)); + console.info(MODULE_NAME, `发送消息 | 地址:${ip} | 端口:${port}`); + console.info(data); + console.info('-'.repeat(32)); + + // 发送消息 + instance.send(buffer, port, ip, (error) => { + if (error) { + console.error('-'.repeat(32)); + console.error(MODULE_NAME, '消息发送失败:'); + console.error(String(error)); + console.error('-'.repeat(32)); + } + }); + + } catch (error) { + console.error('-'.repeat(32)); + console.error(MODULE_NAME, '消息发送异常:'); + console.error(String(error)); + console.error('-'.repeat(32)); + } + +}