PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/mono-for-mac/3.3.0.1/PubNub-Messaging/PubNub-Console/Pubnub_Example.cs

https://github.com/jhey/pubnub-api
C# | 213 lines | 186 code | 19 blank | 8 comment | 20 complexity | baee5189af7497a4738b8d91c53e9b33 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Threading;
  7. namespace PubNub_Messaging
  8. {
  9. public class Pubnub_Example
  10. {
  11. static public Pubnub pubnub;
  12. static public bool deliveryStatus = false;
  13. static public string channel = "";
  14. static public void Main()
  15. {
  16. Console.WriteLine("HINT: TO TEST RE-CONNECT AND CATCH-UP,");
  17. Console.WriteLine(" DISCONNECT YOUR MACHINE FROM NETWORK/INTERNET AND ");
  18. Console.WriteLine(" RE-CONNECT YOUR MACHINE AFTER SOMETIME.");
  19. Console.WriteLine();
  20. Console.WriteLine(" IF NO NETWORK BEFORE MAX RE-TRY CONNECT,");
  21. Console.WriteLine(" NETWORK ERROR MESSAGE WILL BE SENT");
  22. Console.WriteLine();
  23. Console.WriteLine("ENTER Channel Name");
  24. channel = Console.ReadLine();
  25. Console.WriteLine(string.Format("Channel = {0}",channel));
  26. Console.WriteLine();
  27. Console.WriteLine("Enable SSL? ENTER Y for Yes, else N");
  28. string enableSSL = Console.ReadLine();
  29. if (enableSSL.Trim().ToLower() == "y")
  30. {
  31. Console.WriteLine("SSL Enabled");
  32. }
  33. else
  34. {
  35. Console.WriteLine("SSL NOT Enabled");
  36. }
  37. Console.WriteLine();
  38. Console.WriteLine("ENTER cipher key for encryption feature.");
  39. Console.WriteLine("If you don't want to avail at this time, press ENTER.");
  40. string cipheryKey = Console.ReadLine();
  41. if (cipheryKey.Trim().Length > 0)
  42. {
  43. Console.WriteLine("Cipher key provided.");
  44. }
  45. else
  46. {
  47. Console.WriteLine("No Cipher key provided");
  48. }
  49. Console.WriteLine();
  50. pubnub = new Pubnub("demo", "demo", "", cipheryKey,
  51. (enableSSL.Trim().ToLower() == "y") ? true : false);
  52. Console.WriteLine("ENTER 1 FOR Subscribe");
  53. Console.WriteLine("ENTER 2 FOR Publish");
  54. Console.WriteLine("ENTER 3 FOR Presence");
  55. Console.WriteLine("ENTER 4 FOR Detailed History");
  56. Console.WriteLine("ENTER 5 FOR Here_Now");
  57. Console.WriteLine("ENTER 6 FOR Unsubscribe");
  58. Console.WriteLine("ENTER 7 FOR Presence-Unsubscribe");
  59. Console.WriteLine("ENTER 8 FOR Time");
  60. Console.WriteLine("ENTER 0 FOR EXIT OR QUIT");
  61. bool exitFlag = false;
  62. Console.WriteLine("");
  63. while (!exitFlag)
  64. {
  65. string userinput = Console.ReadLine();
  66. switch (userinput)
  67. {
  68. case "0":
  69. exitFlag = true;
  70. break;
  71. case "1":
  72. Console.WriteLine("Running subscribe()");
  73. pubnub.subscribe<string>(channel, DisplayReturnMessage);
  74. //System.Threading.Tasks.Task subtask = System.Threading.Tasks.Task.Factory.StartNew(() => pubnub.subscribe<string>(channel, DisplayReturnMessage));
  75. //pubnub.subscribe<object>(channel, DisplayReturnMessage);
  76. //pubnub.subscribe(channel, DisplayReturnMessage);
  77. break;
  78. case "2":
  79. Console.WriteLine("Running publish()");
  80. Console.WriteLine("Enter the message for publish. To exit loop, enter QUIT");
  81. string publishMsg = Console.ReadLine();
  82. pubnub.publish<string>(channel, publishMsg, DisplayReturnMessage);
  83. break;
  84. case "3":
  85. Console.WriteLine("Running presence()");
  86. pubnub.presence<string>(channel, DisplayReturnMessage);
  87. //System.Threading.Tasks.Task pretask = System.Threading.Tasks.Task.Factory.StartNew(() => pubnub.presence<string>(channel, DisplayReturnMessage));
  88. //pubnub.presence<object>(channel, DisplayReturnMessage);
  89. break;
  90. case "4":
  91. Console.WriteLine("Running detailed history()");
  92. pubnub.detailedHistory<string>(channel, 100, DisplayReturnMessage);
  93. //pubnub.detailedHistory<object>(channel, 100, DisplayReturnMessage);
  94. break;
  95. case "5":
  96. Console.WriteLine("Running Here_Now()");
  97. pubnub.here_now<string>(channel, DisplayReturnMessage);
  98. //pubnub.here_now<object>(channel, DisplayReturnMessage);
  99. break;
  100. case "6":
  101. Console.WriteLine("Running unsubscribe()");
  102. pubnub.unsubscribe<string>(channel, DisplayReturnMessage);
  103. //pubnub.unsubscribe<object>(channel, DisplayReturnMessage);
  104. break;
  105. case "7":
  106. Console.WriteLine("Running presence-unsubscribe()");
  107. pubnub.presence_unsubscribe<string>(channel, DisplayReturnMessage);
  108. break;
  109. case "8":
  110. Console.WriteLine("Running time()");
  111. pubnub.time<string>(DisplayReturnMessage);
  112. break;
  113. default:
  114. Console.WriteLine("INVALID CHOICE.");
  115. break;
  116. }
  117. }
  118. Console.WriteLine("\nPress any key to confirm exit.\n\n");
  119. Console.ReadLine();
  120. }
  121. static void DisplayReturnMessage(string result)
  122. {
  123. Console.WriteLine(result);
  124. }
  125. static void DisplayReturnMessage(object result)
  126. {
  127. IList<object> message = result as IList<object>;
  128. if (message != null && message.Count >= 1)
  129. {
  130. for (int index = 0; index < message.Count; index++)
  131. {
  132. ParseObject(message[index], 1);
  133. }
  134. }
  135. else
  136. {
  137. Console.WriteLine("unable to parse data");
  138. }
  139. }
  140. static void ParseObject(object result, int loop)
  141. {
  142. if (result is object[])
  143. {
  144. object[] arrResult = (object[])result;
  145. foreach (object item in arrResult)
  146. {
  147. if (item != null)
  148. {
  149. if (!item.GetType().IsGenericType)
  150. {
  151. if (!item.GetType().IsArray)
  152. {
  153. Console.WriteLine(item.ToString());
  154. }
  155. else
  156. {
  157. ParseObject(item, loop + 1);
  158. }
  159. }
  160. else
  161. {
  162. ParseObject(item, loop + 1);
  163. }
  164. }
  165. else
  166. {
  167. Console.WriteLine();
  168. }
  169. }
  170. }
  171. else if (result.GetType().IsGenericType && (result.GetType().Name == typeof(Dictionary<,>).Name))
  172. {
  173. Dictionary<string, object> itemList = (Dictionary<string, object>)result;
  174. foreach (KeyValuePair<string, object> pair in itemList)
  175. {
  176. Console.WriteLine(string.Format("key = {0}", pair.Key));
  177. if (pair.Value is object[])
  178. {
  179. Console.WriteLine("value = ");
  180. ParseObject(pair.Value, loop);
  181. }
  182. else
  183. {
  184. Console.WriteLine(string.Format("value = {0}", pair.Value));
  185. }
  186. }
  187. }
  188. else
  189. {
  190. Console.WriteLine(result.ToString());
  191. }
  192. }
  193. }
  194. }