-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebSocket.cs
166 lines (152 loc) · 6.28 KB
/
WebSocket.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CollabClient
{
public class WebSocket
{
// Fields
private ClientWebSocket socket;
private Uri uri;
private string[] protocols;
private bool closeHandled = false;
// Properties
public string Uri => uri.ToString();
public string[] Protocols => protocols.ToArray();
public Dictionary<string,string> Headers { get; set; }
// Events
public event EventHandler<WebSocketCloseEventArgs> OnClose;
public event EventHandler OnOpen;
public event EventHandler<string> OnTextMessage;
public event EventHandler<byte[]> OnBinaryMessage;
// Methods
public WebSocket(string url, params string[] protocols)
{
this.uri = new Uri(url);
this.protocols = protocols;
this.Headers = new Dictionary<string, string>();
// Register dummy handlers on events to prevent NullReferenceException
OnClose += (s, e) => { };
OnOpen += (s, e) => { };
OnTextMessage += (s, e) => { };
OnBinaryMessage += (s, e) => { };
}
public void Close(WebSocketCloseStatus status = WebSocketCloseStatus.NormalClosure, string reason = "")
{
if (this.socket.State != WebSocketState.Open) return;
closeHandled = true;
this.socket.CloseAsync(status, reason, CancellationToken.None).GetAwaiter().GetResult();
this.socket.Dispose();
this.socket = null;
OnClose.Invoke(this, new WebSocketCloseEventArgs
{
Code = status,
Reason = WebSocketCloseReason.CloseByClient,
Message = reason,
});
closeHandled = false;
}
public async Task Connect()
{
if (this.socket != null && this.socket.State == WebSocketState.Open) Close();
this.socket = new ClientWebSocket();
foreach (string protocol in this.protocols) this.socket.Options.AddSubProtocol(protocol);
foreach (KeyValuePair<string, string> header in Headers) this.socket.Options.SetRequestHeader(header.Key, header.Value);
await this.socket.ConnectAsync(this.uri, CancellationToken.None);
messageLoop();
this.OnOpen.Invoke(this, new EventArgs());
}
public bool Send(string msg)
{
return Send(Encoding.UTF8.GetBytes(msg), WebSocketMessageType.Text);
}
public bool Send(byte[] msg, WebSocketMessageType type = WebSocketMessageType.Binary)
{
if (this.socket == null || this.socket.State != WebSocketState.Open) return false;
this.socket.SendAsync(new ArraySegment<byte>(msg), type, true, CancellationToken.None).GetAwaiter().GetResult();
return true;
}
private async void messageLoop()
{
ArraySegment<byte> recieveBuffer = new ArraySegment<byte>(new byte[8192]);
do
{
using (var ms = new MemoryStream())
{
WebSocketReceiveResult res;
do
{
try
{
res = await socket.ReceiveAsync(recieveBuffer, CancellationToken.None);
}
catch (WebSocketException ex)
{
if (this.socket == null || closeHandled) return;
this.socket.Dispose();
this.socket = null;
this.OnClose.Invoke(this, new WebSocketCloseEventArgs
{
Code = null,
Reason = WebSocketCloseReason.Error,
Message = ex.Message,
});
return;
}
if (res.MessageType == WebSocketMessageType.Close)
{
if (this.socket == null || closeHandled) return;
this.socket.Dispose();
this.socket = null;
this.OnClose.Invoke(this, new WebSocketCloseEventArgs
{
Code = res.CloseStatus,
Reason = WebSocketCloseReason.CloseByServer,
Message = res.CloseStatusDescription
});
return;
}
await ms.WriteAsync(recieveBuffer.Array, 0, res.Count);
} while (!res.EndOfMessage);
byte[] msgbytes = ms.ToArray();
if (res.MessageType == WebSocketMessageType.Binary) this.OnBinaryMessage.Invoke(this, msgbytes);
else if (res.MessageType == WebSocketMessageType.Text) this.OnTextMessage.Invoke(this, Encoding.UTF8.GetString(msgbytes));
}
} while (socket.State == WebSocketState.Open);
if (this.socket == null || closeHandled) return;
// this shouldn't happen I don't think but we'll handle it anyway
this.socket.Dispose();
this.socket = null;
this.OnClose.Invoke(this, new WebSocketCloseEventArgs
{
Code = null,
Reason = WebSocketCloseReason.CloseByServer,
Message = ""
});
return;
}
}
public class WebSocketCloseEventArgs
{
/// <summary>
/// Closure code provided by the server, or null if the connection closed uncleanly
/// </summary>
public WebSocketCloseStatus? Code { get; set; }
/// <summary>
/// The reason for the closure
/// </summary>
public WebSocketCloseReason Reason { get; set; }
public string Message { get; set; }
}
public enum WebSocketCloseReason
{
CloseByClient,
CloseByServer,
Error
}
}