HostMode.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // host mode related helper functions.
  2. // usually they set up both server & client.
  3. // it's cleaner to keep them in one place, instead of only in server / client.
  4. using System;
  5. namespace Mirror
  6. {
  7. public static class HostMode
  8. {
  9. // keep the local connections setup in one function.
  10. // makes host setup easier to follow.
  11. internal static void SetupConnections()
  12. {
  13. // create local connections pair, both are connected
  14. Utils.CreateLocalConnections(
  15. out LocalConnectionToClient connectionToClient,
  16. out LocalConnectionToServer connectionToServer);
  17. // set client connection
  18. NetworkClient.connection = connectionToServer;
  19. // set server connection
  20. NetworkServer.SetLocalConnection(connectionToClient);
  21. }
  22. // call OnConnected on server & client.
  23. // public because NetworkClient.ConnectLocalServer was public before too.
  24. public static void InvokeOnConnected()
  25. {
  26. // call server OnConnected with server's connection to client
  27. NetworkServer.OnConnected(NetworkServer.localConnection);
  28. // call client OnConnected with client's connection to server
  29. // => previously we used to send a ConnectMessage to
  30. // NetworkServer.localConnection. this would queue the message
  31. // until NetworkClient.Update processes it.
  32. // => invoking the client's OnConnected event directly here makes
  33. // tests fail. so let's do it exactly the same order as before by
  34. // queueing the event for next Update!
  35. //OnConnectedEvent?.Invoke(connection);
  36. ((LocalConnectionToServer)NetworkClient.connection).QueueConnectedEvent();
  37. }
  38. }
  39. }