Re: Question about getting more info when fail to connect using websocketpp

865 views
Skip to first unread message

Peter Thorson

unread,
Apr 1, 2014, 9:46:00 PM4/1/14
to Nguyen, Dat, webso...@googlegroups.com
In the on_fail handler (or the on_close handler) you can use connection::get_ec() to get a machine readable error code for why the connection was closed or why it failed.

Note, this is only able to resolve errors that the library itself understands. Because of the separation of concerns between the library and its underlying transport policies there are some transport errors that can’t be determined using this method. In these cases get_ec() will return a somewhat more generic code indicating the rough area (websocketpp::transport::tls_error or websocketpp::transport::pass_through). The info log channel should contain a more detailed error provided by that component.

Presently the bundled transport policies don’t offer an interface to retrieve a machine readable error from those levels. If you need a machine readable error at that level let me know and I can look into adding an interface to grab that as well.

In the case of a close rather than a fail you can also use connection::get_remote_close_code and connection::get_local_close_code to get the codes sent in the close handshake.

On Apr 1, 2014, at 12:51, Nguyen, Dat <nguy...@amazon.com> wrote:

Ping again, in case Peter forgot about my question (sent on Friday, might not be a good time).

Add websocketpp group, if anyone have an answer for this. I appreciate your help.

Dat

From: <Nguyen>, "Nguyen, Dat" <nguy...@amazon.com>
Date: Friday, March 28, 2014 at 1:17 PM
To: "pe...@zaphoyd.com" <pe...@zaphoyd.com>
Subject: Question about getting more info when fail to connect using websocketpp

Hi Peter,

I have a question about error handling when on_fail() is called. Currently, I have code that basically follow your example. The question is highlighted below.

I do see this kind of messages in the log when errors happen, but can’t figure out a way to get the error myself.

[2014-03-28 10:51:12] [disconnect] Failed: Timer Expired
[2014-03-28 12:36:06] [disconnect] Failed: Invalid HTTP status.
[2014-03-28 12:36:57] [disconnect] Disconnect close local:[1006,A transport action was requested after shutdown] remote:[4001,before - Could not find any vali]

Thanks for your help. I appreciate it.

Best,
Dat

// .h
class WebSocketWrapper {
public:
    // ...
 
    /** Some short names for websocketpp types */
    typedef websocketpp::client<websocketpp::config::asio_tls_client> WebSocketPPClient;
    typedef websocketpp::config::asio_tls_client::message_type::ptr WebSocketPPMessagePtr;
    typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> WebSocketPPContextPtr;
    typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> WebSocketPPScopedLock;
    typedef websocketpp::connection_hdl WebSocketPPConnectionHandler;
    typedef websocketpp::lib::error_code WebSocketPPErrorCode;
 
private:
    // ...
}

// .cpp

    void WebSocketWrapper::init() {
        // Set up access channels to log everything.
        mpWebSocketClient->set_access_channels(websocketpp::log::alevel::all);
        
        // Initialize the Asio transport policy.
        mpWebSocketClient->init_asio();
        
        // Bind the handlers we are using.
        using websocketpp::lib::placeholders::_1;
        using websocketpp::lib::placeholders::_2;
        using websocketpp::lib::bind;
        mpWebSocketClient->set_tls_init_handler(bind(&WebSocketWrapper::on_tls_init,this,::_1));
        mpWebSocketClient->set_message_handler(bind(&WebSocketWrapper::on_message,this,::_1,::_2));
        mpWebSocketClient->set_open_handler(bind(&WebSocketWrapper::on_open,this,::_1));
        mpWebSocketClient->set_close_handler(bind(&WebSocketWrapper::on_close,this,::_1));
        mpWebSocketClient->set_fail_handler(bind(&WebSocketWrapper::on_fail,this,::_1));
    }
    void WebSocketWrapper::on_fail(WebSocketPPConnectionHandler connectionHandler) {
        // How can I get more information about the failure here?
// E.g the cause of failure, the error code, etc.
    }

Peter Thorson

unread,
Apr 2, 2014, 12:04:24 PM4/2/14
to Nguyen, Dat, Johannsen, Svend, webso...@googlegroups.com

On Apr 1, 2014, at 21:28 , Nguyen, Dat <nguy...@amazon.com> wrote:

Hi Peter,

Thanks a lot for your help. I’m able to get the error code and the remote close code now (yeah!). Two more questions:
- In the case of on_fail, the errorCode will always be present, right?
- In the case of on_close, the errorCode might or might not be present, but the remote close code will always be present, right?

get_ec() will always return a valid lib::error_code. In cases where there has been no “error” it will simply be a value that evaluates to false / 0, indicating “success”. I don’t believe there are any cases where the connection “fails” and the ec is set to success. If there are any they are bugs.

Remote and local close codes will always be present but may contain some special values like 1005/no code to indicate that the code field was blank in the closing handshake or 1006/abnormal close to indicate that the close handshake wasn’t performed properly.

Peter Thorson

unread,
Apr 2, 2014, 1:05:26 PM4/2/14
to Nguyen, Dat, Johannsen, Svend, webso...@googlegroups.com
WebSocket close codes are defined by RFC6455 which set up an IANA registry for certain ranges. Other ranges (4000-4999) are reserved for private/application use and have no predefined codes.

The IANA registry can be found here:


WebSocket++ implements all of the IANA registered codes as named constants in the websocketpp::close::status namespace as documented here:

For the library specific error codes (things of type websocketpp::lib::error_code), the C++11 <system_error> category/error model is used. Every major component has an error namespace that defines an error category and an associated values list. Core library errors are found in the websocketpp::error namespace and error documentation for other components can be found in the respective error namespace within each. If you look at the namespace list (http://doxygen.websocketpp.org/namespaces.html) and any namespace there called “error” will have the lists of error codes related to that component.

On Apr 2, 2014, at 11:37 , Nguyen, Dat <nguy...@amazon.com> wrote:

Hi Peter,

This info is very helpful. One more question: is there a list of known error codes (such as the 1005/1006 code) that we can get?

Best,
Dat
Reply all
Reply to author
Forward
0 new messages