Repository navigation
Expand file tree
/
Copy pathWebSocket.cs
More file actions
119 lines (103 loc) · 3.49 KB
/
Copy pathWebSocket.cs
File metadata and controls
119 lines (103 loc) · 3.49 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ShareInvest.Coinone.EventHandler;
using ShareInvest.Crypto;
using System.Net.WebSockets;
using System.Text;
namespace ShareInvest.Coinone;
public class WebSocket : ShareWebSocket<TickerEventArgs>
{
public WebSocket() : base("stream.coinone.co.kr")
{
}
public async Task RequestPingAsync()
{
while (WebSocketState.Open == Socket.State)
{
await base.RequestAsync(JsonConvert.SerializeObject(new
{
request_type = "PING"
}));
await Task.Delay(TimeSpan.FromMinutes(0x10));
}
}
/// <summary>웹소켓 요청 방법 topic: quote_currency, target_currency 등의 정보를 JSON 형식으로 입력</summary>
/// <param name="requestType">실행할 요청 종류: SUBSCRIBE / UNSUBSCRIBE / PING</param>
/// <param name="channel">구독 채널명: ORDERBOOK / TICKER / TRADE</param>
/// <param name="quoteCurrency">마켓 기준 통화</param>
/// <param name="targetCurrency">조회 요청할 종목</param>
/// <param name="format">단축 변수명으로 구독 원하는 경우 SHORT 입력(기본값 : DEFAULT)</param>
public async Task RequestAsync(string requestType, string channel, string quoteCurrency, string targetCurrency, string? format = null)
{
object value;
if (string.IsNullOrEmpty(format))
{
value = new
{
request_type = requestType,
topic = new
{
quote_currency = quoteCurrency,
target_currency = targetCurrency
},
channel
};
}
else
{
value = new
{
request_type = requestType,
topic = new
{
quote_currency = quoteCurrency,
target_currency = targetCurrency
},
format,
channel
};
}
await base.RequestAsync(JsonConvert.SerializeObject(value));
}
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];
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);
switch (jToken.Value<string>("response_type"))
{
case "DATA" when Convert.ToString(jToken["data"]) is string json:
OnReceiveTicker(json);
continue;
}
Console.WriteLine(new
{
CryptoExchange = nameof(Coinone),
DateTime.Now,
Response = jToken
});
}
Console.WriteLine(new
{
CryptoExchange = nameof(Coinone),
DateTime.Now,
Socket = Socket.State
});
}
public override async Task ConnectAsync(string? token = null, TimeSpan? interval = null)
{
await base.ConnectAsync(token, interval: interval ?? TimeSpan.FromMinutes(0x10));
}
readonly CancellationTokenSource cts = new();
}