ServerMessages.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  3. using Newtonsoft.Json.Linq;
  4. #endif
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. namespace BestHTTP.SignalR.Messages
  9. {
  10. /// <summary>
  11. /// Keep-alive message sent by the server. No data sent with it.
  12. /// </summary>
  13. public sealed class KeepAliveMessage : IServerMessage
  14. {
  15. MessageTypes IServerMessage.Type { get { return MessageTypes.KeepAlive; } }
  16. void IServerMessage.Parse(object data) { }
  17. }
  18. /// <summary>
  19. /// A message that may contains multiple sub-messages and additional informations.
  20. /// </summary>
  21. public sealed class MultiMessage : IServerMessage
  22. {
  23. MessageTypes IServerMessage.Type { get { return MessageTypes.Multiple; } }
  24. /// <summary>
  25. /// Id of the sent message
  26. /// </summary>
  27. public string MessageId { get; private set; }
  28. /// <summary>
  29. /// True if it's an initialization message, false otherwise.
  30. /// </summary>
  31. public bool IsInitialization { get; private set; }
  32. /// <summary>
  33. /// Group token may be sent, if the group changed that the client belongs to.
  34. /// </summary>
  35. public string GroupsToken { get; private set; }
  36. /// <summary>
  37. /// The server suggests that the client should do a reconnect turn.
  38. /// </summary>
  39. public bool ShouldReconnect { get; private set; }
  40. /// <summary>
  41. /// Additional poll delay sent by the server.
  42. /// </summary>
  43. public TimeSpan? PollDelay { get; private set; }
  44. /// <summary>
  45. /// List of server messages sent inside this message.
  46. /// </summary>
  47. public List<IServerMessage> Data { get; private set; }
  48. void IServerMessage.Parse(object data)
  49. {
  50. IDictionary<string, object> dic = data as IDictionary<string, object>;
  51. object value;
  52. this.MessageId = dic["C"].ToString();
  53. if (dic.TryGetValue("S", out value))
  54. IsInitialization = int.Parse(value.ToString()) == 1 ? true : false;
  55. else
  56. IsInitialization = false;
  57. if (dic.TryGetValue("G", out value))
  58. GroupsToken = value.ToString();
  59. if (dic.TryGetValue("T", out value))
  60. ShouldReconnect = int.Parse(value.ToString()) == 1 ? true : false;
  61. else
  62. ShouldReconnect = false;
  63. if (dic.TryGetValue("L", out value))
  64. PollDelay = TimeSpan.FromMilliseconds(double.Parse(value.ToString()));
  65. IEnumerable enumerable = dic["M"] as IEnumerable;
  66. if (enumerable != null)
  67. {
  68. Data = new List<IServerMessage>();
  69. foreach (object subData in enumerable)
  70. {
  71. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  72. IDictionary<string, JToken> subObj = subData as IDictionary<string, JToken>;
  73. #else
  74. IDictionary<string, object> subObj = subData as IDictionary<string, object>;
  75. #endif
  76. IServerMessage subMsg = null;
  77. if (subObj != null)
  78. {
  79. if (subObj.ContainsKey("H"))
  80. subMsg = new MethodCallMessage();
  81. else if (subObj.ContainsKey("I"))
  82. subMsg = new ProgressMessage();
  83. else
  84. subMsg = new DataMessage();
  85. }
  86. else
  87. subMsg = new DataMessage();
  88. subMsg.Parse(subData);
  89. Data.Add(subMsg);
  90. }
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// A simple non-hub data message. It holds only one Data property.
  96. /// </summary>
  97. public sealed class DataMessage : IServerMessage
  98. {
  99. MessageTypes IServerMessage.Type { get { return MessageTypes.Data; } }
  100. public object Data { get; private set; }
  101. void IServerMessage.Parse(object data)
  102. {
  103. this.Data = data;
  104. }
  105. }
  106. /// <summary>
  107. /// A Hub message that orders the client to call a method.
  108. /// </summary>
  109. public sealed class MethodCallMessage : IServerMessage
  110. {
  111. MessageTypes IServerMessage.Type { get { return MessageTypes.MethodCall; } }
  112. /// <summary>
  113. /// The name of the Hub that the method is called on.
  114. /// </summary>
  115. public string Hub { get; private set; }
  116. /// <summary>
  117. /// Name of the Method.
  118. /// </summary>
  119. public string Method { get; private set; }
  120. /// <summary>
  121. /// Arguments of the method call.
  122. /// </summary>
  123. public object[] Arguments { get; private set; }
  124. /// <summary>
  125. /// State changes of the hub. It's handled automatically by the Hub.
  126. /// </summary>
  127. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  128. public IDictionary<string, JToken> State { get; private set; }
  129. #else
  130. public IDictionary<string, object> State { get; private set; }
  131. #endif
  132. void IServerMessage.Parse(object data)
  133. {
  134. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  135. IDictionary<string, JToken> dic = data as IDictionary<string, JToken>;
  136. #else
  137. IDictionary<string, object> dic = data as IDictionary<string, object>;
  138. #endif
  139. Hub = dic["H"].ToString();
  140. Method = dic["M"].ToString();
  141. List<object> args = new List<object>();
  142. foreach (object arg in dic["A"] as IEnumerable)
  143. args.Add(arg);
  144. Arguments = args.ToArray();
  145. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  146. JToken value;
  147. if (dic.TryGetValue("S", out value))
  148. State = value as IDictionary<string, JToken>;
  149. #else
  150. object value;
  151. if (dic.TryGetValue("S", out value))
  152. State = value as IDictionary<string, object>;
  153. #endif
  154. }
  155. }
  156. /// <summary>
  157. /// Message of a server side method invocation result.
  158. /// </summary>
  159. public sealed class ResultMessage : IServerMessage, IHubMessage
  160. {
  161. MessageTypes IServerMessage.Type { get { return MessageTypes.Result; } }
  162. /// <summary>
  163. /// The unique id that the client set when called the server side method. Used by the plugin to deliver this message to the good Hub.
  164. /// </summary>
  165. public UInt64 InvocationId { get; private set; }
  166. /// <summary>
  167. /// The return value of the server side method call, or null if the method's return type is void.
  168. /// </summary>
  169. public object ReturnValue { get; private set; }
  170. /// <summary>
  171. /// State changes of the hub. It's handled automatically by the Hub.
  172. /// </summary>
  173. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  174. public IDictionary<string, JToken> State { get; private set; }
  175. #else
  176. public IDictionary<string, object> State { get; private set; }
  177. #endif
  178. void IServerMessage.Parse(object data)
  179. {
  180. IDictionary<string, object> dic = data as IDictionary<string, object>;
  181. InvocationId = UInt64.Parse(dic["I"].ToString());
  182. object value;
  183. if (dic.TryGetValue("R", out value))
  184. ReturnValue = value;
  185. if (dic.TryGetValue("S", out value))
  186. {
  187. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  188. State = value as IDictionary<string, JToken>;
  189. #else
  190. State = value as IDictionary<string, object>;
  191. #endif
  192. }
  193. }
  194. }
  195. public sealed class FailureMessage : IServerMessage, IHubMessage
  196. {
  197. MessageTypes IServerMessage.Type { get { return MessageTypes.Failure; } }
  198. /// <summary>
  199. /// The unique id that the client set when called the server side method. Used by the plugin to deliver this message to the good Hub.
  200. /// </summary>
  201. public UInt64 InvocationId { get; private set; }
  202. /// <summary>
  203. /// True if it's a hub error.
  204. /// </summary>
  205. public bool IsHubError { get; private set; }
  206. /// <summary>
  207. /// If the method call failed, it contains the error message to detail what happened.
  208. /// </summary>
  209. public string ErrorMessage { get; private set; }
  210. /// <summary>
  211. /// A dictionary that may contain additional error data (can only be present for hub errors). It can be null.
  212. /// </summary>
  213. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  214. public IDictionary<string, JToken> AdditionalData { get; private set; }
  215. #else
  216. public IDictionary<string, object> AdditionalData { get; private set; }
  217. #endif
  218. /// <summary>
  219. /// Stack trace of the error. It present only if detailed error reporting is turned on on the server (https://msdn.microsoft.com/en-us/library/microsoft.aspnet.signalr.hubconfiguration.enabledetailederrors%28v=vs.118%29.aspx).
  220. /// </summary>
  221. public string StackTrace { get; private set; }
  222. /// <summary>
  223. /// State changes of the hub. It's handled automatically by the Hub.
  224. /// </summary>
  225. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  226. public IDictionary<string, JToken> State { get; private set; }
  227. #else
  228. public IDictionary<string, object> State { get; private set; }
  229. #endif
  230. void IServerMessage.Parse(object data)
  231. {
  232. IDictionary<string, object> dic = data as IDictionary<string, object>;
  233. InvocationId = UInt64.Parse(dic["I"].ToString());
  234. object value;
  235. if (dic.TryGetValue("E", out value))
  236. ErrorMessage = value.ToString();
  237. if (dic.TryGetValue("H", out value))
  238. IsHubError = int.Parse(value.ToString()) == 1 ? true : false;
  239. if (dic.TryGetValue("D", out value))
  240. {
  241. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  242. AdditionalData = value as IDictionary<string, JToken>;
  243. #else
  244. AdditionalData = value as IDictionary<string, object>;
  245. #endif
  246. }
  247. if (dic.TryGetValue("T", out value))
  248. StackTrace = value.ToString();
  249. if (dic.TryGetValue("S", out value))
  250. {
  251. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  252. State = value as IDictionary<string, JToken>;
  253. #else
  254. State = value as IDictionary<string, object>;
  255. #endif
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// When a server method is a long running method the server can send the information about the progress of execution of the method to the client.
  261. /// </summary>
  262. public sealed class ProgressMessage : IServerMessage, IHubMessage
  263. {
  264. MessageTypes IServerMessage.Type { get { return MessageTypes.Progress; } }
  265. /// <summary>
  266. /// The unique id that the client set when called the server side method. Used by the plugin to deliver this message to the good Hub.
  267. /// </summary>
  268. public UInt64 InvocationId { get; private set; }
  269. /// <summary>
  270. /// Current progress of the long running method.
  271. /// </summary>
  272. public double Progress { get; private set; }
  273. void IServerMessage.Parse(object data)
  274. {
  275. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  276. IDictionary<string, JToken> dic = data as IDictionary<string, JToken>;
  277. #else
  278. IDictionary<string, object> dic = data as IDictionary<string, object>;
  279. #endif
  280. #if BESTHTTP_SIGNALR_WITH_JSONDOTNET
  281. IDictionary<string, JToken> P = dic["P"] as IDictionary<string, JToken>;
  282. #else
  283. IDictionary<string, object> P = dic["P"] as IDictionary<string, object>;
  284. #endif
  285. InvocationId = UInt64.Parse(P["I"].ToString());
  286. Progress = double.Parse(P["D"].ToString());
  287. }
  288. }
  289. }
  290. #endif