Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
iam trying to develop an app in WP8.1 using xmpp and openfire server.i logged into the server but the message is cannot sending to the server .I successfully logged into the server.if it is not the way to send and receive please give me a sample..or please help me..iam fedup with this.
how can i check/test this with 2 emulator?
how can i see the send and receive messages?


THIS IS MY FIRST PAGE CODE ie:VALIDATING XMPP USING CREDENTIAL
Through this first page code i can log in to the openfire server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP8Xmpp.Resources;
using System.Net.XMPP;
using System.Threading;

namespace WP8Xmpp
{
    public partial class MainPage : PhoneApplicationPage
    {
        /// <summary>
        /// Username of the client
        /// </summary>
        public String UserName { get; set; }
        /// <summary>
        /// Password
        /// </summary>
        public String PassWord { get; set; }

        private Boolean IsXmppSuccess { get; set; }

       private readonly String Server = "server";


        private readonly String ServerIPAddress = "127.0.0.1:9090";//127.0.0.1:9090/

         /// <summary>
        /// Xmpp Client
        /// </summary>
        public XMPPClient ObjXmppClient { get; set; }

        /// <summary>
        /// XMPP Connection 
        /// </summary>
        public XMPPConnection ObjXmppCon { get; set; }


        // Constructor
        public MainPage()
        {
            InitializeComponent();
            
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (txtUserName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Enter Username");
                return;
            }
            if (txtPassword.Password.Trim() == string.Empty)
            {
                MessageBox.Show("Enter Password");
                return;
            }

            UserName = txtUserName.Text.Trim();
            PassWord = txtPassword.Password.Trim();
            IsXmppValid();
        }

        private void IsXmppValid()
        {
            ObjXmppClient = new XMPPClient();
            //initializing the xmpp client with credentials
            ObjXmppClient.JID = UserName + "@" + Server;
            ObjXmppClient.Password = PassWord;
            ObjXmppClient.Server = ServerIPAddress;  //user server for emulator and ip address for device.
            ObjXmppClient.AutoReconnect = true;
            ObjXmppClient.RetrieveRoster = true;
            ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
            ObjXmppClient.AutoAcceptPresenceSubscribe = true;
            ObjXmppClient.AttemptReconnectOnBadPing = true;
            ObjXmppCon = new XMPPConnection(ObjXmppClient);
            ObjXmppCon.Connect();
            ObjXmppClient.Connect();

            //initializing the xmpp connection

            ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
            ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);

               Thread.Sleep(2000);

        }

       void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
        {
            IsXmppSuccess = client.Connected;
        }

        void xMPPClient_OnStateChanged(object sender, EventArgs e)
        {
            switch (ObjXmppClient.XMPPState)
            {
                case XMPPState.Ready:
                    if (IsXmppSuccess)
                    {
                        this.Dispatcher.BeginInvoke(() =>
                        {
                            //CloseOverLay();

                            //MessageBox.Show("Successfully logged in");
                            NavigationService.Navigate((new Uri("/Output.xaml?key=success",UriKind.Relative)));
                            //return;
                        });
                    }
                    else
                    {
                        
                        this.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show("Check server name/IpAddress");
                            //NavigationService.Navigate((new Uri("/Output.xaml?key=failed", UriKind.Relative)));
                            return;
                        });
                    }
                    break;

                case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Enter valid username and password");
                    //NavigationService.Navigate((new Uri("/Output.xaml?key=wrong", UriKind.Relative)));
                    return;

                }); break;
            }
        }
    }
}




this is my second page code,in this page i given the code to send and receive the message,.message cannot send to the server.please help me? i cant fix the problem


my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Net.XMPP;

namespace WP8Xmpp
{
    public partial class Output : PhoneApplicationPage
    {
        public Output()
        {
            InitializeComponent();
        }
      public String UserName = "user2";
       
        public String PassWord= "test" ;

        private Boolean IsXmppSuccess { get; set; }

        private readonly String Server = "server";


        private readonly String ServerIPAddress = "127.0.0.1:9090";//127.0.0.1:9090/

        XMPPClient ObjXmppClient = new XMPPClient();
      
        JID jidto = new JID("user2@server");
   
        bool bReceived = true;

       
       
       
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("key"))
            {
                string val = NavigationContext.QueryString["key"];
                if (val.Contains("success"))
                {
                   // lblShowResult.Text = "Successfully Logged in";
                }
                
            }
        }

        
     

       

        private void SendXmppMessage(String Message, JID ReceiverJid)
                 {
           

                     try
                     {


                         ObjXmppClient.JID = UserName + "@" + Server;
                     
                         ObjXmppClient.Password = PassWord;
                         ObjXmppClient.Server = ServerIPAddress;  //user server for emulator and ip address for device.
                         ObjXmppClient.AutoReconnect = true;
                         ObjXmppClient.RetrieveRoster = true;
                         ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
                         ObjXmppClient.AutoAcceptPresenceSubscribe = true;
                         ObjXmppClient.AttemptReconnectOnBadPing = true;
                         XMPPConnection ObjXmppCon = new XMPPConnection(ObjXmppClient);
                         ObjXmppCon.Connect();
                         ObjXmppClient.Connect();

                         ObjXmppClient.SendChatMessage(Message.Trim(), ReceiverJid);
                     
                         ObjXmppClient.OnNewConversationItem += ObjXmppClient_OnNewConversationItem;
                     

                     }
                       
            catch(Exception ex)
                     {

                      

                     }

                  }

        
 


        void ObjXmppClient_OnNewConversationItem(RosterItem item, bool bReceived, TextMessage msg)
        {
            if (bReceived)
            {
                
               
                MessageBox.Show("sent");

            }
        }
     

      

        private void Button_Click(object sender, RoutedEventArgs e)
        {
      
            SendXmppMessage(txttype.Text, jidto);
    
        }
    }
}
Posted
Updated 19-Oct-15 17:29pm
v9
Comments
sreeyush sudhakaran 19-Oct-15 1:07am    
Please mention your Question as Heading...Then only Experts can trace you :) , Is this code written by you?
Sinisa Hajnal 19-Oct-15 2:42am    
What is the exact message? Is there any stack trace on the message? What are the values of the variables in the critical part of the code?
[no name] 19-Oct-15 5:19am    
am just creating a chat app.my message is in txttype,text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900