The following example demonstrates how to connect to an XMPP server using the XmppClient class.
Connecting using an existing XMPP account
If you already have an account on the server to which you want to connect,
establishing a connection is as easy as initializing a new instance of the
XmppClient
class und calling the
Connect
method as demonstrated in the following example application:
using S22.Xmpp.Client;
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication52 {
class Program {
static void Main(string[] args) {
string hostname = "jabber.dk";
string username = "myusername";
string password = "mysecretpassword";
using (XmppClient client = new XmppClient(hostname, username, password)) {
// Setup any event handlers.
// ...
client.Connect();
Console.WriteLine("Connected as " + client.Jid + Environment.NewLine);
Console.WriteLine(" Type 'send <JID> <Message>' to send a chat message, or 'quit' to exit.");
Console.WriteLine(" Example: send user@domain.com Hello!");
Console.WriteLine();
while (true) {
Console.Write("> ");
string s = Console.ReadLine();
if (s.StartsWith("send ")) {
Match m = Regex.Match(s, @"^send\s(?<jid>[^\s]+)\s(?<message>.+)$");
if (!m.Success)
continue;
string recipient = m.Groups["jid"].Value, message = m.Groups["message"].Value;
// Send the chat-message.
client.SendMessage(recipient, message);
}
if (s == "quit")
return;
}
}
}
}
}
Using the constructor of the XmppClient class as in the example above will automatically negotiate TLS/SSL encryption if the server supports it, however this behaviour can be disabled by passing the constructor a value of false for the tls parameter.
Connecting without an existing account
XmppClient also supports in-band registration, that is account creation from within an XMPP session. Please go here for a brief walkthrough how to register an account using the in-band registration process.