uWebSockets 有一个使用 ClientApp.h 的示例 client.cpp。然而,该示例目前已损坏,通过查看 ClientApp.h 可以清楚地了解原因。
作为参考,这是他们的 GitHub:https://github.com/uNetworking/uWebSockets/
我不清楚如何继续,因为他们的文档非常有限。
我对他们的 Client.cpp 的尝试
int main() {
uWS::ClientApp app({
.open = [](/*auto *ws*/) {
std::cout << "Hello and welcome to client" << std::endl;
},
.message = [](/*auto *ws, auto message*/) {
},
.close = [](/*auto *ws*/) {
std::cout << "bye" << std::endl;
}
});
app.connect("wss://stream.thisisatestaddress.com");
app.run();
}
它构建了,但它被破坏了,所以什么也没有发生。
我唯一需要的就是接收websocketstream;不需要其他任何东西。
所以我尝试了以下不同的方法:
int main() {
struct PerSocketData {
/* Fill with user data */
};
uWS::App().ws<PerSocketData>("wss://dummyurl.com", {
.open = [](auto* ws) {
std::cout << "Connected to WebSocket server" << std::endl;
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
std::cout << "Received message: " << message << std::endl;
// Process the received message as needed
}
}).run();
return 0;
}
我尝试过:
int main() {
struct PerSocketData {
/* Fill with user data */
};
uWS::App().ws<PerSocketData>("/*", {
/* Settings */
.compression = uWS::SHARED_COMPRESSOR,
.maxPayloadLength = 16 * 1024 * 1024,
.idleTimeout = 240,
/* Handlers */
.open = [](auto* ws) {
std::cout << "open" << std::endl; // does not print so it doesnt run
ws->subscribe("wss://dunnyurl.com");
// Perform WebSocket handshake
ws->send("WebSocket handshake message", uWS::OpCode::TEXT);
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
// Print out the received message
std::cout << "Received message: " << message << std::endl;
}
}).run();
return 0;
}
在这两种情况下,“Handlers”部分根本不执行,但也没有给出错误。
正确的做法是什么?
其实我发现ClientApp还没有实现。