forked from Marfusios/websocket-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
144 lines (120 loc) · 4.83 KB
/
Program.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.IO;
using System.Net.WebSockets;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Serilog.Events;
namespace Websocket.Client.Sample
{
class Program
{
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
InitLogging();
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
AssemblyLoadContext.Default.Unloading += DefaultOnUnloading;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Console.WriteLine("|=======================|");
Console.WriteLine("| WEBSOCKET CLIENT |");
Console.WriteLine("|=======================|");
Console.WriteLine();
Log.Debug("====================================");
Log.Debug(" STARTING ");
Log.Debug("====================================");
var factory = new Func<ClientWebSocket>(() =>
{
var client = new ClientWebSocket
{
Options =
{
KeepAliveInterval = TimeSpan.FromSeconds(5),
// Proxy = ...
// ClientCertificates = ...
}
};
//client.Options.SetRequestHeader("Origin", "xxx");
return client;
});
var url = new Uri("wss://www.bitmex.com/realtime");
using (IWebsocketClient client = new WebsocketClient(url, factory))
{
client.Name = "Bitmex";
client.ReconnectTimeout = TimeSpan.FromSeconds(30);
client.ErrorReconnectTimeout = TimeSpan.FromSeconds(30);
client.ReconnectionHappened.Subscribe(type =>
{
Log.Information($"Reconnection happened, type: {type}, url: {client.Url}");
});
client.DisconnectionHappened.Subscribe(info =>
Log.Warning($"Disconnection happened, type: {info.Type}"));
client.MessageReceived.Subscribe(msg =>
{
Log.Information($"Message received: {msg}");
});
Log.Information("Starting...");
client.Start().Wait();
Log.Information("Started.");
Task.Run(() => StartSendingPing(client));
Task.Run(() => SwitchUrl(client));
ExitEvent.WaitOne();
}
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
}
private static async Task StartSendingPing(IWebsocketClient client)
{
while (true)
{
await Task.Delay(1000);
if(!client.IsRunning)
continue;
client.Send("ping");
}
}
private static async Task SwitchUrl(IWebsocketClient client)
{
while (true)
{
await Task.Delay(20000);
var production = new Uri("wss://www.bitmex.com/realtime");
var testnet = new Uri("wss://testnet.bitmex.com/realtime");
var selected = client.Url == production ? testnet : production;
client.Url = selected;
await client.Reconnect();
}
}
private static void InitLogging()
{
var executingDir = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
var logPath = Path.Combine(executingDir, "logs", "verbose.log");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
.WriteTo.ColoredConsole(LogEventLevel.Verbose,
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message} {NewLine}{Exception}")
.CreateLogger();
}
private static void CurrentDomainOnProcessExit(object sender, EventArgs eventArgs)
{
Log.Warning("Exiting process");
ExitEvent.Set();
}
private static void DefaultOnUnloading(AssemblyLoadContext assemblyLoadContext)
{
Log.Warning("Unloading process");
ExitEvent.Set();
}
private static void ConsoleOnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Log.Warning("Canceling process");
e.Cancel = true;
ExitEvent.Set();
}
}
}