diff --git a/.nuget/NuGet.Config b/.nuget/NuGet.Config new file mode 100644 index 0000000..6a318ad --- /dev/null +++ b/.nuget/NuGet.Config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.nuget/NuGet.exe b/.nuget/NuGet.exe new file mode 100644 index 0000000..c296edf Binary files /dev/null and b/.nuget/NuGet.exe differ diff --git a/.nuget/NuGet.targets b/.nuget/NuGet.targets new file mode 100644 index 0000000..d41ddfb --- /dev/null +++ b/.nuget/NuGet.targets @@ -0,0 +1,133 @@ + + + + $(MSBuildProjectDirectory)\..\ + + + false + + + false + + + true + + + false + + + + + + + + + + + $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) + $([System.IO.Path]::Combine($(ProjectDir), "packages.config")) + + + + + $(SolutionDir).nuget + packages.config + + + + + $(NuGetToolsPath)\NuGet.exe + @(PackageSource) + + "$(NuGetExePath)" + mono --runtime=v4.0.30319 $(NuGetExePath) + + $(TargetDir.Trim('\\')) + + -RequireConsent + -NonInteractive + + + $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir "$(SolutionDir) " + $(NuGetCommand) pack "$(ProjectPath)" -Properties Configuration=$(Configuration) $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols + + + + RestorePackages; + $(BuildDependsOn); + + + + + $(BuildDependsOn); + BuildPackage; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Pusher.Connections.Net/Properties/AssemblyInfo.cs b/Pusher.Connections.Net/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..155550e --- /dev/null +++ b/Pusher.Connections.Net/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Pusher.Connections.Net")] +[assembly: AssemblyDescription("Pusher.com client websocket implementation for .NET 4.5")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("6 Wunderkinder GmbH")] +[assembly: AssemblyProduct("Pusher.Connections.Net")] +[assembly: AssemblyCopyright("Copyright © 6 Wunderkinder GmbH 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("fadeb0c2-a863-455f-a56b-98c6e7d2b7d5")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("0.0.1.0")] +[assembly: AssemblyFileVersion("0.0.1.0")] diff --git a/Pusher.Connections.Net/Pusher.Connections.Net.csproj b/Pusher.Connections.Net/Pusher.Connections.Net.csproj new file mode 100644 index 0000000..3f30ea5 --- /dev/null +++ b/Pusher.Connections.Net/Pusher.Connections.Net.csproj @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {082677AB-028A-45D7-BC05-0DF98245A963} + Library + Properties + Pusher.Connections.Net + Pusher.Connections.Net + v4.5 + 512 + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + ..\packages\WebSocket4Net.0.8\lib\net40\WebSocket4Net.dll + + + + + + + + + + {ff212bec-10ac-49ec-9e57-164cd1cad02e} + Pusher + + + + + + + + + \ No newline at end of file diff --git a/Pusher.Connections.Net/WebSocketConnection.cs b/Pusher.Connections.Net/WebSocketConnection.cs new file mode 100644 index 0000000..a5185f7 --- /dev/null +++ b/Pusher.Connections.Net/WebSocketConnection.cs @@ -0,0 +1,106 @@ +using System; +using System.Threading.Tasks; +using WebSocket4Net; +using DataReceivedEventArgs = Pusher.Events.DataReceivedEventArgs; + +namespace Pusher.Connections.Net +{ + public class WebSocketConnection : IConnection + { + private WebSocket _socket; + private readonly Uri _endpoint; + private ConnectionState _connectionState; + + public WebSocketConnection(Uri endpoint) + { + _endpoint = endpoint; + SetupSocket(); + } + + private void SetupSocket() + { + _socket = new WebSocket(_endpoint.ToString()); + _socket.Closed += OnSocketClosed; + _socket.MessageReceived += OnMessageReceived; + } + + private async void OnMessageReceived(object sender, MessageReceivedEventArgs args) + { + if (OnData == null) return; + + var exceptionOccured = false; + try + { + var text = args.Message; + OnData(sender, new DataReceivedEventArgs { TextData = text }); + } + catch (Exception) + { + exceptionOccured = true; + } + // cannot await in catch + if (exceptionOccured) await Reconnect(); + } + + private async Task Reconnect() + { + if (_connectionState == ConnectionState.Connecting || _connectionState == ConnectionState.Connected) return; + + SetupSocket(); + await Open(); + } + + private async void OnSocketClosed(object sender, EventArgs args) + { + if (_connectionState != ConnectionState.Disconnecting) + { + await Reconnect(); + } + _connectionState = ConnectionState.Disconnected; + if (OnClose != null) OnClose(sender, new EventArgs()); + } + + #region Implementation of IConnection + + public void Close() + { + _connectionState = ConnectionState.Disconnecting; + _socket.Close(1000, "Close requested"); + _connectionState = ConnectionState.Disconnected; + } + + public async Task Open() + { + if (_connectionState == ConnectionState.Connected) + { + Close(); + SetupSocket(); + } + + _connectionState = ConnectionState.Connecting; + _socket.Open(); + + _connectionState = ConnectionState.Connected; + if (OnOpen != null) + { + OnOpen(this, new EventArgs()); + } + } + + public async Task SendMessage(string data) + { + if (_connectionState != ConnectionState.Connected) + { + await Open(); + } + + _socket.Send(data); + } + + public event EventHandler OnClose; + public event EventHandler OnOpen; + public event EventHandler OnData; + + #endregion + } +} diff --git a/Pusher.Connections.Net/WebsocketConnectionFactory.cs b/Pusher.Connections.Net/WebsocketConnectionFactory.cs new file mode 100644 index 0000000..7054a79 --- /dev/null +++ b/Pusher.Connections.Net/WebsocketConnectionFactory.cs @@ -0,0 +1,12 @@ +using System; + +namespace Pusher.Connections.Net +{ + public class WebsocketConnectionFactory : IConnectionFactory + { + public IConnection Create(Uri endpoint) + { + return new WebSocketConnection(endpoint); + } + } +} diff --git a/Pusher.Connections.Net/packages.config b/Pusher.Connections.Net/packages.config new file mode 100644 index 0000000..4468ab4 --- /dev/null +++ b/Pusher.Connections.Net/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Pusher.Connections.WindowsStore/Pusher.Connections.WindowsStore.csproj b/Pusher.Connections.WindowsStore/Pusher.Connections.WindowsStore.csproj index 26f4b65..3b9b18e 100644 --- a/Pusher.Connections.WindowsStore/Pusher.Connections.WindowsStore.csproj +++ b/Pusher.Connections.WindowsStore/Pusher.Connections.WindowsStore.csproj @@ -14,6 +14,8 @@ en-US 512 {BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\ + true true @@ -114,6 +116,7 @@ 11.0 +