PostSendTransportBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. using System.Collections.Generic;
  3. using BestHTTP.SignalR.Messages;
  4. namespace BestHTTP.SignalR.Transports
  5. {
  6. /// <summary>
  7. /// A base class for implementations that must send the data in unique requests. These are currently the LongPolling and ServerSentEvents transports.
  8. /// </summary>
  9. public abstract class PostSendTransportBase : TransportBase
  10. {
  11. /// <summary>
  12. /// Sent out send requests. Keeping a reference to them for future use.
  13. /// </summary>
  14. protected List<HTTPRequest> sendRequestQueue = new List<HTTPRequest>();
  15. public PostSendTransportBase(string name, Connection con)
  16. : base(name, con)
  17. {
  18. }
  19. #region Send Implementation
  20. protected override void SendImpl(string json)
  21. {
  22. var request = new HTTPRequest(Connection.BuildUri(RequestTypes.Send, this), HTTPMethods.Post, true, true, OnSendRequestFinished);
  23. request.FormUsage = Forms.HTTPFormUsage.UrlEncoded;
  24. request.AddField("data", json);
  25. Connection.PrepareRequest(request, RequestTypes.Send);
  26. request.Send();
  27. sendRequestQueue.Add(request);
  28. }
  29. void OnSendRequestFinished(HTTPRequest req, HTTPResponse resp)
  30. {
  31. sendRequestQueue.Remove(req);
  32. // error reason if there is any. We will call the manager's Error function if it's not empty.
  33. string reason = string.Empty;
  34. switch (req.State)
  35. {
  36. // The request finished without any problem.
  37. case HTTPRequestStates.Finished:
  38. if (resp.IsSuccess)
  39. {
  40. HTTPManager.Logger.Information("Transport - " + this.Name, "Send - Request Finished Successfully! " + resp.DataAsText);
  41. if (!string.IsNullOrEmpty(resp.DataAsText))
  42. {
  43. IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText);
  44. if (msg != null)
  45. Connection.OnMessage(msg);
  46. }
  47. }
  48. else
  49. reason = string.Format("Send - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
  50. resp.StatusCode,
  51. resp.Message,
  52. resp.DataAsText);
  53. break;
  54. // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
  55. case HTTPRequestStates.Error:
  56. reason = "Send - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
  57. break;
  58. // The request aborted, initiated by the user.
  59. case HTTPRequestStates.Aborted:
  60. reason = "Send - Request Aborted!";
  61. break;
  62. // Connecting to the server is timed out.
  63. case HTTPRequestStates.ConnectionTimedOut:
  64. reason = "Send - Connection Timed Out!";
  65. break;
  66. // The request didn't finished in the given time.
  67. case HTTPRequestStates.TimedOut:
  68. reason = "Send - Processing the request Timed Out!";
  69. break;
  70. }
  71. if (!string.IsNullOrEmpty(reason))
  72. Connection.Error(reason);
  73. }
  74. #endregion
  75. }
  76. }
  77. #endif