ISource2ModWebSockets

WebSocket clients (ws:// and wss://).

Steam has no WebSocket client, so these are Source2Mod's own: one IO thread in the core runs every connection, and everything a socket reports reaches its listener on the main thread. A lost connection is opened again on its own, with a delay growing from 1 to 30 seconds.

class Feed : public ISource2ModWebSocketListener
{
    void OnWebSocketOpen(ISource2ModWebSocket* socket, bool reconnected) override
    {
        socket->Send("{\"hello\":1}");
    }
    void OnWebSocketMessage(ISource2ModWebSocket* socket, const char* data, size_t length, bool binary) override
    {
        // data is valid for this call only.
    }
};

Feed g_feed;
ISource2ModWebSocket* g_pSocket = g_pSource2ModWebSockets->Connect("wss://example.com/feed", &g_feed);
// ...
g_pSocket->Destroy();

Interface id ISource2ModWebSockets001 (SOURCE2MOD_WEBSOCKETS_INTERFACE) · Global g_pSource2ModWebSockets · Header source2mod/ISource2ModWebSockets.h

Methods

Connect

virtual ISource2ModWebSocket* Connect(const char* url, ISource2ModWebSocketListener* listener, const Source2ModWebSocketOptions& options = Source2ModWebSocketOptions()) = 0;

Opens a WebSocket to @p url.

ParameterTypeDefaultDescription
urlconst char*ws:// or wss://, with a port and a path when needed.
listenerISource2ModWebSocketListener*Where the socket reports; must outlive it.
optionsconst Source2ModWebSocketOptions&Source2ModWebSocketOptions()

Returns ISource2ModWebSocket* — The socket, Connecting; nullptr for a URL that is not ws:// or wss://.

Source2ModWebSocketOptions

How Connect() connects. The defaults suit most servers.

Fields

FieldTypeDescription
m_Headersstd::vector<Source2ModHTTPHeader>Extra headers on the opening handshake (Authorization, cookies, ...).
m_bAutoReconnectboolOpen the connection again when it is lost.
m_nPingIntervalSecintSeconds without traffic before a ping goes out; the connection counts as lost after twice that. 0 for no pings.
m_bVerifyPeerboolCheck the server's certificate (wss://) against the system's CA store. Off only for test servers with a self-signed one.
m_nMaxMessageSizesize_tA larger message drops the connection.

ISource2ModWebSocketListener

What a socket reports, on the main thread. Every method is optional.

The plugin the listener's class lies in owns the socket: it is destroyed, listener calls and all, when that plugin unloads.

OnWebSocketOpen

virtual void OnWebSocketOpen(ISource2ModWebSocket* socket, bool reconnected);

The handshake went through. @p reconnected for every open after the first.

ParameterType
socketISource2ModWebSocket*
reconnectedbool

OnWebSocketMessage

virtual void OnWebSocketMessage(ISource2ModWebSocket* socket, const char* data, size_t length, bool binary);

A whole message. @p data is valid only for this call; a text message is also null-terminated.

ParameterType
socketISource2ModWebSocket*
dataconst char*
lengthsize_t
binarybool

OnWebSocketClose

virtual void OnWebSocketClose(ISource2ModWebSocket* socket, int code, const char* reason);

The connection is gone.

Unless it was closed from here or m_bAutoReconnect is off, the socket is Connecting again after this, and OnWebSocketOpen follows once it is back.
ParameterTypeDescription
socketISource2ModWebSocket*
codeintThe close code either side sent, 1006 when the connection was lost without one.
reasonconst char*

OnWebSocketError

virtual void OnWebSocketError(ISource2ModWebSocket* socket, const char* error);

Why connecting or the connection failed, for logging.

ParameterType
socketISource2ModWebSocket*
errorconst char*

ISource2ModWebSocket

One connection. Valid until Destroy(), or until its owner unloads.

Send

virtual bool Send(const char* text) = 0;

Sends a text message. Queued in order; false while the socket is not Open.

ParameterType
textconst char*

SendBinary

virtual bool SendBinary(const void* data, size_t length) = 0;

Sends a binary message; otherwise like Send().

ParameterType
dataconst void*
lengthsize_t

Close

virtual void Close(int code = 1000, const char* reason = "") = 0;

Closes the connection with @p code; it is not opened again. OnWebSocketClose follows once the server answered.

ParameterTypeDefault
codeint1000
reasonconst char*""

GetState

virtual ESource2ModWebSocketState GetState() const = 0;

GetUrl

virtual const char* GetUrl() const = 0;

Destroy

virtual void Destroy() = 0;

Closes the connection if it is open and frees the socket. The listener hears nothing more, not even OnWebSocketClose. Safe from inside a listener call.

Enums

ESource2ModWebSocketState

enum class ESource2ModWebSocketState : int

Where a socket is.

NameValueDescription
Connecting0Opening, or waiting to open again after the connection was lost.
Open
ClosedClosed from this side, or lost with reconnecting off. For good.

On this page