Skip to content

Commit 13a45ab

Browse files
committed
Started implementing websockets
1 parent f6fbaab commit 13a45ab

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

AngleSharp.Io/AngleSharp.Io.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="ConfigurationExtensions.cs" />
50+
<Compile Include="Dom\WebSocketReadyState.cs" />
5051
<Compile Include="Extensions\GeneralExtensions.cs" />
52+
<Compile Include="Dom\IWebSocket.cs" />
5153
<Compile Include="Network\HttpClientRequester.cs" />
5254
<Compile Include="Network\Response.cs" />
5355
<Compile Include="Properties\AssemblyInfo.cs" />

AngleSharp.Io/Dom/IWebSocket.cs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
namespace AngleSharp.Io.Dom
2+
{
3+
using AngleSharp.Attributes;
4+
using AngleSharp.Dom;
5+
using System;
6+
using System.IO;
7+
using System.Linq;
8+
9+
/// <summary>
10+
/// Represents the WebSocket interface. For more information see:
11+
/// http://www.w3.org/TR/2011/WD-websockets-20110419/#the-websocket-interface
12+
/// </summary>
13+
[DomName("WebSocket")]
14+
public class WebSocket : EventTarget, IDisposable
15+
{
16+
#region Fields
17+
18+
readonly Url _url;
19+
readonly MemoryStream _buffered;
20+
String _protocol;
21+
WebSocketReadyState _state;
22+
23+
#endregion
24+
25+
#region Events
26+
27+
[DomName("onopen")]
28+
public event DomEventHandler Opened
29+
{
30+
add { AddEventListener("open", value, false); }
31+
remove { RemoveEventListener("open", value, false); }
32+
}
33+
34+
[DomName("onmessage")]
35+
public event DomEventHandler Message
36+
{
37+
add { AddEventListener("message", value, false); }
38+
remove { RemoveEventListener("message", value, false); }
39+
}
40+
41+
[DomName("onerror")]
42+
public event DomEventHandler Error
43+
{
44+
add { AddEventListener("error", value, false); }
45+
remove { RemoveEventListener("error", value, false); }
46+
}
47+
48+
[DomName("onclose")]
49+
public event DomEventHandler Closed
50+
{
51+
add { AddEventListener("close", value, false); }
52+
remove { RemoveEventListener("close", value, false); }
53+
}
54+
55+
#endregion
56+
57+
#region ctor
58+
59+
/// <summary>
60+
/// Creates a new WebSocket instance.
61+
/// </summary>
62+
/// <param name="url">The URL to connect to.</param>
63+
/// <param name="protocols">The protocols to allow.</param>
64+
[DomConstructor]
65+
public WebSocket(String url, params String[] protocols)
66+
{
67+
_url = new Url(url);
68+
_protocol = String.Empty;
69+
_state = WebSocketReadyState.Connecting;
70+
_buffered = new MemoryStream();
71+
72+
if (_url.IsInvalid || _url.IsRelative)
73+
throw new DomException(DomError.Syntax);
74+
75+
var invalid = protocols.Length - protocols.Distinct().Where(IsValid).Count();
76+
77+
if (invalid > 0)
78+
throw new DomException(DomError.Syntax);
79+
}
80+
81+
#endregion
82+
83+
#region Properties
84+
85+
/// <summary>
86+
/// Gets the URL the connection is made to.
87+
/// </summary>
88+
[DomName("url")]
89+
public String Url
90+
{
91+
get { return _url.Href; }
92+
}
93+
94+
/// <summary>
95+
/// Gets the current state of the connection.
96+
/// </summary>
97+
[DomName("readyState")]
98+
public WebSocketReadyState ReadyState
99+
{
100+
get { return _state; }
101+
}
102+
103+
/// <summary>
104+
/// Gets the number of bytes of UTF-8 text that have been queued using
105+
/// Send() but that, as of the last time the event loop started
106+
/// executing a task, had not yet been transmitted to the network.
107+
/// </summary>
108+
[DomName("bufferedAmount")]
109+
public Int64 Buffered
110+
{
111+
get { return _buffered.Length; }
112+
}
113+
114+
/// <summary>
115+
/// Gets the chosen protocol for the connection.
116+
/// </summary>
117+
[DomName("protocol")]
118+
public String Protocol
119+
{
120+
get { return _protocol; }
121+
}
122+
123+
#endregion
124+
125+
#region Methods
126+
127+
/// <summary>
128+
/// Transmits data using the connection.
129+
/// </summary>
130+
/// <param name="data">The data to transmit.</param>
131+
[DomName("send")]
132+
public void Send(String data)
133+
{
134+
//TODO
135+
}
136+
137+
/// <summary>
138+
/// Closes the WebSocket connection, if open. Otherwise aborts.
139+
/// </summary>
140+
[DomName("close")]
141+
public void Close()
142+
{
143+
//TODO
144+
}
145+
146+
void IDisposable.Dispose()
147+
{
148+
Close();
149+
}
150+
151+
#endregion
152+
153+
#region Helpers
154+
155+
static Boolean IsValid(String protocol)
156+
{
157+
for (int i = 0; i < protocol.Length; i++)
158+
{
159+
if (protocol[i] < 0x21 || protocol[i] > 0x7e)
160+
return false;
161+
}
162+
163+
return true;
164+
}
165+
166+
#endregion
167+
}
168+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace AngleSharp.Io.Dom
2+
{
3+
using AngleSharp.Attributes;
4+
5+
/// <summary>
6+
/// Represents the state of the connection.
7+
/// </summary>
8+
[DomName("WebSocket")]
9+
public enum WebSocketReadyState : ushort
10+
{
11+
/// <summary>
12+
/// The connection has not yet been established.
13+
/// </summary>
14+
[DomName("CONNECTING")]
15+
Connecting = 0,
16+
/// <summary>
17+
/// The connection is established and communication is possible.
18+
/// </summary>
19+
[DomName("OPEN")]
20+
Open = 1,
21+
/// <summary>
22+
/// The connection is going through the closing handshake.
23+
/// </summary>
24+
[DomName("CLOSING")]
25+
Closing = 2,
26+
/// <summary>
27+
/// The connection has been closed or could not be opened.
28+
/// </summary>
29+
[DomName("CLOSED")]
30+
Closed = 3
31+
}
32+
}

0 commit comments

Comments
 (0)