Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
lokeshdotnet #1
Member for 3 months · 1 post
Group memberships: Members
Show profile · Link to this post
Subject: working with xmppdotnet library in .net core 3.1 (wpf)
I am working with the XMPPDotNet library in a WPF framework .NET 3.1 Core. I want to create chat management system using xmpp. So, i used xmppdotnet library . I am providing all the details in the XMPPClient as mentioned in the documentation, but neither the connection is being established nor am I able to add to a group or send a message. Could you please guide me on how to accomplish these tasks? If there are any mistakes in the code, please correct them and let me know.
this is my code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Net.WebSockets;
  7. using System.Reactive.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using System.Xml.Linq;
  22. using XmppDotNet;
  23. using XmppDotNet.Extensions.Client.Message;
  24. using XmppDotNet.Extensions.Client.Presence;
  25. using XmppDotNet.Extensions.Client.Roster;
  26. using XmppDotNet.Transport;
  27. using XmppDotNet.Transport.Socket;
  28. using XmppDotNet.Transport.WebSocket;
  29. using XmppDotNet.Xmpp;
  30. using XmppDotNet.Xmpp.Client;
  31.  
  32. namespace xmppdemo_new
  33. {
  34.     /// <summary>
  35.     /// Interaction logic for MainWindow.xaml
  36.     /// </summary>
  37.     public partial class MainWindow : Window
  38.     {
  39.         XmppClient xmppClient;
  40.         int websocketRetries = 0; // Track number of WebSocket attempts
  41.  
  42.         public MainWindow()
  43.         {
  44.             InitializeComponent();
  45.         }
  46.  
  47.         public async void Connect()
  48.         {
  49.             try
  50.             {
  51.                 // Attempt WebSocket connection first (preferred)
  52.                 xmppClient = new XmppClient(
  53.                   conf =>
  54.                   {
  55.                       conf.UseWebSocketTransport(new StaticNameResolver(new Uri("wss://chat-new.educrypt.ai:5443/bosh")));
  56.                       conf.AutoReconnect = true;
  57.                   }
  58.                 )
  59.                 {
  60.                     Jid = "740@chat-new.educrypt.ai:5443/bosh",
  61.                     Password = "740",
  62.                     Tls = true
  63.                 };
  64.  
  65.                 await xmppClient.ConnectAsync();
  66.                 websocketRetries = 0; // Reset retries on successful connection
  67.                                       // Connection established using WebSocket
  68.                                       // Rest of your code using xmppClient...
  69.             }
  70.             catch (WebSocketException ex) when (ex.Message.Contains("status code '200'"))
  71.             {
  72.                 websocketRetries++;
  73.  
  74.                 // Check if retries exceed a limit (optional)
  75.                 if (websocketRetries > 3)
  76.                 {
  77.                     MessageBox.Show("WebSocket connection failed repeatedly, using TCP socket instead.");
  78.                 }
  79.  
  80.                 // Fallback to TCP socket
  81.                 xmppClient = new XmppClient(
  82.                   conf =>
  83.                   {
  84.                       conf.Transport = new SocketTransport(); // Use SocketTransport for TCP
  85.                       conf.AutoReconnect = true;
  86.                      
  87.                   }
  88.                 )
  89.                 {
  90.                     Jid = "740@chat-new.educrypt.ai",
  91.                     Password = "740",
  92.                     Tls = true,
  93.                 };
  94.  
  95.                 await xmppClient.ConnectAsync();
  96.                 // Connection established using TCP socket
  97.                 // Rest of your code using xmppClient...
  98.             }
  99.  
  100.             // Rest of your connection established logic...
  101.             xmppClient
  102.               .StateChanged
  103.               .Where(s => s == SessionState.Binded)
  104.               .Subscribe(async v =>
  105.               {
  106.                   // request roster (contact list).
  107.                   // This is optional, but most chat clients do this on startup
  108.                   var roster = await xmppClient.RequestRosterAsync();
  109.  
  110.                   // send our online presence to the server
  111.                   await xmppClient.SendPresenceAsync(XmppDotNet.Xmpp.Show.Chat, "free for chat");
  112.  
  113.                   // send a chat message to user2
  114.                   await xmppClient.SendGroupChatMessageAsync("1712213988412@conference.chat-new.educrypt.ai", "This is a test",true);
  115.  
  116.                    
  117.               });
  118.         }
  119.  
  120.         private async void Button_Click(object sender, RoutedEventArgs e)
  121.         {
  122.             var vcardRequest = new VcardIq { Type = IqType.Get, To = "740@chat-new.educrypt.ai" };
  123.  
  124.             // send the request and await the response
  125.             var resultIq = await xmppClient.SendIqAsync(vcardRequest);
  126.  
  127.             // check for success or failure
  128.             if (resultIq.Type == IqType.Result)
  129.             {
  130.                 // server returned a result (sucsess)
  131.                 // => process the vCard here
  132.             }
  133.             else if (resultIq.Type == IqType.Error)
  134.             {
  135.                 // server returned an error
  136.                 // => handle error
  137.             }
  138.         }
  139.  
  140.         private void Window_Loaded(object sender, RoutedEventArgs e)
  141.         {
  142.             Connect();
  143.         }
  144.     }
  145. }

thanks ,please solve my
This post was edited 2 times, last on 2024-04-10, 17:28 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
1) can you get the Xml logs as described in the docs here?
https://xmppdotnet.org/docs/logging/

2) you should use a bare Jid without resource.
3) Have you tried to connect with the console sample program from the Github repo?

Alex
Avatar
Alex #3
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
also, the URI you  supply seems to be a BOSh endpoint, not the Websocket endpoint of your server.

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters: