@@ -75,7 +75,7 @@ IntPtr errorCallback
7575 ) ;
7676
7777 [ DllImport ( "__Internal" ) ]
78- private static extern int WebSocket_Connect ( string uri , string protocol , string authToken ) ;
78+ private static extern int WebSocket_Connect ( string host , string uri , string protocol , string authToken , IntPtr callbackPtr ) ;
7979
8080 [ DllImport ( "__Internal" ) ]
8181 private static extern int WebSocket_Send ( int socketId , byte [ ] data , int length ) ;
@@ -118,8 +118,15 @@ private static void WebGLOnError(int socketId)
118118 Instance ? . HandleWebGLError ( socketId ) ;
119119 }
120120
121+ [ AOT . MonoPInvokeCallback ( typeof ( Action < int > ) ) ]
122+ private static void OnSocketIdReceived ( int socketId )
123+ {
124+ Instance ? . _socketId . TrySetResult ( socketId ) ;
125+ }
126+
121127 private static WebSocket Instance ;
122128 private int _webglSocketId = - 1 ;
129+ private TaskCompletionSource < int > _socketId ;
123130
124131 private void InitializeWebGL ( )
125132 {
@@ -145,7 +152,10 @@ public async Task Connect(string? auth, string host, string nameOrAddress, Conne
145152 var uri = $ "{ host } /v1/database/{ nameOrAddress } /subscribe?connection_id={ connectionId } &compression={ compression } ";
146153 if ( light ) uri += "&light=true" ;
147154
148- _webglSocketId = WebSocket_Connect ( uri , _options . Protocol , auth ) ;
155+ _socketId = new TaskCompletionSource < int > ( ) ;
156+ var callbackPtr = Marshal . GetFunctionPointerForDelegate ( ( Action < int > ) OnSocketIdReceived ) ;
157+ WebSocket_Connect ( host , uri , _options . Protocol , auth , callbackPtr ) ;
158+ _webglSocketId = await _socketId . Task ;
149159 if ( _webglSocketId == - 1 )
150160 {
151161 dispatchQueue . Enqueue ( ( ) => OnConnectError ? . Invoke (
0 commit comments