Skip to content

WebSocketClient

class cAlgo.API

DesktopCLICloud

Represents a web socket client.

Environment behaviour

Cloud — not supported. Connect, Send and Close do nothing: "WebSocket is not supported in cloud environment." is written to the log, State stays None and no events are raised.

Definition

public class WebSocketClient

Namespace: cAlgo.API · Group: WebSocket

Remarks

Algos can use WebSocketClient to establish a WebSocket connection with a local or remote host and send or receive data.

Examples

using System;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots;

public partial class Test : Robot
{
    protected override void OnStart()
    {
        var webSocket = new WebSocketClient();

        webSocket.Connected += args => Print("Connected");
        webSocket.Disconnected += args => Print("Disconnected");
        webSocket.TextReceived += args => Print("Text Received: ", args.Text);
        webSocket.BinaryReceived += args => Print("Binary Received: ", Encoding.UTF8.GetString(args.Data));

        var panel = new StackPanel()
        {
            BackgroundColor = Color.BlueViolet,
            Orientation = Orientation.Vertical,
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };

        var connectButton = new Button
        {
            Text = "Connect"
        };

        connectButton.Click += args => webSocket.Connect(new Uri(Host));

        var disconnectButton = new Button
        {
            Text = "Disconnect"
        };

        disconnectButton.Click += args => webSocket.Close(WebSocketClientCloseStatus.Empty, null);

        var disposeButton = new Button
        {
            Text = "Dispose"
        };

        disposeButton.Click += args =>
        {
            webSocket.Dispose();

            Print($"WebSocket Disposed, state: {webSocket.State}");
        };

        var textBox = new TextBox()
        {
            Text = "Type your message...",
            Width = 100
        };

        var sendTextButton = new Button
        {
            Text = "Send Text"
        };

        var sendBinaryButton = new Button
        {
            Text = "Send Binary"
        };

        sendTextButton.Click += args => webSocket.Send(textBox.Text);
        sendBinaryButton.Click += args => webSocket.Send(Encoding.UTF8.GetBytes(textBox.Text));

        panel.AddChild(connectButton);
        panel.AddChild(disconnectButton);
        panel.AddChild(disposeButton);
        panel.AddChild(textBox);
        panel.AddChild(sendTextButton);
        panel.AddChild(sendBinaryButton);

        Chart.AddControl(panel);
    }
}
import clr
clr.AddReference("cAlgo.API")

from cAlgo.API import *
from System import Uri
from System.Text import Encoding

class Test():
    def on_start(self):
        webSocket = WebSocketClient()
        webSocket.Connected += lambda args: print("Connected")
        webSocket.Disconnected += lambda args: print("Disconnected")
        webSocket.TextReceived += lambda args: print("Text Received: ", args.Text)
        webSocket.BinaryReceived += lambda args: print("Binary Received: ", Encoding.UTF8.GetString(args.Data))

        panel = StackPanel()
        panel.BackgroundColor = Color.BlueViolet
        panel.Orientation = Orientation.Vertical
        panel.HorizontalAlignment = HorizontalAlignment.Center
        panel.VerticalAlignment = VerticalAlignment.Center

        connectButton = Button()
        connectButton.Text = "Connect"
        connectButton.Click += lambda args: webSocket.Connect(Uri(api.Host))

        disconnectButton = Button()
        disconnectButton.Text = "Disconnect"
        disconnectButton.Click += lambda args: webSocket.Close(WebSocketClientCloseStatus.Empty, None)

        disposeButton = Button()
        disposeButton.Text = "Dispose"

        disposeButton.Click += lambda args: self.dispose_client(webSocket)

        textBox = TextBox()
        textBox.Text = "Type your message..."
        textBox.Width = 100

        sendTextButton = Button()
        sendTextButton.Text = "Send Text"
        sendTextButton.Click += lambda args: webSocket.Send(textBox.Text)

        sendBinaryButton = Button()
        sendBinaryButton.Text = "Send Binary"
        sendBinaryButton.Click += lambda args: webSocket.Send(Encoding.UTF8.GetBytes(textBox.Text))

        panel.AddChild(connectButton)
        panel.AddChild(disconnectButton)
        panel.AddChild(disposeButton)
        panel.AddChild(textBox)
        panel.AddChild(sendTextButton)
        panel.AddChild(sendBinaryButton)

        api.Chart.AddControl(panel)

    def dispose_client(self, webSocketClient):
        webSocketClient.Dispose()
        print(f"WebSocket Disposed, state: {webSocketClient.State}")
{
  "AccessRights": "None",
  "Parameters": [
    {
      "Name": "Host",
      "Type": "String",
      "DefaultValue": "ws://localhost:1337"
    }
  ]
}

Constructors

Name Description
WebSocketClient 2 overloadsDesktopCLICloud Creates a WebSocketClient with default options.

Methods

Name Description
CloseDesktopCLICloudvoid Disconnects WebSocketClient.
ConnectDesktopCLICloudvoid Connects WebSocketClient to passed URI.
DisposeDesktopCLICloudvoid Releases the unmanaged resources used by the WebSocketClient instance.
Send 2 overloadsDesktopCLICloudvoid Sends passed text.

Properties

Name Description
CloseStatusDesktopCLICloudWebSocketClientCloseStatus? Gets close status / reason.
CloseStatusDescriptionDesktopCLICloudstring Gets a description of the reason why the instance was closed.
StateDesktopCLICloudWebSocketClientState Gets current state of WebSocketClient instance.

Events

Name Description
BinaryReceivedDesktopCLICloudAction<WebSocketClientBinaryReceivedEventArgs> Occurs when WebSocketClient receives binary data.
ConnectedDesktopCLICloudAction<WebSocketClientConnectedEventArgs> Occurs when WebSocketClient is connected.
DisconnectedDesktopCLICloudAction<WebSocketClientDisconnectEventArgs> Occurs when WebSocketClient is disconnected.
TextReceivedDesktopCLICloudAction<WebSocketClientTextReceivedEventArgs> Occurs when WebSocketClient receives text data.

See also