PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jhey/pubnub-api
C# | 226 lines | 107 code | 26 blank | 93 comment | 18 complexity | beae57480ce0716dc414a689cb96b1bd 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. namespace PubNub_Messaging
  6. {
  7. internal static class Subscribe_Example
  8. {
  9. internal static void SubscribeDemo()
  10. {
  11. Pubnub pubnub = new Pubnub(
  12. "demo",
  13. "demo",
  14. "",
  15. "",
  16. false);
  17. string channel = "my_channel";
  18. Console.WriteLine("Subscribe_Example");
  19. pubnub.subscribe(channel, DisplayReturnMessage);
  20. bool userexit = false;
  21. while (!userexit)
  22. {
  23. Console.WriteLine("For Unsubscribe");
  24. Console.WriteLine("Enter Y for UNSUBSCRIBE or ENTER X to EXIT subscribe loop");
  25. string userinput = Console.ReadLine();
  26. if (userinput.ToLower() == "y")
  27. {
  28. pubnub.unsubscribe(channel, DisplayReturnMessage);
  29. userexit = true;
  30. }
  31. else if (userinput.ToLower() == "x")
  32. {
  33. userexit = true;
  34. }
  35. }
  36. }
  37. internal static void SecureSubscribeDemo()
  38. {
  39. Pubnub pubnub = new Pubnub(
  40. "",
  41. "demo",
  42. "",
  43. "pandu",
  44. false);
  45. string channel = "my_channel";
  46. Console.WriteLine("Secure_Subscribe_Example");
  47. pubnub.subscribe(channel, DisplayReturnMessage);
  48. //Console.WriteLine("second channel");
  49. //pubnub.subscribe("second_channel", DisplaySubscribeReturnMessage1);
  50. //Console.WriteLine("third channel");
  51. //pubnub.subscribe("third_channel", DisplaySubscribeReturnMessage1);
  52. //Console.WriteLine("fourth channel");
  53. //pubnub.subscribe("fourth_channel", DisplaySubscribeReturnMessage1);
  54. //string channelNum = "";
  55. //do
  56. //{
  57. // Console.WriteLine("For Unsub");
  58. // Console.WriteLine("Enter 1 for my_channel, 2 for second_channel, 3 for third_channel, 4 for fourth channel, x for EXIT");
  59. // channelNum = Console.ReadLine();
  60. // if (channelNum == "1")
  61. // {
  62. // pubnub.unsubscribe(channel, DisplaySubscribeReturnMessage1);
  63. // }
  64. // else if (channelNum == "2")
  65. // {
  66. // pubnub.unsubscribe("second_channel", DisplaySubscribeReturnMessage1);
  67. // }
  68. // else if (channelNum == "3")
  69. // {
  70. // pubnub.unsubscribe("third_channel", DisplaySubscribeReturnMessage1);
  71. // }
  72. // else if (channelNum == "4")
  73. // {
  74. // pubnub.unsubscribe("fourth_channel", DisplaySubscribeReturnMessage1);
  75. // }
  76. //} while (channelNum.ToLower() != "x");
  77. //Console.WriteLine("Please wait...");
  78. //Console.ReadLine();
  79. }
  80. static void DisplayReturnMessage(object result)
  81. {
  82. IList<object> message = result as IList<object>;
  83. if (message != null && message.Count >= 2)
  84. {
  85. for (int index = 0; index < message.Count; index++)
  86. {
  87. ParseObject(message[index], 1);
  88. }
  89. }
  90. else
  91. {
  92. Console.WriteLine("unable to parse data");
  93. }
  94. }
  95. static void ParseObject(object result, int loop)
  96. {
  97. if (result is object[])
  98. {
  99. object[] arrResult = (object[])result;
  100. foreach (object item in arrResult)
  101. {
  102. if (!item.GetType().IsGenericType)
  103. {
  104. if (!item.GetType().IsArray)
  105. {
  106. Console.WriteLine(item.ToString());
  107. }
  108. else
  109. {
  110. ParseObject(item, loop + 1);
  111. }
  112. }
  113. else
  114. {
  115. ParseObject(item, loop + 1);
  116. }
  117. }
  118. }
  119. else if (result.GetType().IsGenericType && (result.GetType().Name == typeof(Dictionary<,>).Name))
  120. {
  121. Dictionary<string, object> itemList = (Dictionary<string, object>)result;
  122. foreach (KeyValuePair<string, object> pair in itemList)
  123. {
  124. Console.WriteLine(string.Format("key = {0}", pair.Key));
  125. if (pair.Value is object[])
  126. {
  127. Console.WriteLine("value = ");
  128. ParseObject(pair.Value, loop);
  129. }
  130. else
  131. {
  132. Console.WriteLine(string.Format("value = {0}", pair.Value));
  133. }
  134. }
  135. }
  136. }
  137. //static void DisplayReturnMessage(object result)
  138. //{
  139. // IList<object> message = result as IList<object>;
  140. // if (message != null && message.Count >= 2)
  141. // {
  142. // for (int index = 0; index < message.Count; index++)
  143. // {
  144. // if (!message[index].GetType().IsGenericType)
  145. // {
  146. // if (message[index] is object[])
  147. // {
  148. // object[] itemList = (object[])message[index];
  149. // for (int itemIndex = 0; itemIndex < itemList.Length; itemIndex++)
  150. // {
  151. // if (!itemList[itemIndex].GetType().IsGenericType)
  152. // {
  153. // if (itemList[itemIndex].GetType().IsPrimitive)
  154. // {
  155. // Console.WriteLine(string.Format("[{0}][{1}] = {2}", index, itemIndex, itemList[itemIndex].ToString()));
  156. // }
  157. // else
  158. // {
  159. // if (itemList[itemIndex] is object[])
  160. // {
  161. // object[] subitemList = (object[])itemList[itemIndex];
  162. // for (int subitemIndex = 0; subitemIndex < subitemList.Length; subitemIndex++)
  163. // {
  164. // Console.WriteLine(string.Format("[{0}][{1}][{2}] = {3}", index, itemIndex, subitemIndex, subitemList[subitemIndex].ToString()));
  165. // }
  166. // }
  167. // }
  168. // }
  169. // else if ((itemList[itemIndex].GetType().IsGenericType) && (itemList[itemIndex].GetType().Name == typeof(Dictionary<,>).Name))
  170. // {
  171. // Dictionary<string, object> subitemList = (Dictionary<string, object>)itemList[itemIndex];
  172. // foreach (KeyValuePair<string, object> pair in subitemList)
  173. // {
  174. // Console.WriteLine(string.Format("[{0}][{1}] = {2}", index, pair.Key, pair.Value));
  175. // }
  176. // }
  177. // }
  178. // }
  179. // else
  180. // {
  181. // Console.WriteLine(string.Format("[{0}] = {1}", index, message[index].ToString()));
  182. // }
  183. // }
  184. // else if ((message[index].GetType().IsGenericType) && (message[index].GetType().Name == typeof(Dictionary<,>).Name))
  185. // {
  186. // Dictionary<string, object> itemList = (Dictionary<string, object>)message[index];
  187. // foreach (KeyValuePair<string, object> pair in itemList)
  188. // {
  189. // Console.WriteLine(string.Format("[{0}][{1}] = {2}", index, pair.Key, pair.Value));
  190. // }
  191. // }
  192. // }
  193. // }
  194. // else
  195. // {
  196. // Console.WriteLine("result is not List<object>");
  197. // }
  198. //}
  199. }
  200. }