【问题标题】:How to access IP address of HTTP requests using uWS?如何使用 uWS 访问 HTTP 请求的 IP 地址?
【发布时间】:2021-10-25 08:17:20
【问题描述】:

我正在使用 uWebSockets 做一个项目。我需要做的是从传入的 HTTP 请求中获取发件人 IP 地址。在文档中我可以看到 IP 地址可以从 WebSockets 中取出。有人知道将 uWS 转换为 WebSockets 以获取数据还是有其他方法来获取数据?

#include <iostream>

#include "App.h"

int main() {

  /* Overly simple hello world app */

  uWS::App().get("/*", [](auto *res, auto *req) {

// **this is the place i need to access the ip address of the incoming HttpRequest**

    res->end("Hello world!");
  }).listen(3000, [](auto *listen_socket) {

    if (listen_socket) {
      std::cout << "Listening on port " << 3000 << std::endl;
    }
  }).

run();
  std::cout << "Failed to listen on port 3000" << std::endl;
} 

图书馆链接:https://unetworking.github.io/uWebSockets.js/generated/interfaces/WebSocketBehavior.html

【问题讨论】:

  • 这个库是用c++写的,我也在用c++。(我是个初学者)
  • 请使用您现在拥有的应用程序的代表性示例编辑您的问题,并指出您需要发件人 IP 的位置。

标签: c++ api websocket uwebsockets


【解决方案1】:

根据the documentation,远程地址是响应的一个属性。尔格:

std::string_view remote_ip = res->getRemoteAddressAsText();

【讨论】:

  • 将问题标记为已接受将有助于其他人在未来找到该主题。
猜你喜欢
  • 2023-03-03
  • 2014-10-19
  • 2022-11-28
  • 2015-08-31
  • 2010-11-13
  • 2014-04-24
  • 2012-04-23
  • 2014-11-21
相关资源
最近更新 更多