Skip to content

Commit 28fe1a5

Browse files
authored
Issue96 workaround web socket (#117)
* Fix Issue96 * move to WebSocketsModule * Add fix reference * Update WebServer.cs * Update WebServer.cs * Update WebSocketsModule.cs * Versioning
1 parent 1ba3e9b commit 28fe1a5

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Swan;
1212
#if NET47
1313
using System.Net.WebSockets;
14+
using System.Text.RegularExpressions;
1415
#else
1516
using Net;
1617
#endif
@@ -28,13 +29,25 @@ public class WebSocketsModule : WebModuleBase
2829
private readonly Dictionary<string, WebSocketsServer> _serverMap =
2930
new Dictionary<string, WebSocketsServer>(StringComparer.OrdinalIgnoreCase);
3031

32+
#if NETSTANDARD2_0
33+
private readonly Regex splitter = new Regex(@"(\s|[,;])+");
34+
#endif
35+
3136
/// <summary>
3237
/// Initializes a new instance of the <see cref="WebSocketsModule"/> class.
3338
/// </summary>
3439
public WebSocketsModule()
3540
{
3641
AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, async (context, ct) =>
3742
{
43+
#if NETSTANDARD2_0
44+
// Support for Firefox https://github.com/dotnet/corefx/issues/24550#issuecomment-338048691
45+
var connectionValues = context.Request.Headers.GetValues("Connection");
46+
context.Request.Headers.Remove("Connection");
47+
var headers = connectionValues.Select(tk => splitter.Split(tk)).First();
48+
headers.ToList().ForEach(value => context.Request.Headers.Add("Connection", value));
49+
#endif
50+
3851
// check if it is a WebSocket request (this only works with Win8 and Windows 2012)
3952
if (context.Request.IsWebSocketRequest == false)
4053
return false;
@@ -59,7 +72,7 @@ public WebSocketsModule()
5972
/// The name.
6073
/// </value>
6174
public override string Name => nameof(WebSocketsModule).Humanize();
62-
75+
6376
/// <summary>
6477
/// Registers the web sockets server given a WebSocketsServer Type.
6578
/// </summary>
@@ -93,7 +106,7 @@ public void RegisterWebSocketsServer(Type socketType)
93106
nameof(socketType));
94107
}
95108

96-
_serverMap[attribute.Path] = (WebSocketsServer) Activator.CreateInstance(socketType);
109+
_serverMap[attribute.Path] = (WebSocketsServer)Activator.CreateInstance(socketType);
97110
}
98111

99112
/// <summary>
@@ -196,7 +209,7 @@ public ReadOnlyCollection<WebSocketContext> WebSockets
196209
/// The name of the server.
197210
/// </value>
198211
public abstract string ServerName { get; }
199-
212+
200213
/// <summary>
201214
/// Gets the Encoding used to use the Send method to send a string. The default is UTF8 per the WebSocket specification.
202215
/// </summary>
@@ -258,7 +271,7 @@ await context.AcceptWebSocketAsync(
258271
try
259272
{
260273
#if NET47
261-
// define a receive buffer
274+
// define a receive buffer
262275
var receiveBuffer = new byte[receiveBufferSize];
263276

264277
// define a dynamic buffer that holds multi-part receptions
@@ -472,9 +485,9 @@ protected abstract void OnFrameReceived(
472485
/// </summary>
473486
/// <param name="context">The context.</param>
474487
/// <param name="localEndPoint">The local endpoint.</param>
475-
/// /// <param name="remoteEndPoint">The remote endpoint.</param>
488+
/// <param name="remoteEndPoint">The remote endpoint.</param>
476489
protected abstract void OnClientConnected(
477-
WebSocketContext context,
490+
WebSocketContext context,
478491
System.Net.IPEndPoint localEndPoint,
479492
System.Net.IPEndPoint remoteEndPoint);
480493
#else

src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);portable-net45+win8+wpa81+wp8</PackageTargetFallback>
1313
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
1414
<DebugType>Full</DebugType>
15-
<Version>1.11.1</Version>
15+
<Version>1.11.3</Version>
1616
<Product>EmbedIO</Product>
1717
<Company>Unosquare</Company>
1818
<PackageLicenseUrl>https://raw.githubusercontent.com/unosquare/embedio/master/LICENSE</PackageLicenseUrl>

src/Unosquare.Labs.EmbedIO/WebServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,4 @@ private async Task HandleClientRequest(HttpListenerContext context, Cancellation
436436
}
437437
}
438438
}
439-
}
439+
}

0 commit comments

Comments
 (0)