Repository navigation
Expand file tree
/
Copy pathWebSocket.cs
More file actions
103 lines (87 loc) · 2.65 KB
/
Copy pathWebSocket.cs
File metadata and controls
103 lines (87 loc) · 2.65 KB
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ShareInvest.Bithumb.EventHandler;
using ShareInvest.Crypto;
using System.Diagnostics;
using System.Net.WebSockets;
using System.Text;
namespace ShareInvest.Bithumb;
public class WebSocket : ShareWebSocket<TickerEventArgs>
{
public WebSocket() : base("pubwss.bithumb.com/pub/ws")
{
}
/// <summary>
/// symbols: BTC_KRW, ETH_KRW, …
/// type: 구독 메시지 종류("ticker" / "transaction" / "orderbookdepth")
/// (optional)tickTypes: tick 종류 ("30M"/"1H"/"12H"/"24H"/"MID")
/// </summary>
public async Task RequestAsync(string type, IEnumerable<string> symbols, params string[] tickTypes)
{
await base.RequestAsync(JsonConvert.SerializeObject(new
{
type,
symbols,
tickTypes
}));
}
public async Task RequestAsync(string type, params string[] symbols)
{
await base.RequestAsync(JsonConvert.SerializeObject(new
{
type,
symbols
}));
}
public override async Task RequestAsync(string json)
{
await base.RequestAsync(json);
}
public override async Task ReceiveAsync()
{
while (WebSocketState.Open == Socket.State)
{
var buffer = new byte[0x400];
try
{
var res = await Socket.ReceiveAsync(new ArraySegment<byte>(buffer), cts.Token);
var str = Encoding.UTF8.GetString(buffer, 0, res.Count);
if (string.IsNullOrEmpty(str))
{
continue;
}
var jToken = JToken.Parse(str);
if (string.IsNullOrEmpty(jToken.Value<string>("status")))
{
OnReceiveTicker(str);
continue;
}
Console.WriteLine(new
{
CryptoExchange = nameof(Bithumb),
DateTime.Now,
Response = jToken
});
}
catch (Exception exception)
{
#if DEBUG
Debug.WriteLine(exception);
#else
Console.WriteLine(exception);
#endif
}
}
Console.WriteLine(new
{
CryptoExchange = nameof(Bithumb),
DateTime.Now,
Socket = Socket.State
});
}
public override async Task ConnectAsync(string? token = null, TimeSpan? interval = null)
{
await base.ConnectAsync(token, interval: interval ?? TimeSpan.FromMilliseconds(0xFFFFFFFE));
}
readonly CancellationTokenSource cts = new();
}