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.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | const char* | ws:// or wss://, with a port and a path when needed. | |
listener | ISource2ModWebSocketListener* | Where the socket reports; must outlive it. | |
options | const 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
| Field | Type | Description |
|---|---|---|
m_Headers | std::vector<Source2ModHTTPHeader> | Extra headers on the opening handshake (Authorization, cookies, ...). |
m_bAutoReconnect | bool | Open the connection again when it is lost. |
m_nPingIntervalSec | int | Seconds without traffic before a ping goes out; the connection counts as lost after twice that. 0 for no pings. |
m_bVerifyPeer | bool | Check the server's certificate (wss://) against the system's CA store. Off only for test servers with a self-signed one. |
m_nMaxMessageSize | size_t | A 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.
| Parameter | Type |
|---|---|
socket | ISource2ModWebSocket* |
reconnected | bool |
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.
| Parameter | Type |
|---|---|
socket | ISource2ModWebSocket* |
data | const char* |
length | size_t |
binary | bool |
OnWebSocketClose
virtual void OnWebSocketClose(ISource2ModWebSocket* socket, int code, const char* reason);The connection is gone.
| Parameter | Type | Description |
|---|---|---|
socket | ISource2ModWebSocket* | |
code | int | The close code either side sent, 1006 when the connection was lost without one. |
reason | const char* |
OnWebSocketError
virtual void OnWebSocketError(ISource2ModWebSocket* socket, const char* error);Why connecting or the connection failed, for logging.
| Parameter | Type |
|---|---|
socket | ISource2ModWebSocket* |
error | const 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.
| Parameter | Type |
|---|---|
text | const char* |
SendBinary
virtual bool SendBinary(const void* data, size_t length) = 0;Sends a binary message; otherwise like Send().
| Parameter | Type |
|---|---|
data | const void* |
length | size_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.
| Parameter | Type | Default |
|---|---|---|
code | int | 1000 |
reason | const 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 : intWhere a socket is.
| Name | Value | Description |
|---|---|---|
Connecting | 0 | Opening, or waiting to open again after the connection was lost. |
Open | ||
Closed | Closed from this side, or lost with reconnecting off. For good. |