PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jhey/pubnub-api
C# | 157 lines | 95 code | 14 blank | 48 comment | 18 complexity | ded885ad77ede8dfa148cff553410416 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 Presence_Example
  8. {
  9. internal static void PresenceDemo()
  10. {
  11. Pubnub pubnub = new Pubnub(
  12. "demo",
  13. "demo",
  14. "",
  15. "",
  16. false);
  17. string channel = "my_channel";
  18. Console.WriteLine("Presence_Example");
  19. pubnub.presence(channel, DisplayReturnMessage);
  20. bool pre_unsub = false;
  21. while (!pre_unsub)
  22. {
  23. Console.WriteLine("Enter y for Presence-Unsub; x to EXIT presence loop");
  24. string userchoice = Console.ReadLine();
  25. if (userchoice.ToLower() == "y")
  26. {
  27. Console.WriteLine("PresenceUnsubscribe_Example");
  28. pubnub.presence_unsubscribe(channel, DisplayReturnMessage);
  29. pre_unsub = true;
  30. }
  31. else if (userchoice.ToLower() == "x")
  32. {
  33. pre_unsub = true;
  34. }
  35. }
  36. }
  37. static void DisplayReturnMessage(object result)
  38. {
  39. IList<object> message = result as IList<object>;
  40. if (message != null && message.Count >= 2)
  41. {
  42. for (int index = 0; index < message.Count; index++)
  43. {
  44. ParseObject(message[index], 1);
  45. }
  46. }
  47. else
  48. {
  49. Console.WriteLine("unable to parse data");
  50. }
  51. }
  52. static void ParseObject(object result, int loop)
  53. {
  54. if (result is object[])
  55. {
  56. object[] arrResult = (object[])result;
  57. foreach (object item in arrResult)
  58. {
  59. if (!item.GetType().IsGenericType)
  60. {
  61. if (!item.GetType().IsArray)
  62. {
  63. Console.WriteLine(item.ToString());
  64. }
  65. else
  66. {
  67. ParseObject(item, loop + 1);
  68. }
  69. }
  70. else
  71. {
  72. ParseObject(item, loop + 1);
  73. }
  74. }
  75. }
  76. else if (result.GetType().IsGenericType && (result.GetType().Name == typeof(Dictionary<,>).Name))
  77. {
  78. Dictionary<string, object> itemList = (Dictionary<string, object>)result;
  79. foreach (KeyValuePair<string, object> pair in itemList)
  80. {
  81. Console.WriteLine(string.Format("key = {0}", pair.Key));
  82. if (pair.Value is object[])
  83. {
  84. Console.WriteLine("value = ");
  85. ParseObject(pair.Value, loop);
  86. }
  87. else
  88. {
  89. Console.WriteLine(string.Format("value = {0}", pair.Value));
  90. }
  91. }
  92. }
  93. }
  94. //static void DisplayReturnMessage(object result)
  95. //{
  96. // IList<object> message = result as IList<object>;
  97. // if (message != null && message.Count >= 2)
  98. // {
  99. // for (int index = 0; index < message.Count; index++)
  100. // {
  101. // if (!message[index].GetType().IsGenericType)
  102. // {
  103. // if (message[index] is object[])
  104. // {
  105. // object[] itemList = (object[])message[index];
  106. // for (int itemIndex = 0; itemIndex < itemList.Length; itemIndex++)
  107. // {
  108. // if (!itemList[itemIndex].GetType().IsGenericType)
  109. // {
  110. // Console.WriteLine(string.Format("[{0}][{1}] = {2}", index, itemIndex, itemList[itemIndex].ToString()));
  111. // }
  112. // else if ((itemList[itemIndex].GetType().IsGenericType) && (itemList[itemIndex].GetType().Name == typeof(Dictionary<,>).Name))
  113. // {
  114. // Dictionary<string, object> subitemList = (Dictionary<string, object>)itemList[itemIndex];
  115. // foreach (KeyValuePair<string, object> pair in subitemList)
  116. // {
  117. // Console.WriteLine(string.Format("[{0}][{1}] = {2}", index, pair.Key, pair.Value));
  118. // }
  119. // }
  120. // }
  121. // }
  122. // else
  123. // {
  124. // Console.WriteLine(string.Format("[{0}] = {1}", index, message[index].ToString()));
  125. // }
  126. // }
  127. // else if ((message[index].GetType().IsGenericType) && (message[index].GetType().Name == typeof(Dictionary<,>).Name))
  128. // {
  129. // Dictionary<string, object> itemList = (Dictionary<string, object>)message[index];
  130. // foreach (KeyValuePair<string, object> pair in itemList)
  131. // {
  132. // Console.WriteLine(string.Format("[{0}][{1}] = {2}",index, pair.Key, pair.Value));
  133. // }
  134. // }
  135. // }
  136. // }
  137. // else
  138. // {
  139. // Console.WriteLine("result is not List<object>");
  140. // }
  141. //}
  142. }
  143. }