152 lines
4.5 KiB
C#
152 lines
4.5 KiB
C#
|
using System;
|
|||
|
using System.Net.WebSockets;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using SafeMobileLib.WebsocketClient;
|
|||
|
using SafeMobileLib;
|
|||
|
using System.ComponentModel;
|
|||
|
|
|||
|
namespace AppServer
|
|||
|
{
|
|||
|
public class WebsocketThread
|
|||
|
{
|
|||
|
|
|||
|
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
|
|||
|
private static readonly object padlock = new object();
|
|||
|
private static WebsocketThread instance = null;
|
|||
|
|
|||
|
public String host = "wss://www.bitmex.com/realtime";
|
|||
|
|
|||
|
private WebsocketThread() {
|
|||
|
}
|
|||
|
|
|||
|
public static WebsocketThread Instance
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
lock(padlock)
|
|||
|
{
|
|||
|
if (instance == null)
|
|||
|
instance = new WebsocketThread();
|
|||
|
return instance;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
public static IWebsocketClient _client;
|
|||
|
|
|||
|
public IWebsocketClient getClient()
|
|||
|
{
|
|||
|
return _client;
|
|||
|
}
|
|||
|
|
|||
|
public void StartWebsocket()
|
|||
|
{
|
|||
|
StartWebsocket(this, null);
|
|||
|
}
|
|||
|
|
|||
|
public void StartWebsocket(object sender, DoWorkEventArgs e)
|
|||
|
{
|
|||
|
try {
|
|||
|
|
|||
|
Utils.WriteLine("|=======================|");
|
|||
|
Utils.WriteLine("| WEBSOCKET CLIENT |");
|
|||
|
Utils.WriteLine("|=======================|");
|
|||
|
Utils.WriteLine("");
|
|||
|
|
|||
|
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
|
|||
|
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
|
|||
|
|
|||
|
|
|||
|
var factory = new Func<ClientWebSocket>(() =>
|
|||
|
{
|
|||
|
var client = new ClientWebSocket
|
|||
|
{
|
|||
|
Options =
|
|||
|
{
|
|||
|
KeepAliveInterval = TimeSpan.FromSeconds(5),
|
|||
|
// Proxy = ...
|
|||
|
// ClientCertificates = ...
|
|||
|
}
|
|||
|
};
|
|||
|
return client;
|
|||
|
});
|
|||
|
|
|||
|
var url = new Uri(host);
|
|||
|
|
|||
|
using (IWebsocketClient client = new WebsocketClient(url, factory))
|
|||
|
{
|
|||
|
client.Name = Program.COMPANY;
|
|||
|
client.ReconnectTimeout = TimeSpan.FromSeconds(30);
|
|||
|
client.ErrorReconnectTimeout = TimeSpan.FromSeconds(30);
|
|||
|
client.ReconnectionHappened.Subscribe(type =>
|
|||
|
{
|
|||
|
Utils.WriteLine($"Reconnection happened, type: {type}, url: {client.Url}");
|
|||
|
});
|
|||
|
client.DisconnectionHappened.Subscribe(info =>
|
|||
|
Utils.WriteLine($"Disconnection happened, type: {info.Type}"));
|
|||
|
|
|||
|
client.MessageReceived.Subscribe(msg =>
|
|||
|
{
|
|||
|
});
|
|||
|
|
|||
|
Utils.WriteLine("Starting...");
|
|||
|
client.Start().Wait();
|
|||
|
Utils.WriteLine("Started.");
|
|||
|
|
|||
|
_client = client;
|
|||
|
|
|||
|
Task.Run(() => StartSendingPing(client));
|
|||
|
|
|||
|
ExitEvent.WaitOne();
|
|||
|
}
|
|||
|
|
|||
|
Utils.WriteLine("====================================");
|
|||
|
Utils.WriteLine(" STOPPING ");
|
|||
|
Utils.WriteLine("====================================");
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Utils.WriteLine("StartWebsocket Exception: " + ex.ToString(), ConsoleColor.Red);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void CurrentDomainOnProcessExit(object sender, EventArgs eventArgs)
|
|||
|
{
|
|||
|
Utils.WriteLine("Exiting process");
|
|||
|
ExitEvent.Set();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private static void ConsoleOnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
|||
|
{
|
|||
|
Utils.WriteLine("Canceling process");
|
|||
|
e.Cancel = true;
|
|||
|
ExitEvent.Set();
|
|||
|
}
|
|||
|
|
|||
|
private static async Task StartSendingPing(IWebsocketClient client)
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
await Task.Delay(2000);
|
|||
|
|
|||
|
if (!client.IsRunning)
|
|||
|
continue;
|
|||
|
|
|||
|
client.Send("ping");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void SendJson(IWebsocketClient client, string json)
|
|||
|
{
|
|||
|
if (!client.IsRunning)
|
|||
|
return;
|
|||
|
|
|||
|
//Console.WriteLine("DDDDDDDD: " + json);
|
|||
|
|
|||
|
client.Send(json);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|