Skip to content

Commit 6e6649a

Browse files
HellslicerKévin Poirot
authored and
Kévin Poirot
committed
Upgrade WebSocketListener
1 parent ef58cc6 commit 6e6649a

File tree

6 files changed

+28
-37
lines changed

6 files changed

+28
-37
lines changed

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
1515
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
1616
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
17-
[assembly: AssemblyVersion("0.2.0")]
17+
[assembly: AssemblyVersion("0.2.1")]
1818
// The following attributes are used to specify the signing key for the assembly,
1919
// if desired. See the Mono documentation for more information about signing.
2020
//[assembly: AssemblyDelaySign(false)]

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ This library exposes a simple WebSocket server into FXServer.
88
* Text messages
99
* Authorization header support
1010

11-
## Prerequisites
12-
13-
* `System.Web.dll`
14-
* `System.Security.dll`
15-
* `System.ServiceModel.dll`
16-
* `System.IdentityModel.dll`
17-
1811
## Configuration
1912

2013
Convars available:
@@ -45,7 +38,7 @@ TriggerEvent("WebSocketServer:broadcast", "This message will be broadcasted to a
4538

4639
## Built With
4740

48-
* [vtortola/WebSocketListener](https://github.com/vtortola/WebSocketListener)
41+
* [deniszykov/WebSocketListener](https://github.com/deniszykov/WebSocketListener)
4942

5043
## License
5144

WebSocketEventListener.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ public class WebSocketEventListener : IDisposable
2727

2828
public WebSocketEventListener(IPEndPoint endpoint, WebSocketListenerOptions options)
2929
{
30+
options.Standards.RegisterRfc6455();
3031
_listener = new WebSocketListener(endpoint, options);
31-
_listener.Standards.RegisterStandard(new WebSocketFactoryRfc6455(_listener));
3232
}
3333

3434
public void Start()
3535
{
36-
_listener.Start();
36+
_listener.StartAsync().Wait();
3737
}
3838

3939
public void Stop()
4040
{
41-
_listener.Stop();
41+
_listener.StopAsync().Wait();
4242
}
4343

4444
public async Task ListenAsync()

WebSocketServer.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Net;
55
using System.Threading;
6+
using System.Threading.Tasks;
67
using System.Collections.Generic;
78
using vtortola.WebSockets;
89

@@ -16,6 +17,24 @@ private enum LogLevels { Debug, Info, Warn, Error };
1617
private string authorization;
1718
private List<WebSocket> webSockets;
1819

20+
private async Task<bool> CheckHttpHeaders(WebSocketHttpRequest request, WebSocketHttpResponse response)
21+
{
22+
await Task.Run(() =>
23+
{
24+
if (authorization.Length > 0)
25+
{
26+
var authHeader = request.Headers["Authorization"];
27+
if (authHeader == null || authHeader != authorization)
28+
{
29+
response.Status = HttpStatusCode.Unauthorized;
30+
Log("Rejected Authorization header: " + authHeader, LogLevels.Debug);
31+
}
32+
}
33+
});
34+
35+
return true;
36+
}
37+
1938
public WebSocketServer()
2039
{
2140
logLevel = Function.Call<string>(Hash.GET_CONVAR, "websocket_debug", "false") == "true" ? LogLevels.Debug : LogLevels.Info;
@@ -47,18 +66,7 @@ public WebSocketServer()
4766

4867
var server = new WebSocketEventListener (endpoint, new WebSocketListenerOptions () {
4968
SubProtocols = new String[]{ "text" },
50-
OnHttpNegotiation = (request, response) =>
51-
{
52-
if (authorization.Length > 0)
53-
{
54-
var authHeader = request.Headers["Authorization"];
55-
if (authHeader == null || authHeader != authorization)
56-
{
57-
response.Status = HttpStatusCode.Unauthorized;
58-
Log("Rejected Authorization header: " + authHeader, LogLevels.Debug);
59-
}
60-
}
61-
}
69+
HttpAuthenticationHandler = CheckHttpHeaders
6270
});
6371
server.OnConnect += (ws) =>
6472
{

WebSocketServer.csproj

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,8 @@
4141
<Reference Include="Microsoft.CSharp">
4242
<HintPath>bin\Debug\Microsoft.CSharp.dll</HintPath>
4343
</Reference>
44-
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.24.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45-
<HintPath>packages\Microsoft.Tpl.Dataflow.4.5.24\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll</HintPath>
46-
</Reference>
47-
<Reference Include="vtortola.WebSockets, Version=2.2.3.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
48-
<HintPath>packages\vtortola.WebSocketListener.2.2.3.0\lib\net45\vtortola.WebSockets.dll</HintPath>
49-
</Reference>
50-
<Reference Include="vtortola.WebSockets.Deflate, Version=2.2.3.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
51-
<HintPath>packages\vtortola.WebSocketListener.2.2.3.0\lib\net45\vtortola.WebSockets.Deflate.dll</HintPath>
52-
</Reference>
53-
<Reference Include="vtortola.WebSockets.Rfc6455, Version=2.2.3.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
54-
<HintPath>packages\vtortola.WebSocketListener.2.2.3.0\lib\net45\vtortola.WebSockets.Rfc6455.dll</HintPath>
44+
<Reference Include="deniszykov.WebSocketListener">
45+
<HintPath>packages\deniszykov.WebSocketListener.4.0.0\lib\net45\deniszykov.WebSocketListener.dll</HintPath>
5546
</Reference>
5647
</ItemGroup>
5748
<ItemGroup>

packages.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net452" />
4-
<package id="vtortola.WebSocketListener" version="2.2.3.0" targetFramework="net452" />
3+
<package id="deniszykov.WebSocketListener" version="4.0.0" targetFramework="net452" />
54
</packages>

0 commit comments

Comments
 (0)