31 lines
841 B
TypeScript
31 lines
841 B
TypeScript
import { type udp } from "bun";
|
|
import { MsgProtocol } from "./msgProtocol";
|
|
import { parentPort } from "worker_threads";
|
|
|
|
declare var self: Worker
|
|
|
|
const udpClient = await Bun.udpSocket({})
|
|
|
|
parentPort?.on("message", (msg: MsgProtocol.MessageQuery) => {
|
|
if (MsgProtocol.isMessageQuery(msg)) {
|
|
switch (msg.command) {
|
|
case "send": {
|
|
const args = msg.data.args as {
|
|
data: udp.Data,
|
|
port: number,
|
|
address: string,
|
|
}
|
|
udpClient.send(args.data, args.port, args.address)
|
|
postMessage(MsgProtocol.genMessageResult("Send Successfully", msg))
|
|
break
|
|
}
|
|
default: {
|
|
postMessage(MsgProtocol.genMessageError("No Such Command", msg))
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
postMessage(MsgProtocol.genMessageError("No Currenct Destination", msg))
|
|
}
|
|
})
|