oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
78 stars 32 forks source link

Allows a client to retry connecting when oatpp::network::tcp::client::ConnectionProvider::getConnection() throws "Can't connect" error #46

Closed Saadharta closed 8 months ago

Saadharta commented 9 months ago

Hello there :)

Current configuration: Oatpp 1.3.0 MSVC15 v140

Issue: One of my websocket client using the following environment and being too eager to work throws an exception because it attempt to open the connection before the server is initialized:

class WsClientComponent
{
    public:
        OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper)
            ([] { return oatpp::parser::json::mapping::ObjectMapper::createShared(); }());

        OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, wsClientConnectionProvider)("cltCP", []
            {
                std::string address;
                std::cout << "Target address: ";
                std::cin >> address;
                v_uint16 port;
                std::cout << "Target port: ";
                std::cin >> port;
                return oatpp::network::tcp::client::ConnectionProvider::createShared({ address, port, oatpp::network::Address::IP_4 });
            }());

        OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::websocket::Connector>, wsClientConnector)([]
            {
                OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, wsClientConnectionProvider, "cltCP");
// One solution might be to allow a RequestExecutor to be also be provided in the Connector constructor?
                return oatpp::websocket::Connector::createShared(wsClientConnectionProvider);
            }());

        OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::websocket::WebSocket>, wsClientSocket)("cltWs",[]
            {
                OATPP_COMPONENT(std::shared_ptr<oatpp::websocket::Connector>, wsClientConnector);
                std::cout << "Target address: ";
                std::string url;
                std::cin >> url;
// On powerup the server might take some time to be available therefore the client need to retry connecting again a bit later...
// Currently the connect() chokes on the first get() instruction, throwing a "can't connect" error due to the server not being ready yet... 
                auto ioConnection = wsClientConnector->connect(url); 
                return oatpp::websocket::WebSocket::createShared(ioConnection, true );
            }());
};

After browsing the API documentation , i was left to wonder if there was a way to provide a RequestExecutor carrying a RetryPolicy to a websocket client Connector component? Any other workaround is welcome.

Kind regards,

Saadharta