如何在 Node.js 中的同一进程内侦听同一地址上的不同 UDP 端口 [英] How to listen to different UDP ports on the same address within the same process in Node.js

查看:29
本文介绍了如何在 Node.js 中的同一进程内侦听同一地址上的不同 UDP 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Node.js 应用程序来控制一架小型无人机.以下是来自 SDK 的说明:

I'm writing a Node.js app to control a small drone. Here are the instructions from the SDK:

使用 Wi-Fi 在 Tello 和 PC、Mac 或移动设备之间建立连接.

Use Wi-Fi to establish a connection between the Tello and PC, Mac, or mobile device.

Tello IP:192.168.10.1 UDP 端口:8889 <<-->PC/Mac/手机

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

步骤 1:在 PC、Mac 或移动设备上设置 UDP 客户端,以通过同一端口从 Tello 发送和接收消息.

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.

第 2 步:在发送任何其他命令之前,通过 UDP 端口 8889 向 Tello 发送命令"以启动 SDK 模式.

Step 2: Before sending any other commands, send 'command' to the Tello via UDP PORT 8889 to initiate SDK mode.

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

Tello IP: 192.168.10.1 -->> PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 8890

第 3 步:在 PC、Mac 或移动设备上设置 UDP 服务器,并通过 UDP PORT 8890 检查来自 IP 0.0.0.0 的消息.在尝试第 3 步之前必须完成第 1 步和第 2 步.

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.

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

Tello IP: 192.168.10.1 -->> PC/Mac/Mobile UDP Server: 0.0.0.0 UDP PORT: 11111

第 4 步:在 PC、Mac 或移动设备上设置 UDP 服务器,并通过 UDP PORT 11111 检查来自 IP 0.0.0.0 的消息.

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.

第 5 步:通过 UDP 端口 8889 向 Tello 发送streamon"以开始流式传输.必须先完成第 1 步和第 2 步,然后才能尝试第 5 步.

Step 5: Send 'streamon' to the Tello via UDP PORT 8889 to start streaming. Steps 1 and 2 must be completed before attempting step 5.

命令&接收部分像魅力一样工作,我正在端口 8889 上向/从无人机发送和接收数据报.我的问题是看起来我在其他端口上没有收到任何状态或视频流消息,我很确定这不是无人机的问题,而是我没有用 Node.js 正确设置的问题.任何人都可以看到我的实现中的问题在哪里.这是我的代码:

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. Can anyone see where the problem is in my implementation. Here is my code:

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

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();

推荐答案

您是否打开了防火墙以接受 UDP 端口 8890/11111 ?

Did you open your firewall to accept UDP ports 8890 / 11111 ?

在您的笔记本电脑防火墙中打开端口 8890/udp 和 11111/udp 以接收 Tello 遥测数据.

Open port 8890/udp and 11111/udp in your laptop firewall to receive Tello telemetry data.

在 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 --permanent --add-port=11111/udp

在 Mac 上,使用系统偏好设置打开端口.

On Mac, use System Preferences to open the ports.

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.

这篇关于如何在 Node.js 中的同一进程内侦听同一地址上的不同 UDP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆