Skip to content

Commit 6a88791

Browse files
committed
feat(extension): Managed
1 parent 0a6bd9a commit 6a88791

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

documentation/docs/ws/connecttoserver.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"function": "ConnectToServer",
44
"description": "Creates a WebSocket Client connection.",
55
"arguments": {
6+
"dns": "string",
67
"port": "uint32",
8+
"isSecure": "bool",
9+
"path": "string",
710
"callback": {
811
"arguments": {
912
"connection_uuid": "string",

managed/Websockets.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using SwiftlyS2.Internal_API;
2+
3+
namespace SwiftlyS2.API.Extensions
4+
{
5+
public class WebSockets
6+
{
7+
private static Dictionary<string, Action<string, ulong, string, string>> _servercallbacks = [];
8+
private static Dictionary<string, Action<string, string, string>> _clientcallbacks = [];
9+
10+
private static IntPtr _ctx = IntPtr.Zero;
11+
12+
private static void InitializeContext()
13+
{
14+
if (_ctx != IntPtr.Zero) return;
15+
_ctx = Invoker.CallNative<IntPtr>("WS", "WS", CallKind.ClassFunction);
16+
17+
Scripting.Events.AddEventHandler("OnWSServerMessage", (Scripting.Events.Event e, string server_uuid, ulong client_id, string kind, string message) =>
18+
{
19+
if (_servercallbacks.ContainsKey(server_uuid))
20+
{
21+
_servercallbacks[server_uuid](server_uuid, client_id, kind, message);
22+
}
23+
});
24+
25+
Scripting.Events.AddEventHandler("OnWSServerMessage", (Scripting.Events.Event e, string connection_uuid, string kind, string message) =>
26+
{
27+
if (_clientcallbacks.ContainsKey(connection_uuid))
28+
{
29+
_clientcallbacks[connection_uuid](connection_uuid, kind, message);
30+
}
31+
});
32+
}
33+
34+
public static string StartServer(int port, Action<string, ulong, string, string> callback)
35+
{
36+
InitializeContext();
37+
38+
var callbackUUID = Guid.NewGuid().ToString();
39+
while (_servercallbacks.ContainsKey(callbackUUID))
40+
{
41+
callbackUUID = Guid.NewGuid().ToString();
42+
}
43+
44+
_servercallbacks.Add(callbackUUID, callback);
45+
46+
Internal_API.Invoker.CallNative("WS", "StartServer", Internal_API.CallKind.ClassFunction, _ctx, port, callbackUUID);
47+
return callbackUUID;
48+
}
49+
50+
public static void StopServer(string server_uuid)
51+
{
52+
InitializeContext();
53+
Internal_API.Invoker.CallNative("WS", "StopServer", Internal_API.CallKind.ClassFunction, _ctx, server_uuid);
54+
if(_servercallbacks.ContainsKey(server_uuid)) _servercallbacks.Remove(server_uuid);
55+
}
56+
57+
public static void SendServerMessageToClient(string server_uuid, ulong client_id, string message)
58+
{
59+
InitializeContext();
60+
Internal_API.Invoker.CallNative("WS", "SendServerMessageToClient", Internal_API.CallKind.ClassFunction, _ctx, server_uuid, client_id, message);
61+
}
62+
63+
public static void SendServerMessageToAllClients(string server_uuid, string message)
64+
{
65+
InitializeContext();
66+
Internal_API.Invoker.CallNative("WS", "SendServerMessageToAllClients", Internal_API.CallKind.ClassFunction, _ctx, server_uuid, message);
67+
}
68+
69+
public static void TerminateClientConnectionOnServer(string server_uuid, ulong client_id)
70+
{
71+
InitializeContext();
72+
Internal_API.Invoker.CallNative("WS", "TerminateClientConnectionOnServer", Internal_API.CallKind.ClassFunction, _ctx, server_uuid, client_id);
73+
}
74+
public static string ConnectToServer(string dns, int port, bool isSecure, string path, Action<string, string, string> callback)
75+
{
76+
InitializeContext();
77+
78+
string connection = Internal_API.Invoker.CallNative<string>("WS", "ConnectToServer", Internal_API.CallKind.ClassFunction, _ctx, dns, port, isSecure, path) ?? "";
79+
_clientcallbacks.Add(connection, callback);
80+
return connection;
81+
}
82+
public static void StopConnectionToServer(string connection_uuid)
83+
{
84+
InitializeContext();
85+
86+
Internal_API.Invoker.CallNative("WS", "DisconnectFromServer", Internal_API.CallKind.ClassFunction, _ctx, connection_uuid);
87+
if(_clientcallbacks.ContainsKey(connection_uuid)) _clientcallbacks.Remove(connection_uuid);
88+
}
89+
90+
public static void SendMessageToServer(string connection_uuid, string message)
91+
{
92+
InitializeContext();
93+
Internal_API.Invoker.CallNative("WS", "SendMessageToServer", Internal_API.CallKind.ClassFunction, _ctx, connection_uuid, message);
94+
}
95+
}
96+
}

src/entrypoint.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ bool WSExtension::OnPluginLoad(std::string pluginName, void* pluginState, Plugin
258258

259259
ADD_CLASS("WS");
260260

261+
ADD_CLASS_FUNCTION("WS", "WS", [](FunctionContext* context, ClassData* data) -> void {});
262+
261263
ADD_CLASS_FUNCTION("WS", "~WS", [](FunctionContext* context, ClassData* data) -> void {
262264
if (data->HasData("server_uuids")) {
263265
auto vec = data->GetData<std::vector<std::string>>("server_uuids");

0 commit comments

Comments
 (0)