Skip to content

WebSocketClient

Summary

Represents a web socket client.

Remarks

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

Signature

1
public class WebSocketClient

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 using System;
 using System.Text;
 using cAlgo.API;
 namespace cAlgo.Robots;
 [Robot(AccessRights = AccessRights.None)]
 public class Test : Robot
 {
     [Parameter(DefaultValue = "ws://localhost:1337")]
     public string Host { get; set; }
     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);
     }
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 import clr
 clr.AddReference("cAlgo.API")
 from cAlgo.API import *
 from System import Uri
 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"
         # Host is a parameter of type string defined in C# file of cBot
         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}")

See Also

Methods

Connect

Summary

Connects WebSocketClient to passed URI.

Signature

1
public void Connect(Uri uri)

Parameters

Name Type Description
uri Uri The server URI.

Return Value

void

Close

Summary

Gets close status / reason.

Signature

1
public void Close(WebSocketClientCloseStatus closeStatus, string closeStatusDescription)

Parameters

Name Type Description
closeStatus WebSocketClientCloseStatus
closeStatusDescription string

Return Value

void

Send (2)

Send (1 of 2)

Summary

Sends passed text.

Signature

1
public void Send(string text)

Parameters

Name Type Description
text string Text to send.

Return Value

void

Send (2 of 2)

Summary

Sends passed binary data.

Signature

1
public void Send(byte[] data)

Parameters

Name Type Description
data byte[] Data to send.

Return Value

void

SubscribeToWebSocketClientEvent

Signature

1
private void SubscribeToWebSocketClientEvent()

Return Value

void

OnBinaryReceived

Signature

1
private void OnBinaryReceived(byte[] data)

Parameters

Name Type Description
data byte[]

Return Value

void

OnTextReceived

Signature

1
private void OnTextReceived(string text)

Parameters

Name Type Description
text string

Return Value

void

OnDisconnected

Signature

1
private void OnDisconnected()

Return Value

void

OnConnected

Signature

1
private void OnConnected()

Return Value

void

Dispose

Summary

Releases the unmanaged resources used by the WebSocketClient instance.

Signature

1
public void Dispose()

Return Value

void

Properties

State

Summary

Gets current state of WebSocketClient instance.

Signature

1
public WebSocketClientState State {get;}

Return Value

WebSocketClientState

CloseStatus

Summary

Gets close status / reason.

Signature

1
public WebSocketClientCloseStatus? CloseStatus {get;}

Return Value

WebSocketClientCloseStatus?

CloseStatusDescription

Summary

Gets a description of the reason why the instance was closed.

Signature

1
public string CloseStatusDescription {get;}

Return Value

string

Events

Connected

Summary

Occurs when WebSocketClient is connected.

Signature

1
public event Action<WebSocketClientConnectedEventArgs> Connected;

Disconnected

Summary

Occurs when WebSocketClient is disconnected.

Signature

1
public event Action<WebSocketClientDisconnectEventArgs> Disconnected;

TextReceived

Summary

Occurs when WebSocketClient receives text data.

Signature

1
public event Action<WebSocketClientTextReceivedEventArgs> TextReceived;

BinaryReceived

Summary

Occurs when WebSocketClient receives binary data.

Signature

1
public event Action<WebSocketClientBinaryReceivedEventArgs> BinaryReceived;