forked from Marfusios/websocket-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
91 lines (74 loc) · 3.05 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
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Serilog.Events;
namespace Websocket.Client.Sample.NetFramework
{
class Program
{
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
static void Main(string[] args)
{
InitLogging();
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Console.WriteLine("|=======================|");
Console.WriteLine("| WEBSOCKET CLIENT |");
Console.WriteLine("|=======================|");
Console.WriteLine();
Log.Debug("====================================");
Log.Debug(" STARTING ");
Log.Debug("====================================");
var url = new Uri("wss://www.bitmex.com/realtime");
using (var client = new WebsocketClient(url))
{
client.Name = "Bitmex";
client.ReconnectTimeoutMs = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;
client.ReconnectionHappened.Subscribe(type =>
Log.Information($"Reconnection happened, type: {type}"));
client.DisconnectionHappened.Subscribe(type =>
Log.Warning($"Disconnection happened, type: {type}"));
client.MessageReceived.Subscribe(msg => Log.Information($"Message received: {msg}"));
client.Start();
Task.Run(() => StartSendingPing(client));
ExitEvent.WaitOne();
}
Log.Debug("====================================");
Log.Debug(" STOPPING ");
Log.Debug("====================================");
Log.CloseAndFlush();
}
private static async Task StartSendingPing(WebsocketClient client)
{
while (true)
{
await Task.Delay(1000);
await client.Send("ping");
}
}
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)
.CreateLogger();
}
private static void CurrentDomainOnProcessExit(object sender, EventArgs eventArgs)
{
Log.Warning("Exiting process");
ExitEvent.Set();
}
private static void ConsoleOnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Log.Warning("Canceling process");
e.Cancel = true;
ExitEvent.Set();
}
}
}