如何通过uWS访问HTTP请求的IP地址?



我正在使用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

根据文档,远程地址是响应的一个属性。因此:

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

最新更新