简体   繁体   English

在Express中侦听UDP消息

[英]Listen for UDP messages in Express

I have a basic Node.js server using Express. 我有一个使用Express的基本Node.js服务器。 It needs to be able to handle TCP messages as well as UDP messages. 它需要能够处理TCP消息和UDP消息。 The TCP part is up and running great, but now I need to integrate a way to sniff out the UDP messages. TCP部分运行良好,但是现在我需要集成一种嗅探UDP消息的方法。 I've tried adding a handler to the filter stack using a dgram socket, but it hasn't been successful. 我尝试使用dgram套接字将处理程序添加到过滤器堆栈中,但没有成功。

const express = require('express');
const dgram = require('dgram');

// ...

const app = express();
const dgramSocket = dgram.createSocket('udp4');

// ...

app.use((req, res, next) => {
  dgramSocket.on('listening', () => {
    let addr = app.address();
    console.log(`Listening for UDP packets at ${addr.address}:${addr.port}`);
  });

  dgramSocket.on('error', (err) => {
    console.log(`UDP error: ${err.stack}`);
  });

  dgramSocket.on('message', (msg, rinfo) => {
    console.log(`Received UDP message`);
  });

  next();

}

// ...

app.set('port', 8080);

When I run my server everything else works, but my dgram parts don't even say that they're listening. 当我运行服务器时,其他所有东西都可以工作,但是我的dgram部分甚至都没有说他们正在监听。 I'm not too familiar with Node and even less so with UDP, so I might be on the complete wrong track. 我对Node不太熟悉,对UDP不太了解,所以我可能走错了路。 Has anyone been able to integrate UDP messaging with an Express server? 有没有人能够将UDP消息传递与Express服务器集成?

Looks like I made some unfortunate assumptions about the usage of ports. 看起来我对端口的使用做了一些不幸的假设。 It turns out this can be done quite simply, but you have to listen on two different ports: 事实证明,可以很简单地完成此操作,但是您必须在两个不同的端口上进行监听:

const express = require('express');
const dgram = require('dgram');

// ...

const app = express();

// ... filter stack ...

const socket = dgram.createSocket('udp4');

socket.on('listening', () => {
  let addr = socket.address();
  console.log(`Listening for UDP packets at ${addr.address}:${addr.port}`);
});

socket.on('error', (err) => {
  console.error(`UDP error: ${err.stack}`);
});

socket.on('message', (msg, rinfo) => {
  console.log('Recieved UDP message');
});

app.set('port', 8080); // listen for TCP with Express
socket.bind(8082);     // listen for UDP with dgram

Works like a charm. 奇迹般有效。

Where do you bind the socket to a port? 您在哪里将套接字绑定到端口? Docs: https://nodejs.org/api/dgram.html#dgram_socket_bind_port_address_callback 文件: https//nodejs.org/api/dgram.html#dgram_socket_bind_port_address_callback

Try calling dgramSocket.bind(PORT) at the bottom of the code and see if triggers your console log. 尝试在代码底部调用dgramSocket.bind(PORT) ,看看是否触发了控制台日志。 You will (probably) have to move the socket code outside of the app.use() function. 您(可能)必须将套接字代码移到app.use()函数之外。

If you're just going to be sending messages from the socket, then I don't think you need to bind it to a port, but if you're listening you need to tell it where to listen at. 如果您只是要从套接字发送消息,那么我认为您不需要将其绑定到端口,但是如果您正在监听,则需要告诉它在哪里监听。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM