Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Added WebSocket4Net integration for .NET 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Lang committed Jul 11, 2013
1 parent f0f5eb6 commit 5fa002e
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Pusher.Connections.Net/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
65 changes: 65 additions & 0 deletions Pusher.Connections.Net/Pusher.Connections.Net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{082677AB-028A-45D7-BC05-0DF98245A963}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Pusher.Connections.Net</RootNamespace>
<AssemblyName>Pusher.Connections.Net</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="WebSocket4Net">
<HintPath>..\packages\WebSocket4Net.0.8\lib\net40\WebSocket4Net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSocketConnection.cs" />
<Compile Include="WebsocketConnectionFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pusher\Pusher.csproj">
<Project>{ff212bec-10ac-49ec-9e57-164cd1cad02e}</Project>
<Name>Pusher</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- 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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
106 changes: 106 additions & 0 deletions Pusher.Connections.Net/WebSocketConnection.cs
Original file line number Diff line number Diff line change
@@ -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<EventArgs> OnClose;
public event EventHandler<EventArgs> OnOpen;
public event EventHandler<DataReceivedEventArgs> OnData;

#endregion
}
}
12 changes: 12 additions & 0 deletions Pusher.Connections.Net/WebsocketConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Pusher.Connections.Net
{
public class WebsocketConnectionFactory : IConnectionFactory
{
public IConnection Create(Uri endpoint)
{
return new WebSocketConnection(endpoint);
}
}
}
4 changes: 4 additions & 0 deletions Pusher.Connections.Net/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="WebSocket4Net" version="0.8" targetFramework="net45" />
</packages>
3 changes: 2 additions & 1 deletion Pusher.Connections.WindowsStore/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class WebSocket : IConnection, IDisposable
private MessageWebSocket _socket;
private DataWriter _messageWriter;
private readonly Uri _endpoint;
private ConnectionState _connectionState;
private ConnectionState _connectionState;

public WebSocket(Uri endpoint)
{
_endpoint = endpoint;
Expand Down
13 changes: 13 additions & 0 deletions Pusher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{808380
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pusher.Connections.Net", "Pusher.Connections.Net\Pusher.Connections.Net.csproj", "{082677AB-028A-45D7-BC05-0DF98245A963}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -113,12 +115,23 @@ Global
{56E18157-CBC2-4E0B-8EA2-8EC7634DB8C5}.Release|x86.ActiveCfg = Release|x86
{56E18157-CBC2-4E0B-8EA2-8EC7634DB8C5}.Release|x86.Build.0 = Release|x86
{56E18157-CBC2-4E0B-8EA2-8EC7634DB8C5}.Release|x86.Deploy.0 = Release|x86
{082677AB-028A-45D7-BC05-0DF98245A963}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Debug|Any CPU.Build.0 = Debug|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Debug|ARM.ActiveCfg = Debug|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Debug|x64.ActiveCfg = Debug|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Debug|x86.ActiveCfg = Debug|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Release|Any CPU.ActiveCfg = Release|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Release|Any CPU.Build.0 = Release|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Release|ARM.ActiveCfg = Release|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Release|x64.ActiveCfg = Release|Any CPU
{082677AB-028A-45D7-BC05-0DF98245A963}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{831A6A92-DA04-4255-AE60-00E307A65DD8} = {773CAD5C-12EF-4B84-8C27-267B2B7E658D}
{082677AB-028A-45D7-BC05-0DF98245A963} = {773CAD5C-12EF-4B84-8C27-267B2B7E658D}
{D6C44C74-38BA-41D0-85AF-198F509B72E0} = {C5DA1EB3-4BF9-4E65-9756-CB3825597095}
{56E18157-CBC2-4E0B-8EA2-8EC7634DB8C5} = {082A47ED-0510-48EC-9E4D-CE5DE5908E7D}
EndGlobalSection
Expand Down

0 comments on commit 5fa002e

Please sign in to comment.