Introduction
Implementing Extensible Messaging and Presence Protocol (XMPP) in Windows phone 8 is really a tough experience. Because, there is no third party library which is for free (there are some libraries which are highly expensive).
Background
Project was required with WP8 chat application using Openfire server. After many days of exploration, we found a library which works well in WP8 (** WP8- Windows phone 8) but it lacks documentation and support. I started trial and error with this library. Please find the library link below:
Pre-Requisites
- VS 2012 Express for Mobile Edition/ VS2012 Windows Phone SDK installed or higher
- Basic Knowledge of XAML, C#
- Openfire server installed
Using the Code
Open VS2012 -> New project -> Select Windows Phone Template -> Select Default application -> Name it as required (here named it as WP8Xmpp) -> Select target OS version – 8.0
Use the DLL which is already built (you can find those in the attached sample application).
(Or use the library link mentioned above, download the highlighted files and build in separate application and get the DLL).
Refer to those libraries, phone.socketserver.dll and phone.xmpp.dll.
Login panel UI is shown below:
In MainPage.xaml.cs, create some properties as shown below:
public String UserName { get; set; }
public String PassWord { get; set; }
private Boolean IsXmppSuccess { get; set; }
private readonly String Server = "gmail.com";
private readonly String ServerIPAddress = "127.0.0.1:9090/";
Look at the sample attached.
(Note: While passing server name, it's very difficult to judge instead of IPaddress
.
But I learnt using trial and error method that is for emulator user server name and for device use server IP Address).
There are two main events during connection, ObjXmppCon_OnAsyncConnectFinished
and xMPPClient_OnStateChanged
.
First asyncConnectFinished
should be executed, there I am setting a flag IsXmppSuccess
.
Soon after, followed by statechanged
event, there is a flag
like below mentioned.
public enum XMPPState
{
Unknown = 0,
Connecting = 1,
Connected = 2,
Authenticating = 3,
Authenticated = 4,
AuthenticationFailed = 5,
CanBind = 6,
Binding = 7,
Bound = 8,
Sessioning = 9,
Session = 10,
Ready = 11,
}
If XMPPState
is ready
, then => server and credentials are correct.
If XMPPState
is AuthenticationFailed
=> credentials are wrong.
I am just navigating to other form Output.xaml if the XMPPState
is Ready
.
If there are any issues, I will try to solve them in the above mentioned sample.
Points of Interest
This is my first try of using XMPP in Windows Phone 8. I hope it will help others.
Update
Please find the next part of the article Send and Receive Message in XMPP here.