Skip to content

Commit

Permalink
feat(WebSocketModule): Adds binary message support to WebSocketModule
Browse files Browse the repository at this point in the history
The WebSocketModule was missing the `sendBinary` ReactMethod used to support binary messages.

Fixes microsoft#1060
  • Loading branch information
rozele committed Mar 10, 2017
1 parent 37ff7c2 commit d0861e8
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions ReactWindows/ReactNative/Modules/WebSocket/WebSocketModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using static System.FormattableString;
using UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding;

namespace ReactNative.Modules.WebSocket
{
Expand Down Expand Up @@ -105,7 +106,20 @@ public void send(string message, int id)
Invariant($"Cannot send a message. Unknown WebSocket id '{id}'."));
}

SendMessageInBackground(id, dataWriter, message);
SendMessageInBackground(id, dataWriter, message, false);
}

[ReactMethod]
public void sendBinary(string message, int id)
{
var dataWriter = default(DataWriter);
if (!_dataWriters.TryGetValue(id, out dataWriter))
{
throw new InvalidOperationException(
Invariant($"Cannot send a message. Unknown WebSocket id '{id}'."));
}

SendMessageInBackground(id, dataWriter, message, true);
}

private async void InitializeInBackground(int id, string url, MessageWebSocket webSocket)
Expand All @@ -130,11 +144,19 @@ private async void InitializeInBackground(int id, string url, MessageWebSocket w
}
}

private async void SendMessageInBackground(int id, DataWriter dataWriter, string message)
private async void SendMessageInBackground(int id, DataWriter dataWriter, string message, bool isBinary)
{
try
{
dataWriter.WriteString(message);
if (isBinary)
{
dataWriter.WriteBytes(Convert.FromBase64String(message));
}
else
{
dataWriter.WriteString(message);
}

await dataWriter.StoreAsync().AsTask().ConfigureAwait(false);
}
catch (Exception ex)
Expand Down

0 comments on commit d0861e8

Please sign in to comment.