This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added WebSocket4Net integration for .NET 4.5
- Loading branch information
Christian Lang
committed
Jul 11, 2013
1 parent
f0f5eb6
commit 5fa002e
Showing
7 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters