0

I am trying to develop a windows chat app in wp8.1 in my first page i created a server connection in singleton class.I created another window for sending messages .using singleton how can i maintain the server connection in the next page also? my send button is in the next page.so how can i maintain my connection in the second page also using singleton.?? thanks in advance

this is my first page code for server connection using singleton

  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
   {

    public MainPage()
    {
        InitializeComponent();

    }


    private static volatile Singleton instance;

    private static object syncRoot = new Object();

    public static XMPPConnection ObjXmppCon;

    public  static XMPPClient ObjXmppClient;

    public static Boolean IsXmppSuccess { get; set; }

    public String UserName { get; set; }

    public String PassWord { get; set; }

    public readonly String Server = "taurus";


    public readonly String ServerIPAddress = "127.0.0.1:9090";




    public sealed class Singleton
    {


        private Singleton() { }

        public static Singleton Instance


        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (ObjXmppCon == null)
                            instance = new Singleton();
                    }
                }

                return instance;
            }
        }
    }

  public  void IsXmppValid()
    {

        ObjXmppClient = new XMPPClient();
        ObjXmppClient.JID = UserName + "@" + Server;
        ObjXmppClient.Password = PassWord;
        ObjXmppClient.Server = ServerIPAddress;
        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);


    }

     public  void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
        {

     IsXmppSuccess = client.Connected;

          }



    public   void XMPPClient_OnStateChanged(object sender, EventArgs e)
     {
         switch (ObjXmppClient.XMPPState)
         {
             case XMPPState.Ready:

                 if (IsXmppSuccess)//  the name isxmpp does not contain in the current context
                 {
                     this.Dispatcher.BeginInvoke(() =>
                     {
                         NavigationService.Navigate((new Uri("/Output.xaml?  key=success", UriKind.Relative)));//error

                     });
                 }
                 else
                 {

                     this.Dispatcher.BeginInvoke(() =>
                     {
                         MessageBox.Show("Check server name/IpAddress");

                         return;
                     });
                 }
                 break;

             case XMPPState.AuthenticationFailed:      this.Dispatcher.BeginInvoke(() =>
             {
                 MessageBox.Show("Enter valid username and password");

                 return;

             }); break;
         }
     }

    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();
    }



   }
}
rose
  • 1
  • 3
  • I don't understand. You can easily access your singleton from the other page by using `MainPage.Singleton`. That said, it would be better to put it in its own class rather than in MainPage – Kevin Gosse Oct 28 '15 at 10:43
  • can you please explain? – rose Oct 29 '15 at 03:55

1 Answers1

0

The best way to do this in my opinion would be to use Dependency injection (Ninject would be good). This way you can develop your ServerConnection behind an interface and just inject it into the View Models your pages are using for its data context. Something like below would work.

Singleton binding

Bind<IServerConnection>().To<ServerConnection>().InSingletonScope();

After binding the interface to the implementation using the InSingletonScope you will only have that one instance running. So you can inject it into your view models using constructor injection and use it where needed.

This should make development of a chat application a lot easier. I hope this helps.

Filled Stacks
  • 4,116
  • 1
  • 23
  • 36