Skip to content

Commit

Permalink
Adding Websockets transport
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzyk committed Dec 10, 2015
1 parent 9e5449b commit be8a4ed
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -34,6 +34,5 @@ jquery.signalR.min.js
xamarin/SignalRPackage/component/lib/mobile/
tests/Microsoft.AspNet.SignalR.Client.Store.Tests/AppPackages/
/Microsoft.AspNet.SignalR.sln.ide
<<<<<<< HEAD
/.vs/
.lock.json
project.lock.json
Expand Up @@ -57,7 +57,7 @@
<Link>Properties\CommonVersionInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources.cs" />
<Compile Include="ResourcesStore.cs" />
<Compile Include="Transports\IWebSocketResponse.cs" />
<Compile Include="Transports\MessageReceivedEventArgsWrapper.cs" />
<Compile Include="Transports\WebSocketRequest.cs" />
Expand Down
Expand Up @@ -4,7 +4,7 @@

namespace Microsoft.AspNet.SignalR.Client
{
internal class Resources
internal static class ResourcesStore
{
private static readonly ResourceLoader ResourceLoader =
ResourceLoader.GetForViewIndependentUse("Microsoft.AspNet.SignalR.Client.Store/Resources");
Expand Down
Expand Up @@ -153,12 +153,12 @@ public override Task Send(IConnection connection, string data, string connection
if (connection.State != ConnectionState.Disconnected)
{
// Make this a faulted task and trigger the OnError even to maintain consistency with the HttpBasedTransports
ex = new InvalidOperationException(Resources.GetResourceString("Error_DataCannotBeSentDuringWebSocketReconnect"));
ex = new InvalidOperationException(ResourcesStore.GetResourceString("Error_DataCannotBeSentDuringWebSocketReconnect"));
connection.OnError(ex);
}
else
{
ex = new InvalidOperationException(Resources.GetResourceString("Error_WebSocketUninitialized"));
ex = new InvalidOperationException(ResourcesStore.GetResourceString("Error_DataCannotBeSentDuringWebSocketReconnect"));
}

var tcs = new TaskCompletionSource<object>();
Expand Down
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>Microsoft.AspNet.SignalR.Client</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>
<TargetPlatformVersion>10.0.10586.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down Expand Up @@ -314,12 +314,24 @@
<Compile Include="..\Microsoft.AspNet.SignalR.Core\TaskAsyncHelper.cs">
<Link>Infrastructure\TaskAsyncHelper.cs</Link>
</Compile>
<Compile Include="..\Microsoft.AspNet.SignalR.Client.Store\Transports\IWebSocketResponse.cs">
<Link>Transports\WebSockets\IWebSocketResponse.cs</Link>
</Compile>
<Compile Include="..\Microsoft.AspNet.SignalR.Client.Store\Transports\MessageReceivedEventArgsWrapper.cs">
<Link>Transports\WebSockets\MessageReceivedEventArgsWrapper.cs</Link>
</Compile>
<Compile Include="..\Microsoft.AspNet.SignalR.Client.Store\Transports\WebSocketRequest.cs">
<Link>Transports\WebSockets\WebSocketRequest.cs</Link>
</Compile>
<Compile Include="..\Microsoft.AspNet.SignalR.Client.Store\Transports\WebSocketTransport.cs">
<Link>Transports\WebSockets\WebSocketTransport.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourcesStore.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources.resx">
<Link>Resources.resx</Link>
</EmbeddedResource>
<EmbeddedResource Include="Resources.resx" />
<EmbeddedResource Include="Store.Resources.resx" />
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
Expand All @@ -328,6 +340,7 @@
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<Target Name="BeforeBuild">
<Copy SourceFiles="..\Microsoft.AspNet.SignalR.Client\Resources.resx" DestinationFiles="$(ProjectDir)Resources.resx" SkipUnchangedFiles="true" />
<Copy SourceFiles="..\Microsoft.AspNet.SignalR.Client.Store\Resources.resw" DestinationFiles="$(ProjectDir)Store.Resources.resx" SkipUnchangedFiles="true" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
31 changes: 31 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client.UWP/ResourcesStore.cs
@@ -0,0 +1,31 @@
using System.Globalization;
using System.Reflection;
using System.Resources;

namespace Microsoft.AspNet.SignalR.Client
{
internal static class ResourcesStore
{
private static ResourceManager resourceMan;

internal static ResourceManager ResourceManager
{
get
{
if (ReferenceEquals(resourceMan, null))
{
var assembly = typeof(Resources).GetTypeInfo().Assembly;
var temp = new ResourceManager("Microsoft.AspNet.SignalR.Client.Store.Resources", assembly);
resourceMan = temp;
}

return resourceMan;
}
}

public static string GetResourceString(string resourceName)
{
return ResourceManager.GetString(resourceName, null);
}
}
}
126 changes: 126 additions & 0 deletions src/Microsoft.AspNet.SignalR.Client.UWP/Store.Resources.resx
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Error_WebSocketUninitialized" xml:space="preserve">
<value>The underlying WebSocket is not initialized. Call the 'Start' method to initialize the WebSocket.</value>
</data>
<data name="Error_DataCannotBeSentDuringWebSocketReconnect" xml:space="preserve">
<value>Data cannot be sent because the WebSocket connection is reconnecting.</value>
</data>
</root>
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.SignalR.Client/Http/HttpHelper.cs
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static byte[] ProcessPostData(IDictionary<string, string> postData)
continue;
}

sb.AppendFormat("{0}={1}", pair.Key, UrlEncoder.UrlEncode(pair.Value));
sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}", pair.Key, UrlEncoder.UrlEncode(pair.Value));
}

return Encoding.UTF8.GetBytes(sb.ToString());
Expand Down
Expand Up @@ -27,7 +27,7 @@ public AutoTransport(IHttpClient httpClient)

_transports = new List<IClientTransport>()
{
#if NET45
#if NET45 || WINDOWS_UWP
new WebSocketTransport(httpClient),
#endif
new ServerSentEventsTransport(httpClient),
Expand Down
Expand Up @@ -7,7 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using StoreClientResources = StoreClient::Microsoft.AspNet.SignalR.Client.Resources;
using StoreClientResources = StoreClient::Microsoft.AspNet.SignalR.Client.ResourcesStore;

namespace Microsoft.AspNet.SignalR.Client.Store.Tests
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task WebSocketReconnects()
Assert.True(reconnectingInvoked);
Assert.Equal(ConnectionState.Connected, hubConnection.State);

// TODO: this is a workaround to a race condition in WebSocket.
// TODO: this is a workaround to a race condition in WebSocket.
// Should be removed once the race in WebSockets is fixed
await Task.Delay(200);

Expand Down Expand Up @@ -131,7 +131,7 @@ public async Task WebSocketReconnectsIfConnectionLost()
if (stateChange.OldState == ConnectionState.Connected &&
stateChange.NewState == ConnectionState.Reconnecting)
{
// Reverting quick timeout
// Reverting quick timeout
((IConnection) hubConnection).KeepAliveData = new KeepAliveData(
timeoutWarning: TimeSpan.FromSeconds(30),
timeout: TimeSpan.FromSeconds(20),
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task WebSocketReconnectsIfConnectionLost()

Assert.True(await Task.Run(() => reconnectedWh.Wait(5000)));

// TODO: this is a workaround to a race condition in WebSocket.
// TODO: this is a workaround to a race condition in WebSocket.
// Should be removed once the race in WebSockets is fixed
await Task.Delay(200);

Expand Down
Expand Up @@ -200,7 +200,7 @@ public async Task CannotInvokeSendIfWebSocketUnitialized()
var fakeConnection = new FakeConnection {State = ConnectionState.Disconnected};

Assert.Equal(
StoreClient::Microsoft.AspNet.SignalR.Client.Resources.GetResourceString("Error_WebSocketUninitialized"),
StoreClient::Microsoft.AspNet.SignalR.Client.ResourcesStore.GetResourceString("Error_WebSocketUninitialized"),
(await Assert.ThrowsAsync<InvalidOperationException>(
async () => await new WebSocketTransport().Send(fakeConnection, null, null))).Message);
}
Expand Down

0 comments on commit be8a4ed

Please sign in to comment.