Gin
Loading...
Searching...
No Matches
AsyncWebsocket Class Reference

Asynchronous WebSocket client with non-blocking operations. More...

#include <gin_asyncwebsocket.h>

Inherits juce::Thread.

Public Member Functions

 AsyncWebsocket (const juce::URL url, const juce::StringPairArray &customHeaders={})
 ~AsyncWebsocket () override
void connect ()
void disconnect ()
bool isConnected ()
void setPingInterval (int seconds)
void send (const juce::String &text)
void send (const juce::MemoryBlock &binary)
void sendPing ()

Public Attributes

std::function< void()> onConnect
std::function< void()> onDisconnect
std::function< void(const juce::String &)> onText
std::function< void(const juce::MemoryBlock &)> onBinary

Detailed Description

Asynchronous WebSocket client with non-blocking operations.

AsyncWebsocket provides a thread-safe, asynchronous interface to WebSocket connections. All network operations run on a background thread, ensuring that none of the public methods block the calling thread. All callbacks are delivered on the JUCE message thread for safe UI updates.

Key Features:

  • Non-blocking connect, disconnect, and send operations
  • Automatic ping/pong keep-alive with configurable interval
  • Thread-safe message queuing for outgoing data
  • Lambda-based callbacks on the message thread
  • Support for both text and binary WebSocket messages
  • Custom header support for authentication and protocols

Usage: Create an AsyncWebsocket with a URL, register callback handlers, then call connect(). The connection runs asynchronously on a background thread.

AsyncWebsocket ws(juce::URL("wss://example.com/socket"));
ws.onConnect = []() {
DBG("Connected!");
};
ws.onText = [](const juce::String& message) {
DBG("Received: " + message);
};
ws.onDisconnect = []() {
DBG("Disconnected");
};
ws.connect(); // Non-blocking
ws.send("Hello, server!");
AsyncWebsocket(const juce::URL url, const juce::StringPairArray &customHeaders={})

Thread Safety:

  • All public methods are thread-safe
  • Callbacks execute on the JUCE message thread
  • Internal message queue protected by critical section
See also
WebSocket, SecureStreamingSocket

Constructor & Destructor Documentation

◆ AsyncWebsocket()

AsyncWebsocket::AsyncWebsocket ( const juce::URL url,
const juce::StringPairArray & customHeaders = {} )

◆ ~AsyncWebsocket()

AsyncWebsocket::~AsyncWebsocket ( )
override

Member Function Documentation

◆ connect()

void AsyncWebsocket::connect ( )

◆ disconnect()

void AsyncWebsocket::disconnect ( )

◆ isConnected()

bool AsyncWebsocket::isConnected ( )

◆ setPingInterval()

void AsyncWebsocket::setPingInterval ( int seconds)

◆ send() [1/2]

void AsyncWebsocket::send ( const juce::String & text)

◆ send() [2/2]

void AsyncWebsocket::send ( const juce::MemoryBlock & binary)

◆ sendPing()

void AsyncWebsocket::sendPing ( )

Member Data Documentation

◆ onConnect

std::function<void ()> AsyncWebsocket::onConnect

◆ onDisconnect

std::function<void ()> AsyncWebsocket::onDisconnect

◆ onText

std::function<void (const juce::String&)> AsyncWebsocket::onText

◆ onBinary

std::function<void (const juce::MemoryBlock&)> AsyncWebsocket::onBinary

The documentation for this class was generated from the following file: