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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Net.WebSockets;
using System.Reactive.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using XmppDotNet;
using XmppDotNet.Extensions.Client.Message;
using XmppDotNet.Extensions.Client.Presence;
using XmppDotNet.Extensions.Client.Roster;
using XmppDotNet.Transport;
using XmppDotNet.Transport.Socket;
using XmppDotNet.Transport.WebSocket;
using XmppDotNet.Xmpp;
using XmppDotNet.Xmpp.Client;
namespace xmppdemo_new
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
XmppClient xmppClient;
int websocketRetries = 0; // Track number of WebSocket attempts
public MainWindow()
{
InitializeComponent();
}
public async void Connect()
{
try
{
// Attempt WebSocket connection first (preferred)
xmppClient = new XmppClient(
conf =>
{
conf.UseWebSocketTransport(new StaticNameResolver(new Uri("wss://chat-new.educrypt.ai:5443/bosh")));
conf.AutoReconnect = true;
}
)
{
Jid = "740@chat-new.educrypt.ai:5443/bosh",
Password = "740",
Tls = true
};
await xmppClient.ConnectAsync();
websocketRetries = 0; // Reset retries on successful connection
// Connection established using WebSocket
// Rest of your code using xmppClient...
}
catch (WebSocketException ex) when (ex.Message.Contains("status code '200'"))
{
websocketRetries++;
// Check if retries exceed a limit (optional)
if (websocketRetries > 3)
{
MessageBox.Show("WebSocket connection failed repeatedly, using TCP socket instead.");
}
// Fallback to TCP socket
xmppClient = new XmppClient(
conf =>
{
conf.Transport = new SocketTransport(); // Use SocketTransport for TCP
conf.AutoReconnect = true;
}
)
{
Jid = "740@chat-new.educrypt.ai",
Password = "740",
Tls = true,
};
await xmppClient.ConnectAsync();
// Connection established using TCP socket
// Rest of your code using xmppClient...
}
// Rest of your connection established logic...
xmppClient
.StateChanged
.Where(s => s == SessionState.Binded)
.Subscribe(async v =>
{
// request roster (contact list).
// This is optional, but most chat clients do this on startup
var roster = await xmppClient.RequestRosterAsync();
// send our online presence to the server
await xmppClient.SendPresenceAsync(XmppDotNet.Xmpp.Show.Chat, "free for chat");
// send a chat message to user2
await xmppClient.SendGroupChatMessageAsync("1712213988412@conference.chat-new.educrypt.ai", "This is a test",true);
});
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var vcardRequest = new VcardIq { Type = IqType.Get, To = "740@chat-new.educrypt.ai" };
// send the request and await the response
var resultIq = await xmppClient.SendIqAsync(vcardRequest);
// check for success or failure
if (resultIq.Type == IqType.Result)
{
// server returned a result (sucsess)
// => process the vCard here
}
else if (resultIq.Type == IqType.Error)
{
// server returned an error
// => handle error
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Connect();
}
}
}
thanks ,please solve my