use tinypool
This commit is contained in:
parent
67439780ed
commit
27b69c5d0d
|
@ -2,5 +2,10 @@ import { resolve } from "path";
|
||||||
import Tinypool from "tinypool";
|
import Tinypool from "tinypool";
|
||||||
|
|
||||||
const udpClientsPool = new Tinypool({
|
const udpClientsPool = new Tinypool({
|
||||||
filename: resolve(__dirname, "./udpClient.ts")
|
filename: resolve(__dirname, "./udpClient.ts"),
|
||||||
|
workerData: {}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export function send() {
|
||||||
|
udpClientsPool.run({}, { name: "send" })
|
||||||
|
}
|
||||||
|
|
|
@ -1,33 +1,35 @@
|
||||||
import { parentPort } from "worker_threads"
|
|
||||||
import { MsgProtocol } from "./msgProtocol"
|
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
|
import { type udp } from "bun"
|
||||||
|
|
||||||
declare var self: Worker
|
declare var self: Worker
|
||||||
export type UDPServerMsgType = "port"
|
export type UDPServerMsgType = "port"
|
||||||
|
|
||||||
|
interface BinaryTypeList {
|
||||||
|
arraybuffer: ArrayBuffer;
|
||||||
|
buffer: Buffer;
|
||||||
|
uint8array: Uint8Array;
|
||||||
|
// TODO: DataView
|
||||||
|
// dataview: DataView;
|
||||||
|
}
|
||||||
|
type BinaryType = keyof BinaryTypeList;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const udpServer = await Bun.udpSocket({
|
const udpServer = await Bun.udpSocket({
|
||||||
port: 33000,
|
port: 33000,
|
||||||
socket: {
|
socket: {
|
||||||
data(_socket, _buf, _port, _addr) {
|
data(
|
||||||
|
_socket: udp.Socket<BinaryType>,
|
||||||
|
data: BinaryTypeList[BinaryType],
|
||||||
|
port: number,
|
||||||
|
address: string,
|
||||||
|
) {
|
||||||
// todo : Handle Recieved Data
|
// todo : Handle Recieved Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
parentPort?.on("message", (msg: MsgProtocol.MessageQuery) => {
|
|
||||||
if (MsgProtocol.isMessageQuery(msg)) {
|
|
||||||
switch (msg.command) {
|
|
||||||
case "port": {
|
|
||||||
postMessage(MsgProtocol.genMessageResult(udpServer.port, msg))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
postMessage("UDP Server Start Successfully!")
|
export function udpServerPort(): number {
|
||||||
|
return udpServer.port
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue