forked from GoChartingAdmin/client-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cs
44 lines (43 loc) · 1.55 KB
/
example.cs
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
using System;
using WebSocket4Net;
using System.Security.Authentication;
namespace HelloWorld
{
class Hello
{
WebSocket websocket;
static void Main()
{
new Hello().Start();
}
public void Start(){
this.websocket = new WebSocket("wss://socket.polygon.io/stocks", sslProtocols: SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls);
this.websocket.Opened += websocket_Opened;
this.websocket.Error += websocket_Error;
this.websocket.Closed += websocket_Closed;
this.websocket.MessageReceived += websocket_MessageReceived;
this.websocket.Open();
Console.ReadKey();
}
private void websocket_Opened(object sender, EventArgs e)
{
Console.WriteLine("Connected!");
this.websocket.Send("{\"action\":\"auth\",\"params\":\"YOUR_API_KEY\"}");
this.websocket.Send("{\"action\":\"subscribe\",\"params\":\"T.AAPL\"}");
}
private void websocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
{
Console.WriteLine("WebSocket Error");
Console.WriteLine(e.Exception.Message);
}
private void websocket_Closed(object sender, EventArgs e)
{
Console.WriteLine("Connection Closed...");
// Add Reconnect logic... this.Start()
}
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}