-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathMixchWebsocket.cs
68 lines (59 loc) · 1.84 KB
/
MixchWebsocket.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
using System;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using Common;
namespace MixchSitePlugin
{
class MixchWebsocket : IMixchWebsocket
{
private Websocket _websocket;
private readonly ILogger _logger;
public event EventHandler<Packet> Received;
public async Task ReceiveAsync(LiveUrlInfo liveUrlInfo, string userAgent, List<Cookie> cookies)
{
var origin = $"https://mixch.tv";
_websocket = new Websocket();
_websocket.Received += Websocket_Received;
_websocket.Opened += Websocket_Opened;
var url = $"wss://chat.mixch.tv/{liveUrlInfo.Environment}/room/{liveUrlInfo.LiveId}";
await _websocket.ReceiveAsync(url, userAgent, origin);
// 切断後処理
_heartbeatTimer.Enabled = false;
}
private void Websocket_Opened(object sender, EventArgs e)
{
_heartbeatTimer.Enabled = true;
}
private void Websocket_Received(object sender, string e)
{
Packet packet = null;
try
{
packet = JsonConvert.DeserializeObject<Packet>(e);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
if (packet == null)
return;
Received?.Invoke(this, packet);
}
public async Task SendAsync(string s)
{
await _websocket.SendAsync(s);
}
System.Timers.Timer _heartbeatTimer = new System.Timers.Timer();
public MixchWebsocket(ILogger logger)
{
_logger = logger;
}
public void Disconnect()
{
_websocket.Disconnect();
}
}
}