You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

connections.md 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ---
  2. title: Managing Connections
  3. ---
  4. In Discord.Net, once a client has been started, it will automatically
  5. maintain a connection to Discord's gateway, until it is manually
  6. stopped.
  7. ### Usage
  8. To start a connection, invoke the `StartAsync` method on a client that
  9. supports a WebSocket connection.
  10. To end a connection, invoke the `StopAsync` method. This will
  11. gracefully close any open WebSocket or UdpSocket connections.
  12. Since the Start/Stop methods only signal to an underlying connection
  13. manager that a connection needs to be started, **they return before a
  14. connection is actually made.**
  15. As a result, you will need to hook into one of the connection-state
  16. based events to have an accurate representation of when a client is
  17. ready for use.
  18. All clients provide a `Connected` and `Disconnected` event, which is
  19. raised respectively when a connection opens or closes. In the case of
  20. the DiscordSocketClient, this does **not** mean that the client is
  21. ready to be used.
  22. A separate event, `Ready`, is provided on DiscordSocketClient, which
  23. is raised only when the client has finished guild stream or guild
  24. sync, and has a complete guild cache.
  25. [DiscordSocketClient]: xref:Discord.WebSocket.DiscordSocketClient
  26. [DiscordRpcClient]: xref:Discord.Rpc.DiscordRpcClient
  27. ### Samples
  28. [!code-csharp[Connection Sample](samples/events.cs)]
  29. ### Tips
  30. Avoid running long-running code on the gateway! If you deadlock the
  31. gateway (as explained in [events]), the connection manager will be
  32. unable to recover and reconnect.
  33. Assuming the client disconnected because of a fault on Discord's end,
  34. and not a deadlock on your end, we will always attempt to reconnect
  35. and resume a connection.
  36. Don't worry about trying to maintain your own connections, the
  37. connection manager is designed to be bulletproof and never fail - if
  38. your client doesn't manage to reconnect, you've found a bug!
  39. [events]: events.md