Skip to content

Commit

Permalink
make WebSocketOpen non-async
Browse files Browse the repository at this point in the history
  • Loading branch information
austinmilt committed Dec 1, 2024
1 parent eac754a commit 54df3c0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
54 changes: 44 additions & 10 deletions src/BizHawk.Client.Common/Api/ClientWebSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,62 @@ public Task Close(
return task;
}

/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
public async Task Receive(int bufferSize, int maxMessages)
public async Task Connect(int bufferSize, int maxMessages)
{
_w ??= new();
if ((_w != null) && (_w.State != WebSocketState.Open))
{
await _w.ConnectAsync(_uri, CancellationToken.None);
await Receive(bufferSize, maxMessages);
}
}

/// <summary>opens a connection to the configured server and passes messages to [consumer]</summary>
public async Task Connect(Action<string> consumer, int bufferSize = 1024)
{
_w ??= new();
if ((_w != null) && (_w.State != WebSocketState.Open))
{
await _w.ConnectAsync(_uri, CancellationToken.None);
}

var buffer = new ArraySegment<byte>(new byte[bufferSize]);
while ((_w != null) && (_w.State == WebSocketState.Open))
{
WebSocketReceiveResult result;
result = await _w.ReceiveAsync(buffer, CancellationToken.None);
if (maxMessages == 0 || _receivedMessages.Count < maxMessages)
{
_receivedMessages.Enqueue(Encoding.UTF8.GetString(buffer.Array, 0, result.Count));
}
var result = await _w.ReceiveAsync(buffer, CancellationToken.None);
string message = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
consumer(message);
}
}

public async Task Connect(int bufferSize, int maxMessages)
/// <summary>opens a connection to the configured server and passes messages to [consumer]</summary>
public async Task Connect(Action<byte[]> consumer, int bufferSize = 2048)
{
_w ??= new();
if ((_w != null) && (_w.State != WebSocketState.Open))
{
await _w.ConnectAsync(_uri, CancellationToken.None);
await Receive(bufferSize, maxMessages);
}

var buffer = new ArraySegment<byte>(new byte[bufferSize]);
while ((_w != null) && (_w.State == WebSocketState.Open))
{
_ = await _w.ReceiveAsync(buffer, CancellationToken.None);
consumer(buffer.Array);
}
}

/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
public async Task Receive(int bufferSize, int maxMessages)
{
var buffer = new ArraySegment<byte>(new byte[bufferSize]);
while ((_w != null) && (_w.State == WebSocketState.Open))
{
var result = await _w.ReceiveAsync(buffer, CancellationToken.None);
if (maxMessages == 0 || _receivedMessages.Count < maxMessages)
{
_receivedMessages.Enqueue(Encoding.UTF8.GetString(buffer.Array, 0, result.Count));
}
}
}

Expand Down
21 changes: 9 additions & 12 deletions src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using NLua;

namespace BizHawk.Client.Common
Expand Down Expand Up @@ -254,25 +253,23 @@ private void CheckHttp()
}
}

[LuaMethod("ws_open", "Opens a websocket and returns the id so that it can be retrieved later. If an id is provided, reconnects to the ")]
[LuaMethod("ws_open", "Opens a websocket and returns the id so that it can be retrieved later. If an id is provided, reconnects to the server")]
[LuaMethodExample("local ws_id = comm.ws_open(\"wss://echo.websocket.org\");")]
public async Task<string> WebSocketOpen(string uri, string guid = null, int bufferSize = 1024, int maxMessages = 20)
public string WebSocketOpen(
string uri,
string guid = null,
int bufferSize = 1024,
int maxMessages = 20)
{
var wsServer = APIs.Comm.WebSockets;
if (wsServer == null)
{
return null;
}
var localGuid = guid == null ? Guid.NewGuid() : Guid.Parse(guid);
if (guid == null)
{
_websockets[localGuid] = wsServer.Open(new Uri(uri));
await _websockets[localGuid].Connect(bufferSize, maxMessages);
}
else
{
await _websockets[localGuid].Connect(bufferSize, maxMessages);
}

_websockets[localGuid] ??= wsServer.Open(new Uri(uri));
_websockets[localGuid].Connect(bufferSize, maxMessages).Wait(500);
return localGuid.ToString();
}

Expand Down

0 comments on commit 54df3c0

Please sign in to comment.