ConnectionStatusSample.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. using System;
  3. using UnityEngine;
  4. using BestHTTP.SignalR;
  5. using BestHTTP.SignalR.Hubs;
  6. using BestHTTP.Examples;
  7. sealed class ConnectionStatusSample : MonoBehaviour
  8. {
  9. readonly Uri URI = new Uri("https://besthttpsignalr.azurewebsites.net/signalr");
  10. /// <summary>
  11. /// Reference to the SignalR Connection
  12. /// </summary>
  13. Connection signalRConnection;
  14. GUIMessageList messages = new GUIMessageList();
  15. #region Unity Events
  16. void Start()
  17. {
  18. // Connect to the StatusHub hub
  19. signalRConnection = new Connection(URI, "StatusHub");
  20. // General events
  21. signalRConnection.OnNonHubMessage += signalRConnection_OnNonHubMessage;
  22. signalRConnection.OnError += signalRConnection_OnError;
  23. signalRConnection.OnStateChanged += signalRConnection_OnStateChanged;
  24. // SaveLocal up a Callback for Hub events
  25. signalRConnection["StatusHub"].OnMethodCall += statusHub_OnMethodCall;
  26. // Connect to the server
  27. signalRConnection.Open();
  28. }
  29. void OnDestroy()
  30. {
  31. // Close the connection when we are closing the sample
  32. signalRConnection.Close();
  33. }
  34. void OnGUI()
  35. {
  36. GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
  37. {
  38. GUILayout.BeginHorizontal();
  39. if (GUILayout.Button("START") && signalRConnection.State != ConnectionStates.Connected)
  40. signalRConnection.Open();
  41. if (GUILayout.Button("STOP") && signalRConnection.State == ConnectionStates.Connected)
  42. {
  43. signalRConnection.Close();
  44. messages.Clear();
  45. }
  46. if (GUILayout.Button("PING") && signalRConnection.State == ConnectionStates.Connected)
  47. {
  48. // Call a Hub-method on the server.
  49. signalRConnection["StatusHub"].Call("Ping");
  50. }
  51. GUILayout.EndHorizontal();
  52. GUILayout.Space(20);
  53. GUILayout.Label("Connection Status Messages");
  54. GUILayout.BeginHorizontal();
  55. GUILayout.Space(20);
  56. messages.Draw(Screen.width - 20, 0);
  57. GUILayout.EndHorizontal();
  58. });
  59. }
  60. #endregion
  61. #region SignalR Events
  62. /// <summary>
  63. /// Called on server-sent non-hub messages.
  64. /// </summary>
  65. void signalRConnection_OnNonHubMessage(Connection manager, object data)
  66. {
  67. messages.Add("[Server Message] " + data.ToString());
  68. }
  69. /// <summary>
  70. /// Called when the SignalR Connection's state changes.
  71. /// </summary>
  72. void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
  73. {
  74. messages.Add(string.Format("[State Change] {0} => {1}", oldState, newState));
  75. }
  76. /// <summary>
  77. /// Called when an error occures. The plugin may close the connection after this event.
  78. /// </summary>
  79. void signalRConnection_OnError(Connection manager, string error)
  80. {
  81. messages.Add("[Error] " + error);
  82. }
  83. /// <summary>
  84. /// Called when the "StatusHub" hub wants to call a method on this client.
  85. /// </summary>
  86. void statusHub_OnMethodCall(Hub hub, string method, params object[] args)
  87. {
  88. string id = args.Length > 0 ? args[0] as string : string.Empty;
  89. string when = args.Length > 1 ? args[1].ToString() : string.Empty;
  90. switch (method)
  91. {
  92. case "joined":
  93. messages.Add(string.Format("[{0}] {1} joined at {2}", hub.Name, id, when));
  94. break;
  95. case "rejoined":
  96. messages.Add(string.Format("[{0}] {1} reconnected at {2}", hub.Name, id, when));
  97. break;
  98. case "leave":
  99. messages.Add(string.Format("[{0}] {1} leaved at {2}", hub.Name, id, when));
  100. break;
  101. default: // pong
  102. messages.Add(string.Format("[{0}] {1}", hub.Name, method));
  103. break;
  104. }
  105. }
  106. #endregion
  107. }
  108. #endif