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
+ }
0 commit comments