import dgram from 'node:dgram';
//https://nodejs.org/api/dgram.html
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
server.bind(41234);
// Prints: server listening 0.0.0.0:41234
(async()=> {
console.log("== async start ==");
//await delay(1000);
await new Promise(resolve => setTimeout(resolve, 1000));
let sendmsg1 = "message test1";
console.log("send msg: " + sendmsg1);
const client = dgram.createSocket('udp4');
client.send(sendmsg1, 41234, 'localhost', (err) => {
client.close();
});
console.log("== async end ==");
})();
비동기 지연 시간 만들기