mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-07-18 07:25:50 +00:00
Added websocket base
This commit is contained in:
parent
94eb269ade
commit
0f949334f2
99
CryptoExchange.Net/BaseSocket.cs
Normal file
99
CryptoExchange.Net/BaseSocket.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Security.Authentication;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CryptoExchange.Net.Interfaces;
|
||||||
|
using SuperSocket.ClientEngine.Proxy;
|
||||||
|
using WebSocket4Net;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net
|
||||||
|
{
|
||||||
|
public class BaseSocket: IWebsocket
|
||||||
|
{
|
||||||
|
protected WebSocket socket;
|
||||||
|
|
||||||
|
protected readonly List<Action<Exception>> errorhandlers = new List<Action<Exception>>();
|
||||||
|
protected readonly List<Action> openhandlers = new List<Action>();
|
||||||
|
protected readonly List<Action> closehandlers = new List<Action>();
|
||||||
|
protected readonly List<Action<string>> messagehandlers = new List<Action<string>>();
|
||||||
|
|
||||||
|
public bool IsClosed => socket.State == WebSocketState.Closed;
|
||||||
|
public bool IsOpen => socket.State == WebSocketState.Open;
|
||||||
|
|
||||||
|
public BaseSocket(string url):this(url, new Dictionary<string, string>(), new Dictionary<string, string>())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseSocket(string url, IDictionary<string, string> cookies, IDictionary<string, string> headers)
|
||||||
|
{
|
||||||
|
socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList());
|
||||||
|
socket.Opened += (o, s) => Handle(openhandlers);
|
||||||
|
socket.Closed += (o, s) => Handle(closehandlers);
|
||||||
|
socket.Error += (o, s) => Handle(errorhandlers, s.Exception);
|
||||||
|
socket.MessageReceived += (o, s) => Handle(messagehandlers, s.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public event Action OnClose
|
||||||
|
{
|
||||||
|
add => closehandlers.Add(value);
|
||||||
|
remove => closehandlers.Remove(value);
|
||||||
|
}
|
||||||
|
public event Action<string> OnMessage
|
||||||
|
{
|
||||||
|
add => messagehandlers.Add(value);
|
||||||
|
remove => messagehandlers.Remove(value);
|
||||||
|
}
|
||||||
|
public event Action<Exception> OnError
|
||||||
|
{
|
||||||
|
add => errorhandlers.Add(value);
|
||||||
|
remove => errorhandlers.Remove(value);
|
||||||
|
}
|
||||||
|
public event Action OnOpen
|
||||||
|
{
|
||||||
|
add => openhandlers.Add(value);
|
||||||
|
remove => closehandlers.Remove(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void Handle(List<Action> handlers)
|
||||||
|
{
|
||||||
|
foreach (var handle in new List<Action>(handlers))
|
||||||
|
handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void Handle<T>(List<Action<T>> handlers, T data)
|
||||||
|
{
|
||||||
|
foreach (var handle in new List<Action<T>>(handlers))
|
||||||
|
handle(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
socket.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Send(string data)
|
||||||
|
{
|
||||||
|
socket.Send(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Connect()
|
||||||
|
{
|
||||||
|
return await socket.OpenAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetEnabledSslProtocols(SslProtocols protocols)
|
||||||
|
{
|
||||||
|
socket.Security.EnabledSslProtocols = protocols;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetProxy(string host, int port)
|
||||||
|
{
|
||||||
|
IPAddress address;
|
||||||
|
socket.Proxy = IPAddress.TryParse(host, out address)
|
||||||
|
? new HttpConnectProxy(new IPEndPoint(address, port))
|
||||||
|
: new HttpConnectProxy(new DnsEndPoint(host, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<PackageId>CryptoExchange.Net</PackageId>
|
<PackageId>CryptoExchange.Net</PackageId>
|
||||||
<Authors>JKorf</Authors>
|
<Authors>JKorf</Authors>
|
||||||
<PackageVersion>0.0.6</PackageVersion>
|
<PackageVersion>0.0.7</PackageVersion>
|
||||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||||
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
|
||||||
<PackageLicenseUrl>https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE</PackageLicenseUrl>
|
<PackageLicenseUrl>https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE</PackageLicenseUrl>
|
||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
|
||||||
|
<PackageReference Include="WebSocket4Net" Version="0.15.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
24
CryptoExchange.Net/Interfaces/IWebsocket.cs
Normal file
24
CryptoExchange.Net/Interfaces/IWebsocket.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Security.Authentication;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net.Interfaces
|
||||||
|
{
|
||||||
|
public interface IWebsocket
|
||||||
|
{
|
||||||
|
void SetEnabledSslProtocols(SslProtocols protocols);
|
||||||
|
|
||||||
|
event Action OnClose;
|
||||||
|
event Action<string> OnMessage;
|
||||||
|
event Action<Exception> OnError;
|
||||||
|
event Action OnOpen;
|
||||||
|
|
||||||
|
bool IsClosed { get; }
|
||||||
|
bool IsOpen { get; }
|
||||||
|
|
||||||
|
Task<bool> Connect();
|
||||||
|
void Send(string data);
|
||||||
|
void Close();
|
||||||
|
void SetProxy(string host, int port);
|
||||||
|
}
|
||||||
|
}
|
7
CryptoExchange.Net/Interfaces/IWebsocketFactory.cs
Normal file
7
CryptoExchange.Net/Interfaces/IWebsocketFactory.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace CryptoExchange.Net.Interfaces
|
||||||
|
{
|
||||||
|
public interface IWebsocketFactory
|
||||||
|
{
|
||||||
|
IWebsocket CreateWebsocket(string url);
|
||||||
|
}
|
||||||
|
}
|
12
CryptoExchange.Net/WebsocketFactory.cs
Normal file
12
CryptoExchange.Net/WebsocketFactory.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using CryptoExchange.Net.Interfaces;
|
||||||
|
|
||||||
|
namespace CryptoExchange.Net
|
||||||
|
{
|
||||||
|
public class WebsocketFactory : IWebsocketFactory
|
||||||
|
{
|
||||||
|
public IWebsocket CreateWebsocket(string url)
|
||||||
|
{
|
||||||
|
return new BaseSocket(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user