MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / Connect

Method Connect

sdks/csharp/src/WebSocket.cs:149–388  ·  view source on GitHub ↗
(string? auth, string host, string nameOrAddress, ConnectionId connectionId, Compression compression, bool light, bool? confirmedReads)

Source from the content-addressed store, hash-verified

147#endif
148
149 public async Task Connect(string? auth, string host, string nameOrAddress, ConnectionId connectionId, Compression compression, bool light, bool? confirmedReads)
150 {
151#if UNITY_WEBGL && !UNITY_EDITOR
152 if (_isConnecting || _isConnected) return;
153
154 _isConnecting = true;
155 _cancelConnectRequested = false;
156 try
157 {
158 var uri = $"{host}/v1/database/{nameOrAddress}/subscribe?connection_id={connectionId}&compression={compression}";
159 if (light) uri += "&light=true";
160 if (confirmedReads.HasValue)
161 {
162 // Ensure to transmit the bool as lowercase.
163 var enabled = confirmedReads.GetValueOrDefault() ? "true" : "false";
164 uri += $"&confirmed={enabled}";
165 }
166
167 _socketId = new TaskCompletionSource<int>();
168 var callbackPtr = Marshal.GetFunctionPointerForDelegate((Action<int>)OnSocketIdReceived);
169 WebSocket_Connect(host, uri, _options.Protocol, auth, callbackPtr);
170 _webglSocketId = await _socketId.Task;
171 if (_webglSocketId == -1)
172 {
173 dispatchQueue.Enqueue(() => OnConnectError?.Invoke(
174 new Exception("Failed to connect WebSocket")));
175 }
176 else if (_cancelConnectRequested)
177 {
178 // If cancel was requested before open, proactively close now.
179 WebSocket_Close(_webglSocketId, (int)WebSocketCloseStatus.NormalClosure, "Canceled during connect.");
180 }
181 }
182 catch (Exception e)
183 {
184 dispatchQueue.Enqueue(() => OnConnectError?.Invoke(e));
185 }
186 finally
187 {
188 _isConnecting = false;
189 }
190 // Events will be handled via UnitySendMessage callbacks
191#else
192 var uri = $"{host}/v1/database/{nameOrAddress}/subscribe?connection_id={connectionId}&compression={compression}";
193 if (light)
194 {
195 uri += "&light=true";
196 }
197 if (confirmedReads.HasValue)
198 {
199 // Ensure to transmit the bool as lowercase.
200 var enabled = confirmedReads.GetValueOrDefault() ? "true" : "false";
201 uri += $"&confirmed={enabled}";
202 }
203 var url = new Uri(uri);
204 Ws.Options.AddSubProtocol(_options.Protocol);
205
206 _connectCts = new CancellationTokenSource(10000);

Callers

nothing calls this directly

Calls 1

OnConnectErrorFunction · 0.50

Tested by

no test coverage detected