[关闭]
@Seymour 2018-08-08T10:16:02.000000Z 字数 3113 阅读 1471

c# agsXMPP 最简单示例

C#


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using agsXMPP;
  5. using agsXMPP.protocol.client;
  6. using agsXMPP.Collections;
  7. using agsXMPP.protocol.iq.roster;
  8. using System.Threading;
  9. namespace JabberClient
  10. {
  11. class Program
  12. {
  13. static bool _wait;
  14. static void Main(string[] args)
  15. {
  16. /*
  17. * Starting Jabber Console, setting the Display settings
  18. *
  19. */
  20. Console.Title = "Jabber Client";
  21. Console.ForegroundColor = ConsoleColor.White;
  22. /*
  23. * Login
  24. *
  25. */
  26. Console.WriteLine("Login");
  27. Console.WriteLine();
  28. Console.WriteLine("JID: ");
  29. string JID_Sender = Console.ReadLine();
  30. Console.WriteLine("Password: ");
  31. string Password = Console.ReadLine();
  32. /*
  33. * Creating the Jid and the XmppClientConnection objects
  34. */
  35. Jid jidSender = new Jid(JID_Sender);
  36. XmppClientConnection xmpp = new XmppClientConnection(jidSender.Server);
  37. /*
  38. * Open the connection
  39. * and register the OnLogin event handler
  40. */
  41. try
  42. {
  43. xmpp.Open(jidSender.User, Password);
  44. xmpp.OnLogin += new ObjectHandler(xmpp_OnLogin);
  45. }
  46. catch (Exception e)
  47. {
  48. Console.WriteLine(e.Message);
  49. }
  50. /*
  51. * workaround, jus waiting till the login
  52. * and authentication is finished
  53. *
  54. */
  55. Console.Write("Wait for Login ");
  56. int i = 0;
  57. _wait = true;
  58. do
  59. {
  60. Console.Write(".");
  61. i++;
  62. if (i == 10)
  63. _wait = false;
  64. Thread.Sleep(500);
  65. } while (_wait);
  66. Console.WriteLine();
  67. /*
  68. *
  69. * just reading a few information
  70. *
  71. */
  72. Console.WriteLine("Login Status:");
  73. Console.WriteLine("xmpp Connection State {0}", xmpp.XmppConnectionState);
  74. Console.WriteLine("xmpp Authenticated? {0}", xmpp.Authenticated);
  75. Console.WriteLine();
  76. /*
  77. *
  78. * tell the world we are online and in chat mode
  79. *
  80. */
  81. Console.WriteLine("Sending Precence");
  82. Presence p = new Presence(ShowType.chat, "Online");
  83. p.Type = PresenceType.available;
  84. xmpp.Send(p);
  85. Console.WriteLine();
  86. /*
  87. *
  88. * get the roster (see who's online)
  89. */
  90. xmpp.OnPresence += new PresenceHandler(xmpp_OnPresence);
  91. //wait until we received the list of available contacts
  92. Console.WriteLine();
  93. Thread.Sleep(500);
  94. /*
  95. * now we catch the user entry, TODO: who is online
  96. */
  97. Console.WriteLine("Enter Chat Partner JID:");
  98. string JID_Receiver = Console.ReadLine();
  99. Console.WriteLine();
  100. /*
  101. * Chat starts here
  102. */
  103. Console.WriteLine("Start Chat");
  104. /*
  105. * Catching incoming messages in
  106. * the MessageCallBack
  107. */
  108. xmpp.MesagageGrabber.Add(new Jid(JID_Receiver),
  109. new BareJidComparer(),
  110. new MessageCB(MessageCallBack),
  111. null);
  112. /*
  113. * Sending messages
  114. *
  115. */
  116. string outMessage;
  117. bool halt = false;
  118. do
  119. {
  120. Console.ForegroundColor =ConsoleColor.Green;
  121. outMessage = Console.ReadLine();
  122. if (outMessage == "q!")
  123. {
  124. halt = true;
  125. }
  126. else
  127. {
  128. xmpp.Send(new Message(new Jid(JID_Receiver), MessageType.chat, outMessage));
  129. }
  130. } while (!halt);
  131. Console.ForegroundColor = ConsoleColor.White;
  132. /*
  133. * finally we close the connection
  134. *
  135. */
  136. xmpp.Close();
  137. }
  138. // Is called, if the precence of a roster contact changed
  139. static void xmpp_OnPresence(object sender, Presence pres)
  140. {
  141. Console.WriteLine("Available Contacts: ");
  142. Console.WriteLine("{0}@{1} {2}", pres.From.User, pres.From.Server, pres.Type);
  143. //Console.WriteLine(pres.From.User + "@" + pres.From.Server + " " + pres.Type);
  144. Console.WriteLine();
  145. }
  146. // Is raised when login and authentication is finished
  147. static void xmpp_OnLogin(object sender)
  148. {
  149. _wait = false;
  150. Console.WriteLine("Logged In");
  151. }
  152. //Handles incoming messages
  153. static void MessageCallBack(object sender,
  154. agsXMPP.protocol.client.Message msg,
  155. object data)
  156. {
  157. if (msg.Body != null)
  158. {
  159. Console.ForegroundColor = ConsoleColor.Red;
  160. Console.WriteLine("{0}>> {1}", msg.From.User, msg.Body);
  161. Console.ForegroundColor = ConsoleColor.Green;
  162. }
  163. }
  164. }
  165. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注