ark::comms::WebSocketClient

Defined in header “ark/comms/websocket_client.hh”.


This class represents a websocket client it can talk to a websocket server and send/receive messages. This client will handle disconnects/reconnects transparently in the background.

Methods

  • WebSocketClient(const NetworkAddress & address, const std::string & target_path)
    Constructor. Initializes a websocket client which attempts to connect to the given network address.

  • WebSocketClient(const core::Url & url)
    Constructor. Initializes a websocket client against the given URL.

  • WebSocketClient(const WebSocketClientConfig & config)
    Constructor. Initializes a websocket client against the given configuration.

  • void disconnect()
    Explicitly disconnect. Note that if you call poll() again after this, we will try to reconnect.

  • bool connected()
    Returns true if we are connected, or false otherwise.

  • bool subscribed()
    Returns true if we are subscribed (the remote server has fully upgraded us to a websocket).

  • void add_to_socket_set(core::PollSet & set)
    Adds this client to the given socket set, so that if you poll(), it will trigger when the socket is ready to read.

    Does nothing if the socket is currently disconnected.

  • void set_target_address(const NetworkAddress & address)
    Resets the network address. Can only be called if the socket is already disconnected. This is intended mostly to assist with unit testing.

  • size_t queued_message_count()
    Returns the number of queued messages that have been read so far. You can read this many messages off the queue before you would need to call poll() again.

  • WebSocketMessagePtr pop_message()
    Returns the most recently received queue message, if any. Returns a nullptr if there are none.

  • std::vector< WebSocketMessagePtr > pop_messages()
    Returns all of the queued messages to date. Clears out the queue.

  • void write(const core::ByteBuffer & data)
    Writes the given message over the websocket, sending it next-in-line in the event that messages are queued. You should be regularly invoking poll to ensure data fully completes.

  • bool poll(std::chrono::nanoseconds duration)
    Polls for new events. This is the main message pump for the client, and takes care of flushing write buffers, connections, and reading data.

    Returns true if this processed data, or false otherwise.

  • WebSocketClientStatistics statistics()
    Returns statistics related to this client.