versatica / libmediasoupclient

mediasoup client side C++ library
https://mediasoup.org
ISC License
286 stars 177 forks source link

Server handshake response error: websocketpp.processor:20 #108

Closed dongdan002 closed 3 years ago

dongdan002 commented 3 years ago

when i use websocketpp to connect to local host or mediasoup demo, the client connect failed with below err.

The websocketpp client failed:

[2020-12-24 15:57:53] [connect] Successful connection [2020-12-24 15:57:53] [error] Server handshake response error: websocketpp.processor:20 (Invalid HTTP status.) [2020-12-24 15:57:53] [fail] WebSocket Connection 192.168.1.41:4443 - "WebSocket++/0.8.2" /?roomId=wuodujbw&peerId=ytev7jq1 403 websocketpp.processor:20 Invalid HTTP status. Fail handler 3 1006 Invalid HTTP status. 1006

websocketpp.processor:20 - Invalid HTTP status.

and the mediasoup server logs:

protoo-server:WARN:WebSocketServer _onRequest() | invalid/missing Sec-WebSocket-Protocol

the websocketpp client:

define ASIO_STANDALONE

include <websocketpp/config/asio_client.hpp>

include <websocketpp/client.hpp>

include

include

typedef websocketpp::client client; using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind;

// pull out the type of messages sent by our config typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr; typedef websocketpp::lib::shared_ptr context_ptr; typedef client::connection_ptr connection_ptr;

const char* request = "{\"request\":true,\"id\":6575451,\"method\":\"getRouterRtpCapabilities\",\"data\":{}}";

class perftest { public: typedef perftest type; typedef std::chrono::duration<int,std::micro> dur_type;

perftest () {
    m_endpoint.set_access_channels(websocketpp::log::alevel::all);
    m_endpoint.set_error_channels(websocketpp::log::elevel::all);

    // Initialize ASIO
    m_endpoint.init_asio();

    // Register our handlers
    m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));
    m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
    m_endpoint.set_message_handler(bind(&type::on_message,this,::_1,::_2));
    m_endpoint.set_open_handler(bind(&type::on_open,this,::_1));
    m_endpoint.set_close_handler(bind(&type::on_close,this,::_1));
    m_endpoint.set_fail_handler(bind(&type::on_fail,this,::_1));
}

void start(std::string uri) {
    websocketpp::lib::error_code ec;
    client::connection_ptr con = m_endpoint.get_connection(uri, ec);
    con->append_header("Sec-WebSocket-Key", "xeg5tz2nFXCO3saFet6qDA==");
    if (ec) {
        m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
        return;
    }

    //con->set_proxy("http://humupdates.uchicago.edu:8443");

    m_endpoint.connect(con);

    m_start = std::chrono::high_resolution_clock::now();
    m_endpoint.run();

}

void on_socket_init(websocketpp::connection_hdl) {
    m_socket_init = std::chrono::high_resolution_clock::now();
}

context_ptr on_tls_init(websocketpp::connection_hdl) {
    m_tls_init = std::chrono::high_resolution_clock::now();
    context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::tlsv1);

    try {
        ctx->set_options(asio::ssl::context::default_workarounds |
                         asio::ssl::context::no_sslv2 |
                         asio::ssl::context::no_sslv3 |
                         asio::ssl::context::single_dh_use);
       // ctx->set_verify_mode(asio::ssl::verify_none);
    } catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }

    return ctx;
}

void on_fail(websocketpp::connection_hdl hdl) {
    client::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);

    std::cout << "Fail handler" << std::endl;
    std::cout << con->get_state() << std::endl;
    std::cout << con->get_local_close_code() << std::endl;
    std::cout << con->get_local_close_reason() << std::endl;
    std::cout << con->get_remote_close_code() << std::endl;
    std::cout << con->get_remote_close_reason() << std::endl;
    std::cout << con->get_ec() << " - " << con->get_ec().message() << std::endl;
}

void on_open(websocketpp::connection_hdl hdl) {
    m_open = std::chrono::high_resolution_clock::now();
    m_endpoint.send(hdl, request, websocketpp::frame::opcode::text);
}
void on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
    m_message = std::chrono::high_resolution_clock::now();

    std::string head = msg->get_header();
    std::string playLoad = msg->get_raw_payload();
    std::cout << "header: " << head << std::endl;
    std::cout << "play load: " << playLoad << std::endl;
    websocketpp::lib::error_code ec;
    m_endpoint.send(hdl, msg->get_payload(), msg->get_opcode(), ec);
    if (ec) {
        std::cout << "Echo failed because: " << ec.message() << std::endl;
    }
    //m_endpoint.close(hdl,websocketpp::close::status::going_away,"");
}
void on_close(websocketpp::connection_hdl) {
    m_close = std::chrono::high_resolution_clock::now();

    std::cout << "Socket Init: " << std::chrono::duration_cast<dur_type>(m_socket_init-m_start).count() << std::endl;
    std::cout << "TLS Init: " << std::chrono::duration_cast<dur_type>(m_tls_init-m_start).count() << std::endl;
    std::cout << "Open: " << std::chrono::duration_cast<dur_type>(m_open-m_start).count() << std::endl;
    std::cout << "Message: " << std::chrono::duration_cast<dur_type>(m_message-m_start).count() << std::endl;
    std::cout << "Close: " << std::chrono::duration_cast<dur_type>(m_close-m_start).count() << std::endl;
}

private: client m_endpoint; std::thread t1; std::chrono::high_resolution_clock::time_point m_start; std::chrono::high_resolution_clock::time_point m_socket_init; std::chrono::high_resolution_clock::time_point m_tls_init; std::chrono::high_resolution_clock::time_point m_open; std::chrono::high_resolution_clock::time_point m_message; std::chrono::high_resolution_clock::time_point m_close; };

int main(int argc, char* argv[]) { std::string uri = "wss://v3demo.mediasoup.org:4443/?roomId=b3pcftkc&peerId=3ck2s9kh";//"wss://webrtc.yuntongxun.net:4443 -->this is ok";

perftest endpoint;
try {

    endpoint.start(uri);
} catch (websocketpp::exception const & e) {
    std::cout << e.what() << std::endl;
} catch (std::exception const & e) {
    std::cout << e.what() << std::endl;
} catch (...) {
    std::cout << "other exception" << std::endl;
}

}

ibc commented 3 years ago

Use the public mediasoup forum for questions.