Skip to main content

WebSocketManager

A class in Alta.Api.Client, Alta.Api.Client.HighLevel.Subscriptions.

A small WebSocket client for the Alta API that sends JSON messages and routes incoming events to handlers by event name. Opens the socket lazily on the first send.

Constructors​

MemberWhat it is
WebSocketManager(string authToken, string apiKey, string uri)Creates a WebSocketManager from authToken, apiKey and uri.

Fields​

5 private and internal fields
MemberWhat it is
private readonly string apiKeyAPI key sent in the x-api-key header.
private readonly string authTokenThe bearer token, stored with the Bearer prefix, sent in the authorization header.
private readonly Dictionary<string, Action<string>> handlersEvent name to callback map used by MessageReceived.
private readonly string uriWebSocket address to connect to.
private TaskCompletionSource<WebSocket> webSocketSourceCompletes when the socket opens. Once set, later sends reuse it; there is no reconnect if it drops.

Methods​

MemberWhat it is
async Task SendMessage(string path, object body)Serialises body to JSON, wraps it in a WebsocketMessageBody with the given path and sends it, connecting first if needed.
void SetHandler(string type, Action<string> handle)Registers a callback for an event type. A second handler for the same type is chained after the first, not replaced.
3 private and internal methods
MemberWhat it is
private Task<WebSocket> GetWebSocket()Returns the shared socket task, opening the connection with the auth and API key headers the first time it's called.
private void HandleError(object sender, ErrorEventArgs e)Socket error handler; only writes the exception to the console.
private void MessageReceived(object sender, MessageReceivedEventArgs eventArgs)Parses each incoming message as a WebsocketResponseBody and calls the handler registered for its Event with the Content.