|
| 1 | +# Rejecting Client Connections |
| 2 | + |
| 3 | +SpacetimeDB provides a way to disconnect a client during a client connection attempt. |
| 4 | + |
| 5 | +:::server-rust |
| 6 | +In Rust, if we returned and error (or a panic) during the `client_connected` reducer, the client will be disconnected. |
| 7 | + |
| 8 | +Here is a simple example where the server module throws an error for all incoming client connections. |
| 9 | +```rust |
| 10 | +#[reducer(client_connected)] |
| 11 | +pub fn client_connected(_ctx: &ReducerContext) -> Result<(), String> { |
| 12 | + let client_is_rejected = true; |
| 13 | + if client_is_rejected { |
| 14 | + Err("The client connection was rejected. With our current code logic, all clients will be rejected.".to_string()) |
| 15 | + } else { |
| 16 | + Ok(()) |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Client behavior can vary by client type. For example: |
| 22 | +* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: |
| 23 | + `Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` |
| 24 | + |
| 25 | +* **Rust clients**: Client disconnection behavior is currently undefined and will generate an error reading: |
| 26 | + `Unable to send subscribe message: WS sender loop has dropped its recv channel: TrySendError { kind: Disconnected }` |
| 27 | + |
| 28 | +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. |
| 29 | + |
| 30 | +Regardless of the client type, from the rust server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: |
| 31 | +`ERROR: : The client connection was rejected. With our current code logic, all clients will be rejected.` |
| 32 | +::: |
| 33 | +:::server-csharp |
| 34 | +In C#, if we throw an exception during the `ClientConnected` reducer, the client will be disconnected. |
| 35 | + |
| 36 | +Here is a simple example where the server module throws an error for all incoming client connections. |
| 37 | +```csharp |
| 38 | +[Reducer(ReducerKind.ClientConnected)] |
| 39 | +// Called when a client connects to a SpacetimeDB database server |
| 40 | +public static void ClientConnected(ReducerContext ctx) |
| 41 | +{ |
| 42 | + throw new Exception("The client connection was rejected. With our current code logic, all clients will be rejected."); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Client behavior can vary by client type. For example: |
| 47 | +* **C# clients**: Client disconnection behavior is currently undefined and will generate an error reading: |
| 48 | +`Disconnected abnormally: System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.` |
| 49 | + |
| 50 | +* **Rust clients**: Client will receive an `on_disconnected` event with no error message. |
| 51 | + |
| 52 | +* **TypeScript clients**: Client will receive an `Error connecting to SpacetimeDB:` and a `CloseEvent` with a code of 1006. |
| 53 | + |
| 54 | +Regardless of the client type, from the C# server's perspective, the client will be disconnected and the server module's logs will contain an entry reading: |
| 55 | +`ERROR: : System.Exception: The client connection was rejected. With our current code logic, all clients will be rejected.` |
| 56 | +::: |
0 commit comments