Skip to content

Commit 1904b92

Browse files
author
Elad Zelingher
committed
Adding binary connection to WampSharp.Windows
1 parent c6c73d4 commit 1904b92

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/net45/Default/WampSharp.Windows/WAMP2/V2/Fluent/MessageWebSocketActivator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ private IControlledWampConnection<TMessage> GetConnectionFactory<TMessage>(IWamp
3535
return CreateTextConnection(textBinding);
3636
}
3737

38+
IWampBinaryBinding<TMessage> binaryBinding = binding as IWampBinaryBinding<TMessage>;
39+
40+
if (binaryBinding != null)
41+
{
42+
return CreateBinaryConnection(binaryBinding);
43+
}
44+
3845
throw new Exception();
3946
}
4047

4148
protected IControlledWampConnection<TMessage> CreateTextConnection<TMessage>(IWampTextBinding<TMessage> textBinding)
4249
{
4350
return new MessageWebSocketTextConnection<TMessage>(mServerAddress, textBinding);
4451
}
52+
53+
protected IControlledWampConnection<TMessage> CreateBinaryConnection<TMessage>(IWampBinaryBinding<TMessage> binaryBinding)
54+
{
55+
return new MessageWebSocketBinaryConnection<TMessage>(mServerAddress, binaryBinding);
56+
}
4557
}
4658
}
4759

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#if PCL
2+
using System;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using WampSharp.V2.Binding;
6+
using Windows.Networking.Sockets;
7+
using WampSharp.Core.Message;
8+
9+
namespace WampSharp.Windows
10+
{
11+
public class MessageWebSocketBinaryConnection<TMessage> : MessageWebSocketConnection<TMessage>
12+
{
13+
private readonly IWampBinaryBinding<TMessage> mBinaryBinding;
14+
15+
public MessageWebSocketBinaryConnection(string uri, IWampBinaryBinding<TMessage> binding) :
16+
base(uri, binding, SocketMessageType.Binary)
17+
{
18+
mBinaryBinding = binding;
19+
}
20+
21+
// Regarding the async modifier - see https://github.com/Code-Sharp/WampSharp/issues/122
22+
protected override async void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
23+
{
24+
try
25+
{
26+
Stream stream = args.GetDataStream().AsStreamForRead();
27+
28+
MemoryStream memoryStream = new MemoryStream();
29+
30+
await stream.CopyToAsync(memoryStream);
31+
32+
stream.Position = 0;
33+
34+
WampMessage<TMessage> message = mBinaryBinding.Parse(stream);
35+
36+
RaiseMessageArrived(message);
37+
}
38+
catch (Exception ex)
39+
{
40+
RaiseConnectionError(ex);
41+
42+
if (mWebSocket != null)
43+
{
44+
mWebSocket.Dispose();
45+
}
46+
}
47+
}
48+
49+
protected override async Task SendAsync(WampMessage<object> message)
50+
{
51+
try
52+
{
53+
Stream stream = mWebSocket.OutputStream.AsStreamForWrite();
54+
55+
byte[] frame = mBinaryBinding.Format(message);
56+
57+
await stream.WriteAsync(frame, 0, frame.Length).ConfigureAwait(false);
58+
59+
await stream.FlushAsync().ConfigureAwait(false);
60+
}
61+
catch (Exception ex)
62+
{
63+
RaiseConnectionError(ex);
64+
65+
if (mWebSocket != null)
66+
{
67+
mWebSocket.Dispose();
68+
}
69+
70+
throw;
71+
}
72+
}
73+
}
74+
}
75+
#endif

src/pcl/Default/WampSharp.Windows/WampSharp.Windows.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<Compile Include="..\..\..\net45\Default\WampSharp.Windows\Windows\MessageWebSocketTextConnection.cs">
6666
<Link>Windows\MessageWebSocketTextConnection.cs</Link>
6767
</Compile>
68+
<Compile Include="..\..\..\net45\Default\WampSharp.Windows\Windows\MessageWebSocketBinaryConnection.cs">
69+
<Link>Windows\MessageWebSocketBinaryConnection.cs</Link>
70+
</Compile>
6871
</ItemGroup>
6972
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
7073
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

0 commit comments

Comments
 (0)