Skip to content

Commit

Permalink
Issue96 workaround web socket (#117)
Browse files Browse the repository at this point in the history
* Fix Issue96

* move to WebSocketsModule

* Add fix reference

* Update WebServer.cs

* Update WebServer.cs

* Update WebSocketsModule.cs

* Versioning
  • Loading branch information
geoperez authored Nov 29, 2017
1 parent 1ba3e9b commit 28fe1a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Swan;
#if NET47
using System.Net.WebSockets;
using System.Text.RegularExpressions;
#else
using Net;
#endif
Expand All @@ -28,13 +29,25 @@ public class WebSocketsModule : WebModuleBase
private readonly Dictionary<string, WebSocketsServer> _serverMap =
new Dictionary<string, WebSocketsServer>(StringComparer.OrdinalIgnoreCase);

#if NETSTANDARD2_0
private readonly Regex splitter = new Regex(@"(\s|[,;])+");
#endif

/// <summary>
/// Initializes a new instance of the <see cref="WebSocketsModule"/> class.
/// </summary>
public WebSocketsModule()
{
AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, async (context, ct) =>
{
#if NETSTANDARD2_0
// Support for Firefox https://github.com/dotnet/corefx/issues/24550#issuecomment-338048691
var connectionValues = context.Request.Headers.GetValues("Connection");
context.Request.Headers.Remove("Connection");
var headers = connectionValues.Select(tk => splitter.Split(tk)).First();
headers.ToList().ForEach(value => context.Request.Headers.Add("Connection", value));
#endif
// check if it is a WebSocket request (this only works with Win8 and Windows 2012)
if (context.Request.IsWebSocketRequest == false)
return false;
Expand All @@ -59,7 +72,7 @@ public WebSocketsModule()
/// The name.
/// </value>
public override string Name => nameof(WebSocketsModule).Humanize();

/// <summary>
/// Registers the web sockets server given a WebSocketsServer Type.
/// </summary>
Expand Down Expand Up @@ -93,7 +106,7 @@ public void RegisterWebSocketsServer(Type socketType)
nameof(socketType));
}

_serverMap[attribute.Path] = (WebSocketsServer) Activator.CreateInstance(socketType);
_serverMap[attribute.Path] = (WebSocketsServer)Activator.CreateInstance(socketType);
}

/// <summary>
Expand Down Expand Up @@ -196,7 +209,7 @@ public ReadOnlyCollection<WebSocketContext> WebSockets
/// The name of the server.
/// </value>
public abstract string ServerName { get; }

/// <summary>
/// Gets the Encoding used to use the Send method to send a string. The default is UTF8 per the WebSocket specification.
/// </summary>
Expand Down Expand Up @@ -258,7 +271,7 @@ await context.AcceptWebSocketAsync(
try
{
#if NET47
// define a receive buffer
// define a receive buffer
var receiveBuffer = new byte[receiveBufferSize];

// define a dynamic buffer that holds multi-part receptions
Expand Down Expand Up @@ -472,9 +485,9 @@ protected abstract void OnFrameReceived(
/// </summary>
/// <param name="context">The context.</param>
/// <param name="localEndPoint">The local endpoint.</param>
/// /// <param name="remoteEndPoint">The remote endpoint.</param>
/// <param name="remoteEndPoint">The remote endpoint.</param>
protected abstract void OnClientConnected(
WebSocketContext context,
WebSocketContext context,
System.Net.IPEndPoint localEndPoint,
System.Net.IPEndPoint remoteEndPoint);
#else
Expand Down
2 changes: 1 addition & 1 deletion src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);portable-net45+win8+wpa81+wp8</PackageTargetFallback>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<DebugType>Full</DebugType>
<Version>1.11.1</Version>
<Version>1.11.3</Version>
<Product>EmbedIO</Product>
<Company>Unosquare</Company>
<PackageLicenseUrl>https://raw.githubusercontent.com/unosquare/embedio/master/LICENSE</PackageLicenseUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/Unosquare.Labs.EmbedIO/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,4 @@ private async Task HandleClientRequest(HttpListenerContext context, Cancellation
}
}
}
}
}

0 comments on commit 28fe1a5

Please sign in to comment.