HubWithAuthorization.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if !BESTHTTP_DISABLE_SIGNALR_CORE
  2. using BestHTTP.Examples;
  3. using BestHTTP.SignalRCore;
  4. using BestHTTP.SignalRCore.Encoders;
  5. using System;
  6. using UnityEngine;
  7. namespace BestHTTP.Examples
  8. {
  9. /// <summary>
  10. /// A sample to demonstrate Bearer token authorization on the server. The client will connect to the /redirect route
  11. /// where it will receive the token and will receive the new url (/HubWithAuthorization) to connect to.
  12. /// HubWithAuthorization without the token would throw an error.
  13. /// </summary>
  14. public sealed class HubWithAuthorizationSample : MonoBehaviour
  15. {
  16. // Server uri to connect to
  17. readonly Uri URI = new Uri(GUIHelper.BaseURL + "/redirect");
  18. // Instance of the HubConnection
  19. HubConnection hub;
  20. Vector2 scrollPos;
  21. string uiText;
  22. void Start()
  23. {
  24. // Server side of this example can be found here:
  25. // https://github.com/Benedicht/BestHTTP_DemoSite/blob/master/BestHTTP_DemoSite/Hubs/
  26. // Crete the HubConnection
  27. hub = new HubConnection(URI, new JsonProtocol(new LitJsonEncoder()));
  28. // Subscribe to hub events
  29. hub.OnConnected += Hub_OnConnected;
  30. hub.OnError += Hub_OnError;
  31. hub.OnClosed += Hub_OnClosed;
  32. hub.OnMessage += Hub_OnMessage;
  33. // And finally start to connect to the server
  34. hub.StartConnect();
  35. uiText = "StartConnect called\n";
  36. }
  37. void OnDestroy()
  38. {
  39. if (hub != null)
  40. hub.StartClose();
  41. }
  42. // Draw the text stored in the 'uiText' field
  43. void OnGUI()
  44. {
  45. GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
  46. {
  47. scrollPos = GUILayout.BeginScrollView(scrollPos, false, false);
  48. GUILayout.BeginVertical();
  49. GUILayout.Label(uiText);
  50. GUILayout.EndVertical();
  51. GUILayout.EndScrollView();
  52. });
  53. }
  54. /// <summary>
  55. /// This callback is called when the plugin is connected to the server successfully. Messages can be sent to the server after this point.
  56. /// </summary>
  57. private void Hub_OnConnected(HubConnection hub)
  58. {
  59. uiText += "Hub Connected\n";
  60. // Call a parameterless function. We expect a string return value.
  61. hub.Invoke<string>("Echo", "Message from the client")
  62. .OnSuccess(ret => uiText += string.Format(" 'Echo' returned: '{0}'\n", ret));
  63. }
  64. /// <summary>
  65. /// This callback is called for every hub message. If false is returned, the plugin will cancel any further processing of the message.
  66. /// </summary>
  67. private bool Hub_OnMessage(HubConnection hub, BestHTTP.SignalRCore.Messages.Message message)
  68. {
  69. //uiText += string.Format("( Message received: {0} )\n", message.ToString());
  70. return true;
  71. }
  72. /// <summary>
  73. /// This is called when the hub is closed after a StartClose() call.
  74. /// </summary>
  75. private void Hub_OnClosed(HubConnection hub)
  76. {
  77. uiText += "Hub Closed\n";
  78. }
  79. /// <summary>
  80. /// Called when an unrecoverable error happen. After this event the hub will not send or receive any messages.
  81. /// </summary>
  82. private void Hub_OnError(HubConnection hub, string error)
  83. {
  84. uiText += "Hub Error: " + error + "\n";
  85. }
  86. }
  87. }
  88. #endif