简体   繁体   English

如何在Node.js的同一进程中侦听同一地址上的不同UDP端口

[英]How to listen to different UDP ports on the same address within the same process in Node.js

I'm writing a Node.js app to control a small drone. 我正在编写一个Node.js应用程序来控制小型无人机。 Here are the instructions from the SDK: 以下是SDK中的说明:


Use Wi-Fi to establish a connection between the Tello and PC, Mac, or mobile device. 使用Wi-Fi在Tello与PC,Mac或移动设备之间建立连接。

Send Command & Receive Response 发送命令并接收响应

Tello IP: 192.168.10.1 UDP PORT: 8889 <<-->> PC/Mac/Mobile Tello IP:192.168.10.1 UDP端口:8889 <<->> PC / Mac / Mobile

Step 1: Set up a UDP client on the PC, Mac, or mobile device to send and receive message from the Tello via the same port. 步骤1:在PC,Mac或移动设备上设置UDP客户端,以通过同一端口从Tello发送和接收消息。

Step 2: Before sending any other commands, send 'command' to the Tello via UDP PORT 8889 to initiate SDK mode. 步骤2:在发送任何其他命令之前,通过UDP PORT 8889将“命令”发送到Tello以启动SDK模式。

Receive Tello State 接收泰洛州

Tello IP: 192.168.10.1 -->> PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 8890 Tello IP:192.168.10.1->> PC / Mac /移动UDP服务器:0.0.0.0 UDP端口:8890

Step 3: Set up a UDP server on the PC, Mac, or mobile device and check the message from IP 0.0.0.0 via UDP PORT 8890. Steps 1 and 2 must be completed before attempting step 3. 步骤3:在PC,Mac或移动设备上设置UDP服务器,并通过UDP PORT 8890检查来自IP 0.0.0.0的消息。在尝试步骤3之前,必须完成步骤1和2。

Receive Tello Video Stream 接收Tello视频流

Tello IP: 192.168.10.1 -->> PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 11111 Tello IP:192.168.10.1->> PC / Mac /移动UDP服务器:0.0.0.0 UDP端口:11111

Step 4: Set up a UDP server on the PC, Mac, or mobile device and check the message from IP 0.0.0.0 via UDP PORT 11111. 步骤4:在PC,Mac或移动设备上设置UDP服务器,并通过UDP PORT 11111检查IP 0.0.0.0中的消息。

Step 5: Send 'streamon' to the Tello via UDP PORT 8889 to start streaming. 步骤5:通过UDP PORT 8889将“ streamon”发送到Tello以开始流式传输。 Steps 1 and 2 must be completed before attempting step 5. 在尝试步骤5之前,必须完成步骤1和2。


The Command & Receive part works like a charm and I am sending and receiving datagrams to/from the drone on port 8889. My problem is that it looks like I'm not receiving any State or Video Stream messages on the other ports and I'm pretty sure that it's not a problem with the drone but something I'm not setting up properly with Node. 命令和接收部分的工作原理就像一个超级按钮,我正在端口8889上与无人机之间收发数据报。我的问题是,看起来我在其他端口上没有收到任何状态或视频流消息,我我很确定这不是无人机问题,但我没有使用Node正确设置某些问题。 Can anyone see where the problem is in my implementation. 谁能看到问题在我的实现中。 Here is my code: 这是我的代码:

tello.ts tello.ts

import dgram from 'dgram';

export class Tello {
  private LOCAL_IP_ = '0.0.0.0';
  private TELLO_IP_ = '192.168.10.1';

  private COMMAND_PORT_ = 8889;
  private commandSocket_ = dgram.createSocket('udp4');

  private STATE_PORT_ = 8890;
  private stateSocket_ = dgram.createSocket('udp4');

  private VIDEO_PORT_ = 11111;
  private videoSocket_ = dgram.createSocket('udp4');

  constructor() {}

  startCommandSocket() {
    this.commandSocket_.addListener('message', (msg, rinfo) => {
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.commandSocket_.bind(this.COMMAND_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the command socket');
    });
  }

  startStateSocket() {
    this.stateSocket_.addListener('message', (msg, rinfo) => {
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.stateSocket_.bind(this.STATE_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the state socket');
    });
  }

  startVideoSocket() {
    this.videoSocket_.addListener('message', (msg, rinfo) => {
      console.log('receiving video');      
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.videoSocket_.bind(this.VIDEO_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the video socket');
    });
  }

  private sendCommand_(command: string) {
    // As this is sent over UDP and we have no guarantee that the packet is received or a response given
    // we are sending the command 5 times in a row to add robustess and resiliency.
    //for (let i = 0; i < 5; i++) {
    this.commandSocket_.send(command, this.COMMAND_PORT_, this.TELLO_IP_);
    //}
    console.log(`sending command: ${command} to ${this.TELLO_IP_}`);
  }

  /**
   * Enter SDK mode.
   */
  command() {
    this.sendCommand_('command');
  }

  /**
   * Auto takeoff.
   */
  takeoff() {
    this.sendCommand_('takeoff');
  }

  /**
   * Auto landing.
   */
  land() {
    this.sendCommand_('land');
  }

  streamVideoOn() {
    this.sendCommand_('streamon');
  }

  streamVideoOff() {
    this.sendCommand_('streamoff');
  }

  ...

}

index.ts index.ts

import { waitForSeconds } from './utils';
import { Tello } from './tello'

const main = async () => {
  const tello = new Tello();

  tello.startCommandSocket();
  await waitForSeconds(1);
  tello.command();
  await waitForSeconds(1);  
  tello.startStateSocket();
  await waitForSeconds(1);
  tello.startVideoSocket();
  await waitForSeconds(1);
  tello.streamVideoOn();
  await waitForSeconds(1);

  tello.takeoff();
  await waitForSeconds(10);
  tello.land(); 
};

main();

Here is the sample code that receives and decodes the h264 video stream provided by Tello SDK team, using the "streamon" command. 这是使用“ streamon”命令接收和解码Tello SDK团队提供的h264视频流的示例代码。 https://github.com/dji-sdk/Tello-Python .​​ Please refer to doc/reademe.pdf and the source code under the path of h264 decoder for the specific processing method of the received video stream data. https://github.com/dji-sdk/Tello-Python请参考DOC / reademe.pdf和源代码H264解码器的路径下对所接收的视频流数据的具体处理方法。

Before you run the sample code, you should install some dependencies by using the install script. 在运行示例代码之前,应该使用安装脚本安装一些依赖项。

Did you open your firewall to accept UDP ports 8890 / 11111 ? 您是否打开防火墙接受UDP端口8890/11111?

Open port 8890/udp and 11111/udp in your laptop firewall to receive Tello telemetry data. 在笔记本电脑防火墙中打开端口8890 / udp和11111 / udp,以接收Tello遥测数据。

On Linux 在Linux上

$ sudo firewall-cmd --permanent --add-port=8890/udp $ sudo firewall-cmd --permanent --add-port = 8890 / udp

$ sudo firewall-cmd --permanent --add-port=11111/udp $ sudo firewall-cmd-永久--add-port = 11111 / udp

On Mac, use System Preferences to open the ports. 在Mac上,使用“系统偏好设置”打开端口。

Open System Preferences > Security & Privacy > Firewall > Firewall Options
Click the + / Add button
Choose 'node' application from the Applications folder and click Add.
Ensure that the option next to the application is set to Allow incoming connections.
Click OK.

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

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