Adapt WS to use Task

This commit is contained in:
Alessandro Proto 2023-03-17 21:00:20 +01:00
parent 9fadf6d064
commit 4a9d2bad34
2 changed files with 16 additions and 32 deletions

View file

@ -315,19 +315,14 @@ public class HTTP : IComponent
L.Pop(1);
}
var clientTask = TaskMeta.Push(L, WebSocketClient.ObjectType);
var connectTask = wsClient.ConnectAsync(uri, CancellationToken.None);
connectTask.ContinueWith(async task =>
{
if (task.IsFaulted || task.IsCanceled)
{
_game.LuaRuntime.QueueEvent("websocket_failure", LK =>
{
LK.PushInteger(requestId);
LK.PushString(task.Exception?.Message);
return 2;
});
clientTask.Reject(task.Exception?.Message);
return;
}
@ -336,14 +331,10 @@ public class HTTP : IComponent
var handle = new WebSocketClient.Client(wsClient, requestId);
WebSocketConnections.Add(handle);
_game.LuaRuntime.QueueEvent("websocket_connect", LK =>
clientTask.Fulfill(LK =>
{
LK.PushInteger(requestId);
ObjectManager.PushObject(LK, handle);
LK.SetMetaTable(WebSocketClient.ObjectType);
return 2;
});
var buffer = new byte[4096];

View file

@ -29,21 +29,6 @@ public class WebSocketClient : IComponent
public record Client(ClientWebSocket Socket, long RequestId);
public void LuaInit(Lua L)
{
CreateMeta(L);
}
public static void CreateMeta(Lua L)
{
L.NewMetaTable(ObjectType);
L.SetFuncs(MetaMethods, 0);
L.NewLibTable(Methods);
L.SetFuncs(Methods, 0);
L.SetField(-2, "__index");
L.Pop(1);
}
internal static LuaRegister[] Methods = new LuaRegister[]
{
new()
@ -89,12 +74,20 @@ public class WebSocketClient : IComponent
new(),
};
private static readonly Dictionary<string, LuaFunction> functions = new()
public void LuaInit(Lua L)
{
["getRequestID"] = L_GetRequestId,
["send"] = L_Send,
["closeAsync"] = L_CloseAsync,
};
CreateMeta(L);
}
public static void CreateMeta(Lua L)
{
L.NewMetaTable(ObjectType);
L.SetFuncs(MetaMethods, 0);
L.NewLibTable(Methods);
L.SetFuncs(Methods, 0);
L.SetField(-2, "__index");
L.Pop(1);
}
public static Client ToObject(Lua L, bool gc = false)
{