ConnectionStatusSample.cs 4.3 KB

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