titles 说明了一切,我无法让它工作,我使用参数“chrome.exe --remote-debugging-port=9999”启动,并尝试通过 c++ 中的 boost asio 发送 websocket 请求,每当我尝试访问时localhost:9999/devtools 它还出现“localhost 无法加载此页面”。
测试代码是使用AI生成的:
#include <iostream>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
typedef websocketpp::client<websocketpp::config::asio_client> client;
client c; // Global client instance
void on_open(websocketpp::connection_hdl hdl) {
std::cout << "Connection opened!" << std::endl;
// Prepare JSON payload for navigation command
json payload = {
{"id", 1},
{"method", "Page.navigate"},
{"params", {{"url", "http://example.com"}}}
};
// Convert JSON payload to string
std::string message = payload.dump();
// Send the command
c.send(hdl, message, websocketpp::frame::opcode::text);
}
void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
std::cout << "Received message: " << msg->get_payload() << std::endl;
}
void on_close(websocketpp::connection_hdl hdl) {
std::cout << "Connection closed!" << std::endl;
}
int main() {
// Initialize ASIO
c.init_asio();
// Set handlers
c.set_open_handler(&on_open);
c.set_message_handler(&on_message);
c.set_close_handler(&on_close);
// Get the correct WebSocket URL from http://localhost:9999/json
std::string uri = "ws://127.0.0.1:9999/json"; // Replace with actual WebSocket URL from JSON response
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "Could not create connection because: " << ec.message() << std::endl;
return 0;
}
// Connect and start the ASIO io_service run loop
c.connect(con);
try {
c.run();
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
其他错误:
[2024-11-20 07:45:07] [连接] 连接成功 [2024-11-20 07:45:07] [错误] 服务器握手响应错误:websocketpp.processor:20(无效的 HTTP 状态。) [2024-11-20 07:45:07] [失败] WebSocket 连接 127.0.0.1:6969 - "WebSocket++/0.8.2" /json 404 websocketpp.processor:20 无效的 HTTP 状态。