Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. using Microsoft.AspNet.SignalR.Client;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Console_SignalR
  11. {
  12. class SampleSocketImplementation
  13. {
  14. private static HubConnection hubConnection;
  15. private static IHubProxy coreHubProxy;
  16.  
  17. public static bool queryExchangeStateInProgress = false;
  18. public static bool verboseOutput = false;
  19.  
  20. private class ExchangeState
  21. {
  22. [JsonProperty("MarketName")]
  23. public string MarketName { get; set; }
  24. [JsonProperty("Nounce")]
  25. public int Nounce { get; set; }
  26. [JsonProperty("Buys")]
  27. public List<TransactionInfo> Buys { get; set; }
  28. [JsonProperty("Sells")]
  29. public List<TransactionInfo> Sells { get; set; }
  30. [JsonProperty("Fills")]
  31. public List<OrderFillInfo> Fills { get; set; }
  32.  
  33. public override string ToString()
  34. {
  35. StringBuilder output = new StringBuilder();
  36. output.AppendLine("MarketName: " + MarketName);
  37. output.AppendLine("Nounce: " + Nounce);
  38.  
  39. output.AppendLine("Buys:");
  40. if (Buys.Count == 0)
  41. {
  42. output.AppendLine("\tNo buys available...");
  43. }
  44. else
  45. {
  46. foreach (TransactionInfo buy in Buys)
  47. {
  48. output.AppendLine("\tType: " + buy.Type);
  49. output.AppendLine("\tRate: " + buy.Rate);
  50. output.AppendLine("\tQuantity: " + buy.Quantity);
  51. }
  52. }
  53.  
  54. output.AppendLine("Sells:");
  55. if (Sells.Count == 0)
  56. {
  57. output.AppendLine("\tNo sells available...");
  58. }
  59. else
  60. {
  61. foreach (TransactionInfo sell in Sells)
  62. {
  63. output.AppendLine("\tType: " + sell.Type);
  64. output.AppendLine("\tRate: " + sell.Rate);
  65. output.AppendLine("\tQuantity: " + sell.Quantity);
  66. }
  67. }
  68.  
  69. output.AppendLine("Fills:");
  70. if (Fills.Count == 0)
  71. {
  72. output.AppendLine("\tNo fills available...");
  73. }
  74. else
  75. {
  76. foreach (OrderFillInfo fill in Fills)
  77. {
  78. output.AppendLine("\tOrderType: " + fill.OrderType);
  79. output.AppendLine("\tRate: " + fill.Rate);
  80. output.AppendLine("\tQuantity: " + fill.Quantity);
  81. output.AppendLine("\tTimestamp: " + fill.TimeStamp);
  82. }
  83. }
  84.  
  85. return output.ToString();
  86. }
  87. }
  88.  
  89. private class TransactionInfo
  90. {
  91. [JsonProperty("Type")]
  92. public int Type { get; set; }
  93. [JsonProperty("Rate")]
  94. public float Rate { get; set; }
  95. [JsonProperty("Quantity")]
  96. public float Quantity { get; set; }
  97. }
  98.  
  99. private class OrderFillInfo
  100. {
  101. [JsonProperty("OrderType")]
  102. public string OrderType { get; set; }
  103. [JsonProperty("Rate")]
  104. public float Rate { get; set; }
  105. [JsonProperty("Quantity")]
  106. public float Quantity { get; set; }
  107. [JsonProperty("TimeStamp")]
  108. public DateTime TimeStamp { get; set; }
  109. }
  110.  
  111. static void Main(string[] args)
  112. {
  113. // Set up a connction to the Bittrex Hub
  114. hubConnection = new HubConnection("https://socket.bittrex.com");
  115. hubConnection.TraceLevel = TraceLevels.Events;
  116. hubConnection.TraceWriter = Console.Out;
  117.  
  118. coreHubProxy = hubConnection.CreateHubProxy("coreHub");
  119.  
  120. // register a call back to the server so the client can be updated
  121. // on exchange changes for the markets registered
  122. coreHubProxy.On("updateExchangeState", marketJSon =>
  123. {
  124. HandleUpdatedExchangeState(marketJSon);
  125. }
  126. );
  127.  
  128. // The sample holds a loop in the console to allow the user to
  129. // specify new markets to register for updates to while receiving
  130. // updates for already registered markets
  131. hubConnection.Start().ContinueWith(task =>
  132. {
  133. if (task.IsFaulted)
  134. {
  135. Console.WriteLine("There was an error opening the connection:{0}",
  136. task.Exception.GetBaseException());
  137. }
  138. else
  139. {
  140. Console.WriteLine("Connected");
  141. bool breakLoop = false;
  142.  
  143. while (true)
  144. {
  145. string message = Console.ReadLine();
  146.  
  147. if (string.IsNullOrEmpty(message))
  148. {
  149. continue;
  150. }
  151. else
  152. {
  153. switch (message.ToLower())
  154. {
  155. case "quit":
  156. Console.WriteLine("quitting...");
  157. breakLoop = true;
  158. break;
  159. case "verbose":
  160. verboseOutput = !verboseOutput;
  161. break;
  162. default:
  163. // The marketName is case sensitive. As all
  164. // are uppercase, forcing here
  165. queryExchangeState(message.ToUpper());
  166. break;
  167. }
  168. }
  169.  
  170. // user initiated Quit command
  171. if (breakLoop) break;
  172. }
  173. }
  174.  
  175. }).Wait();
  176.  
  177. // Loop was broken by 'quit' issued by user. Stop the connection to
  178. // the hub
  179. hubConnection.Stop();
  180. }
  181.  
  182. static void queryExchangeState(string marketName)
  183. {
  184. if (hubConnection.State != ConnectionState.Connected)
  185. return;
  186.  
  187. // do not allow other queries until the current query is complete
  188. if (queryExchangeStateInProgress)
  189. return;
  190.  
  191. queryExchangeStateInProgress = true;
  192.  
  193. coreHubProxy.Invoke("SubscribeToExchangeDeltas", marketName).ContinueWith(task1 =>
  194. {
  195. if (task1.IsFaulted)
  196. {
  197. Console.WriteLine("There was an error calling send: {0}", task1.Exception.GetBaseException());
  198. }
  199. else
  200. {
  201. Console.WriteLine("Task Status: {0}",task1.Status.ToString());
  202. Console.WriteLine("Result from Invoke {0}\n\t{1} registered for updates",
  203. task1.Status.ToString(),
  204. marketName);
  205. }
  206. });
  207.  
  208. queryExchangeStateInProgress = false;
  209.  
  210. }
  211.  
  212. static void HandleUpdatedExchangeState(dynamic marketJSon)
  213. {
  214. // Deserialize the JSon broadcast from Server and display
  215. ExchangeState newExState = marketJSon.ToObject<ExchangeState>();
  216. if (verboseOutput)
  217. {
  218. Console.WriteLine(newExState.ToString());
  219. }
  220. else
  221. {
  222. Console.WriteLine(String.Format("MarketName: {0}\tNounce: {1}",
  223. newExState.MarketName,
  224. newExState.Nounce));
  225. }
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement