Skip to content

Commit

Permalink
fix(walletconnect): handle server error messages in payloads
Browse files Browse the repository at this point in the history
Also sets default ping-pong to infinity (timespan = 0)

fix #173
  • Loading branch information
jasonboukheir committed Dec 20, 2022
1 parent d3dc173 commit 4b4b4e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
8 changes: 0 additions & 8 deletions Algorand.Unity.Package/Assets/Resources.meta

This file was deleted.

5 changes: 5 additions & 0 deletions Runtime/Algorand.Unity.WalletConnect/JsonRpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net.WebSockets;
using System.Threading;
using Algorand.Unity.Json;
using Algorand.Unity.WebSocket;
using Cysharp.Threading.Tasks;
using UnityEngine;
Expand Down Expand Up @@ -188,6 +189,10 @@ private async UniTaskVoid PollJsonRpcMessages(CancellationToken cancellationToke
Debug.LogWarning(inner.ToString());
throw;
}
catch (JsonReadException ex)
{
Debug.LogWarning($"Response from JsonRpcServer was not Json:\n\"{System.Text.Encoding.UTF8.GetString(evt.Payload)}\"\nError:\n{ex}");
}
break;
case WebSocketEventType.Error:
Debug.LogWarning(evt.Error);
Expand Down
25 changes: 17 additions & 8 deletions Runtime/Algorand.Unity.WebSocket/NativeWebSocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;

namespace Algorand.Unity.WebSocket
{
Expand All @@ -12,6 +14,7 @@ public class NativeWebSocketClient : IWebSocketClient

private ClientWebSocket Connection;
private readonly Uri uri;
private readonly ArraySegment<byte> buffer;
public Queue<WebSocketEvent> EventQueue { get; } = new Queue<WebSocketEvent>();

public WebSocketState ReadyState => Connection?.State ?? WebSocketState.Closed;
Expand All @@ -20,6 +23,8 @@ public NativeWebSocketClient(string url)
{
uri = new Uri(url);
Connection = new ClientWebSocket();
Connection.Options.KeepAliveInterval = TimeSpan.Zero;
buffer = System.Net.WebSockets.WebSocket.CreateClientBuffer(8 * 1024, 8 * 1024);
}

public void Connect()
Expand Down Expand Up @@ -48,23 +53,27 @@ private async UniTaskVoid ConnectAsync()
{
await Connection.ConnectAsync(uri, CancellationToken.None);
OnOpen();
var buffer = new byte[1024 * 4];
try
{
while (true)
{
var result = await Connection.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (Connection.CloseStatus.HasValue)
using var ms = new MemoryStream();
WebSocketReceiveResult result;
do
{
result = await Connection.ReceiveAsync(buffer, CancellationToken.None);
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);

ms.Seek(0, SeekOrigin.Begin);

if (Connection.CloseStatus.HasValue || result.CloseStatus.HasValue)
{
OnClose();
break;
}
var payload = new byte[result.Count];
for (var i = 0; i < payload.Length; i++)
{
payload[i] = buffer[i];
}

var payload = ms.ToArray();
OnMessage(payload);
}
}
Expand Down

0 comments on commit 4b4b4e1

Please sign in to comment.