Skip to content

ClientSocket

Inherits IDisposable

Public Member Functions

void

Dispose()

Releases the resources used by this instance.

bool

IsTokenValid()

Checks whether the current JWT token is valid based on its structure and expiration time. if the current token is valid; otherwise, .

void

Connect()

Opens the WebSocket connection.

void

Disconnect()

Closes the WebSocket connection and disposes of the client.

Task

GetAuthCode()

Requests an application auth code from the service. A task that resolves to the received ApplicationAuthCode.

Task

ExtendToken()

Extends the validity period of the current user token. if the token was successfully extended; otherwise, . This method requires the functionality to be enabled for the application on the Crowd Control service. If the application is not authorized to extend tokens, this method will always return false. Please contact Crowd Control support for more information.

Task

RedeemAuthCode(string code)

Redeems an application auth code for a user token and decodes the user information. if the auth code was redeemed successfully; otherwise, .

string?

GetInteractLink()

Gets the Crowd Control interact page link for the current user. The interact page URL for the current user, or if no user is available.

Task

SubscribeTopic(params string[] topic)

Subscribes to one or more PubSub topics. if the subscription message was sent successfully; otherwise, .

Task

SubscribeTopic(IEnumerable topic)

Subscribes to one or more PubSub topics. if the subscription message was sent successfully; otherwise, .

void

RestartSessionIfNeeded(object _)

Timer callback that attempts to restart the session when reconnect conditions are met.

Task

RestartSessionIfNeeded()

Checks the connection and session status, and attempts to restart the session if the connection was lost but auto-reconnect is enabled. A task that completes when the reconnection attempt, if any, has finished.

Task

RestartSession()

Restarts the current game session by resubscribing to the necessary PubSub topics. if the session was successfully restarted; otherwise, .

Task

StartSession(bool forceNew)

Starts a game session for the configured game pack. if the session was successfully started; otherwise, .

Task

StopSession()

Stops the current game session. if the session was successfully stopped; otherwise, .

Task

LoadCustomEffects(IEnumerable effects, CustomEffects.OperationMode mode)

Loads a set of custom effects to the service for the current game pack. if the effects were successfully loaded; otherwise, .

Task

DeleteCustomEffects(IEnumerable effects)

Removes custom effects from the service by their effect IDs. A task that resolves to true when the removal request completes successfully; otherwise false.

Task

DeleteCustomEffects(IEnumerable effects)

Removes custom effects from the service by their effect IDs. A task that resolves to true when the removal request completes successfully; otherwise false.

Task

Ping()

Sends a ping message to the service and waits for a pong response to verify connectivity. if a pong response was received within the timeout period; otherwise, .

Task

Send(string method, JObject payload)

Sends an RPC message with the specified method and payload. if the message was sent successfully; otherwise, .

Task

SendRaw(MessageData message)

Sends message data by wrapping it into a local message payload. if the message was sent successfully; otherwise, .

Task

SendRaw(LocalMessage message)

Sends a raw JSON message over the WebSocket connection. if the message was sent successfully; otherwise, .

Task

SendRaw(JObject message)

Sends a raw JSON message over the WebSocket connection. if the message was sent successfully; otherwise, .

Task

Call(string method, params object[] args)

Sends an RPC call with the specified method and arguments. if the message was sent successfully; otherwise, .

Task

Call(string method, IEnumerable args)

Sends an RPC call with the specified method and arguments. if the message was sent successfully; otherwise, .

void

Respond(EffectRequest request, EffectStatus status, string? message)

Sends an effect response back to the service.

void

Respond(EffectRequest request, EffectResponse response)

Sends an effect response back to the service.

void

Respond(EffectRequest request, EffectStatus status, StandardErrors? messageID, string? message)

Sends an effect response back to the service.

void

Respond(EffectRequest request, EffectStatus status, SITimeSpan? timeRemaining, string? message)

Sends an effect response back to the service.

void

Respond(EffectRequest request, EffectStatus status, SITimeSpan? timeRemaining, StandardErrors? messageID, string? message)

Sends an effect response back to the service.

void

MetadataChanged(IDictionary metadata)

Sends a metadata update message for the current game pack.

void

MetadataChanged(string key, object value)

Sends a metadata update message for the current game pack.

Public Static Functions

bool

IsTokenValid(string? token)

Checks whether a given JWT token is valid based on its structure and expiration time. if the token is valid; otherwise, .

bool

TryGetJwtContents(string token, out User user)

Decodes the payload of a JWT token into a User object. if the token was successfully decoded; otherwise, .

Private Functions

void

RefreshToken(object? _)

Task

RefreshToken()

void

OnOpen(object? sender, EventArgs e)

Handles the WebSocket open event.

void

OnClose(object? sender, CloseEventArgs e)

Handles the WebSocket close event.

void

OnError(object? sender, ErrorEventArgs e)

Handles the WebSocket error event.

void

OnMessage(object? sender, MessageEventArgs e)

Handles incoming WebSocket messages and routes them based on payload type.

void

SendKeepalivePing(object? _)

Timer callback that sends a keepalive ping so the service does not close the connection for inactivity.

bool

ProcessAuthResponse(string responseBody)

Processes an authentication response to extract and decode the user token. if a valid token was extracted and decoded; otherwise, .

void

Report(string effectID, EffectStatus status)

Reports status for one effect by id.

void

Report(string[] effectIDs, EffectStatus status)

Reports status for multiple effects by id.

void

SubscribeToTopics(params string[] topics)

Subscribes to PubSub topics using the current token.

void

SubscribeToTopics(IEnumerable topics)

Subscribes to PubSub topics using the current token.

Protected Functions

void

Dispose(bool disposing, bool stopSession)

Releases the resources used by this instance.

Private Attributes

WebSocketSharp.CustomHeaders.WebSocket

m_ws

Underlying WebSocket connection to the PubSub service.

HttpClient

m_http

HTTP client for REST API calls.

ApplicationAuthCodeRedeemed

m_lastApplicationAuthCodeRedeemed

The most recent application auth code redemption payload received from the service.

string?

m_jwt

The current JWT token.

string?

m_authCodeVerifier

The PKCE verifier for the active application auth-code attempt.

User?

m_localUser

The local user information.

List

m_subscribedTopics

Set of currently subscribed topics.

string

m_sessionID

The current game session ID.

SITimeSpan

PING_TIMEOUT

TimeSpan

PING_INTERVAL

Interval between keepalive pings. The service closes connections after 10 minutes of inactivity, so pings must be sent at least every 9 minutes.

TimeSpan

TOKEN_REFRESH_TIME

TimeSpan

TOKEN_REFRESH_MARGIN

TimeSpan

RECONNECT_TIME

ServerMessage

EMPTY_PONG

Represents a static server message instance for a direct 'pong' response. This instance is used to avoid instantiating multiple identical 'pong' messages.

Timer?

m_tokenRefreshTimer

Timer?

m_reconnectTimer

Timer?

m_pingTimer

Properties

bool

Connected

[get, ]

Gets whether the WebSocket is connected.

string

GameID

[get, ]

The game identifier used when connecting to the Crowd Control service.

string

ApplicationID

[get, ]

The application identifier used for authentication with the Crowd Control service.

bool

IsDisposed

[get, set]

Gets a value indicating whether this instance has been disposed.

bool

AutoReconnect

[get, set]

Gets or sets a value indicating whether the client should automatically reconnect after an unexpected disconnect.

bool

IsActiveSession

[get, ]

Gets a value indicating whether the client is connected, has a session identifier, and has a valid token.

Events

Action<ApplicationAuthCode>?

ApplicationAuthCodeReceived

Raised when an application auth code is received from the Crowd Control API.

Action<ApplicationAuthCodeRedeemed>?

ApplicationAuthCodeRedeemedReceived

Raised when an application auth code has been redeemed.

Action<ApplicationAuthCodeError>?

ApplicationAuthCodeErrorReceived

Raised when an error occurs during the auth code flow.

Action<string>?

JwtTokenReceived

Raised when a new JWT token is obtained, either through redeeming an auth code or extending an existing token.

Action?

SessionReady

Raised when the client has successfully started a game session and is ready to receive effect requests.

Action?

SessionEnded

Raised when the current game session has been stopped and the client will no longer receive effect requests until a new session is started.

Action<Pong>?

PongReceived

Raised when a pong response is received from the Crowd Control API.

Action<EffectRequest>?

EffectRequestReceived

Raised when an effect request is received from the Crowd Control API.

Action<EffectRequest, Common.EffectResponse>?

EffectResponseSent

Raised when an effect request response is sent to the Crowd Control API.

Action<EffectReport>?

EffectReportSent

Raised when an effect class report is sent to the Crowd Control API.