PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/jhey/pubnub-api
C# | 114 lines | 92 code | 10 blank | 12 comment | 15 complexity | b35a8611f0dbc1dd6af2ed62f59be89b 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 partial class Publish_Example
  8. {
  9. internal static void PublishDemo()
  10. {
  11. Pubnub pubnub = new Pubnub(
  12. "demo",
  13. "demo",
  14. "",
  15. "",
  16. false);
  17. string channel = "my_channel";
  18. Console.WriteLine("Publish_Example");
  19. bool exitFlag = false;
  20. while (!exitFlag)
  21. {
  22. Console.WriteLine("Enter the message for publish. To exit loop, enter QUIT");
  23. string userinput = Console.ReadLine();
  24. if (userinput.ToLower() == "quit")
  25. {
  26. exitFlag = true;
  27. }
  28. else
  29. {
  30. pubnub.publish(channel, userinput, DisplayReturnMessage);
  31. }
  32. }
  33. }
  34. static void DisplayReturnMessage(object result)
  35. {
  36. IList<object> message = result as IList<object>;
  37. if (message != null && message.Count >= 2)
  38. {
  39. for (int index = 0; index < message.Count; index++)
  40. {
  41. ParseObject(message[index], 1);
  42. }
  43. }
  44. else
  45. {
  46. Console.WriteLine("unable to parse data");
  47. }
  48. }
  49. static void ParseObject(object result, int loop)
  50. {
  51. if (result is object[])
  52. {
  53. object[] arrResult = (object[])result;
  54. foreach (object item in arrResult)
  55. {
  56. if (!item.GetType().IsGenericType)
  57. {
  58. if (!item.GetType().IsArray)
  59. {
  60. Console.WriteLine(item.ToString());
  61. }
  62. else
  63. {
  64. ParseObject(item, loop + 1);
  65. }
  66. }
  67. else
  68. {
  69. ParseObject(item, loop + 1);
  70. }
  71. }
  72. }
  73. else if (result.GetType().IsGenericType && (result.GetType().Name == typeof(Dictionary<,>).Name))
  74. {
  75. Dictionary<string, object> itemList = (Dictionary<string, object>)result;
  76. foreach (KeyValuePair<string, object> pair in itemList)
  77. {
  78. Console.WriteLine(string.Format("key = {0}", pair.Key));
  79. if (pair.Value is object[])
  80. {
  81. Console.WriteLine("value = ");
  82. ParseObject(pair.Value, loop);
  83. }
  84. else
  85. {
  86. Console.WriteLine(string.Format("value = {0}", pair.Value));
  87. }
  88. }
  89. }
  90. }
  91. //static void DisplayPublishReturnMessage(object result)
  92. //{
  93. // List<object> message = result as List<object>;
  94. // if (message != null && message.Count >= 2)
  95. // {
  96. // Console.WriteLine(string.Format("[{0}, {1}, {2}]", message[0].ToString(),message[1].ToString(),message[2].ToString()));
  97. // }
  98. // else
  99. // {
  100. // Console.WriteLine("result is not List<object>");
  101. // }
  102. //}
  103. }
  104. }