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

Commit

Permalink
Initial checkin of library.
Browse files Browse the repository at this point in the history
  • Loading branch information
vegardlarsen committed Jan 16, 2013
1 parent 483b0f1 commit 87d3afa
Show file tree
Hide file tree
Showing 65 changed files with 4,373 additions and 4 deletions.
30 changes: 30 additions & 0 deletions Pusher.Connections.WindowsStore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using System.Runtime.CompilerServices;
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.WindowsStore")]
[assembly: AssemblyDescription("Pusher.com client websocket implementation for Windows Store")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Digital Creations AS")]
[assembly: AssemblyProduct("Pusher.Connections.WindowsStore")]
[assembly: AssemblyCopyright("Copyright © 2013 Digital Creation AS")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 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")]
[assembly: AssemblyInformationalVersion("0.0.1.0-prerelease")]
[assembly: ComVisible(false)]
124 changes: 124 additions & 0 deletions Pusher.Connections.WindowsStore/Pusher.Connections.WindowsStore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?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>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{831A6A92-DA04-4255-AE60-00E307A65DD8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Pusher.Connections.WindowsStore</RootNamespace>
<AssemblyName>Pusher.Connections.WindowsStore</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</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;NETFX_CORE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<ProjectReference Include="..\Pusher\Pusher.csproj">
<Project>{FF212BEC-10AC-49EC-9E57-164CD1CAD02E}</Project>
<Name>Pusher</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSocket.cs" />
<Compile Include="WebsocketConnectionFactory.cs" />
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '11.0' ">
<VisualStudioVersion>11.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.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>
121 changes: 121 additions & 0 deletions Pusher.Connections.WindowsStore/WebSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Pusher.Events;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;

namespace Pusher.Connections.WindowsStore
{
public class WebSocket : IConnection, IDisposable
{
private MessageWebSocket _socket;
private DataWriter _messageWriter;
private readonly Uri _endpoint;
private ConnectionState _connectionState;
public WebSocket(Uri endpoint)
{
_endpoint = endpoint;
SetupSocket();
}

private void SetupSocket()
{
_socket = new MessageWebSocket();
_socket.Control.MessageType = SocketMessageType.Utf8;
_socket.Closed += OnSocketClosed;
_socket.MessageReceived += OnMessageReceived;
}

private async void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
if (OnData == null) return;

var exceptionOccured = false;
try
{
var reader = args.GetDataReader();
var text = reader.ReadString(reader.UnconsumedBufferLength);
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(IWebSocket sender, WebSocketClosedEventArgs args)
{
_messageWriter = null;
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;
await _socket.ConnectAsync(_endpoint);
_messageWriter = new DataWriter(_socket.OutputStream);
_connectionState = ConnectionState.Connected;
if (OnOpen != null)
{
OnOpen(this, new EventArgs());
}
}

public async Task SendMessage(string data)
{
if (_messageWriter == null)
{
await Open();
}
Debug.Assert(_messageWriter != null);

_messageWriter.WriteString(data);
await _messageWriter.StoreAsync();
}

public event EventHandler<EventArgs> OnClose;
public event EventHandler<EventArgs> OnOpen;
public event EventHandler<DataReceivedEventArgs> OnData;

#endregion

#region Implementation of IDisposable

public void Dispose()
{
_socket.Dispose();
}

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

namespace Pusher.Connections.WindowsStore
{
public class WebsocketConnectionFactory : IConnectionFactory
{
public IConnection Create(Uri endpoint)
{
return new WebSocket(endpoint);
}
}
}
21 changes: 21 additions & 0 deletions Pusher.Samples.WindowsStore/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Application x:Class="Pusher.Samples.WindowsStore.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Pusher.Samples.WindowsStore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Pusher.Samples.WindowsStore.ViewModel" mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
</Application>
Loading

0 comments on commit 87d3afa

Please sign in to comment.