Class WebSocketClient

Synopsis

#include <lib/inc/drogon/WebSocketClient.h>

class WebSocketClient

Description

WebSocket client abstract class.

Methods

~WebSocketClient
connectToServerConnect to the server.
getConnectionGet the WebSocket connection that is typically used to send messages.
getLoopGet the event loop of the client;.
newWebSocketClientCreate a websocket client using the given ip and port to connect to server.
setConnectionClosedHandlerSet the connection closing handler. When the websocket connection is closed, the callback is called.
setMessageHandlerSet messages handler. When a message is recieved from the server, the callback is called.

Source

Lines 37-121 in lib/inc/drogon/WebSocketClient.h. Line 38 in lib/inc/drogon/WebSocketClient.h.

class WebSocketClient
{
  public:
    /// Get the WebSocket connection that is typically used to send messages.
    virtual WebSocketConnectionPtr getConnection() = 0;

    /**
     * @brief Set messages handler. When a message is recieved from the server,
     * the callback is called.
     *
     * @param callback The function to call when a message is received.
     */
    virtual void setMessageHandler(
        const std::function<void(std::string &&message,
                                 const WebSocketClientPtr &,
                                 const WebSocketMessageType &)> &callback) = 0;

    /// Set the connection closing handler. When the connection is established
    /// or closed, the @param callback is called with a bool parameter.

    /**
     * @brief Set the connection closing handler. When the websocket connection
     * is closed, the  callback is called
     *
     * @param callback The function to call when the connection is closed.
     */
    virtual void setConnectionClosedHandler(
        const std::function<void(const WebSocketClientPtr &)> &callback) = 0;

    /// Connect to the server.
    virtual void connectToServer(const HttpRequestPtr &request,
                                 const WebSocketRequestCallback &callback) = 0;

    /// Get the event loop of the client;
    virtual trantor::EventLoop *getLoop() = 0;

    /**
     * @brief Create a websocket client using the given ip and port to connect
     * to server.
     *
     * @param ip The ip address of the server.
     * @param port The port of the server.
     * @param useSSL If useSSL is set to true, the client connects to the server
     * using SSL.
     * @param loop If the loop parameter is set to nullptr, the client uses the
     * HttpAppFramework's event loop, otherwise it runs in the loop identified
     * by the parameter.
     * @return WebSocketClientPtr The smart pointer to the WebSocket client.
     * @note The ip parameter support for both ipv4 and ipv6 address
     */
    static WebSocketClientPtr newWebSocketClient(
        const std::string &ip,
        uint16_t port,
        bool useSSL = false,
        trantor::EventLoop *loop = nullptr);

    /// Create a websocket client using the given hostString to connect to
    /// server
    /**
     * @param hostString must be prefixed by 'ws://' or 'wss://'
     * Examples for hostString:
     * @code
       wss://www.google.com
       ws://www.google.com
       wss://127.0.0.1:8080/
       ws://127.0.0.1
       @endcode
     * @param loop if the parameter is set to nullptr, the client uses the
     * HttpAppFramework's main event loop, otherwise it runs in the loop
     * identified by the parameter.
     *
     * @note
     * Don't add path and parameters in hostString, the request path and
     * parameters should be set in HttpRequestPtr when calling the
     * connectToServer() method.
     *
     */
    static WebSocketClientPtr newWebSocketClient(
        const std::string &hostString,
        trantor::EventLoop *loop = nullptr);

    virtual ~WebSocketClient()
    {
    }
};





Add Discussion as Guest

Log in to DocsForge