Chrome --remote-debugging-port 远程调试功能失效,WebSocket连接报错404
Chrome --remote-debugging-port 远程调试功能失效,WebSocket连接报错404
我明白你现在碰到的麻烦——Chrome远程调试连不上,WebSocket还报404,确实挺闹心的。咱们一步步拆解问题,把它搞定:
一、先确认Chrome远程调试的基础环境是否正常
你启动Chrome的命令chrome.exe --remote-debugging-port=9999本身是对的,但有个容易踩的坑:如果已经有Chrome进程在运行,新启动的实例会共享现有进程,远程调试端口可能不会生效。
解决办法:
- 先关闭所有Chrome窗口(包括后台的进程,可以用任务管理器确认)
- 用带独立用户数据目录的命令启动,强制新建一个独立会话:
chrome.exe --remote-debugging-port=9999 --user-data-dir="C:\temp\chrome_dev_session" - 启动后,打开浏览器访问
http://localhost:9999/json:如果能返回一段JSON数组,说明远程调试已经正常启动;如果还是打不开,检查Chrome进程是否真的占用了9999端口(可以用netstat -ano | findstr :9999查看)。
另外,你说访问localhost:9999/devtools打不开是正常的,正确的调试前端页面需要从/json返回的devtoolsFrontendUrl字段获取,直接访问/devtools本来就不存在。
二、你的WebSocket代码的核心错误
看你的代码和错误日志,问题出在WebSocket连接地址用错了:
你代码里写的uri = "ws://127.0.0.1:9999/json",但/json是HTTP接口(用来获取调试目标列表),根本不是WebSocket端点,服务器自然返回404。
正确的流程应该是:
- 先通过HTTP请求访问
http://localhost:9999/json,拿到所有调试目标的JSON数据 - 从JSON数组里找到你要调试的页面(比如第一个元素),提取
webSocketDebuggerUrl字段,这个才是真正的WebSocket调试地址,格式类似ws://localhost:9999/devtools/page/abc123xyz
三、修正后的代码示例
我给你改了下代码,添加了一个获取正确WebSocket地址的函数,你可以参考:
#include <iostream> #include <websocketpp/config/asio_no_tls_client.hpp> #include <websocketpp/client.hpp> #include <nlohmann/json.hpp> #include <boost/asio.hpp> #include <sstream> using json = nlohmann::json; typedef websocketpp::client<websocketpp::config::asio_client> client; client c; // Global client instance // 新增:获取正确的WebSocket调试地址 std::string get_websocket_debug_url() { boost::asio::io_service io_service; boost::asio::ip::tcp::resolver resolver(io_service); boost::asio::ip::tcp::socket socket(io_service); // 连接到Chrome远程调试的HTTP端口 auto endpoints = resolver.resolve("localhost", "9999"); boost::asio::connect(socket, endpoints); // 发送HTTP GET请求获取调试目标列表 std::string request = "GET /json HTTP/1.1\r\nHost: localhost:9999\r\nConnection: close\r\n\r\n"; boost::asio::write(socket, boost::asio::buffer(request)); // 读取响应(先读响应头,再读JSON数据) boost::asio::streambuf response_buf; boost::asio::read_until(socket, response_buf, "\r\n\r\n"); std::istream response_stream(&response_buf); std::string header_line; // 跳过所有响应头行 while (std::getline(response_stream, header_line) && header_line != "\r"); // 读取JSON内容 std::string json_content; std::string line; while (std::getline(response_stream, line)) { json_content += line; } // 解析JSON,返回第一个调试目标的WebSocket地址 json debug_targets = json::parse(json_content); if (!debug_targets.empty()) { return debug_targets[0]["webSocketDebuggerUrl"]; } return ""; } 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() { // 先获取正确的WebSocket调试地址 std::string ws_url = get_websocket_debug_url(); if (ws_url.empty()) { std::cout << "Error: Failed to get WebSocket debugger URL. Check if Chrome remote debugging is running correctly." << std::endl; return 1; } std::cout << "Got WebSocket URL: " << ws_url << std::endl; // 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); websocketpp::lib::error_code ec; client::connection_ptr con = c.get_connection(ws_url, 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; }
四、额外注意点
看你的错误日志里显示WebSocket Connection 127.0.0.1:6969,但你启动Chrome用的是9999端口,这说明你测试时可能代码里的端口写错了?一定要确保代码里的端口和Chrome启动的--remote-debugging-port参数完全一致。
备注:内容来源于stack exchange,提问作者drmarble
相关产品推荐
相关产品推荐

