Not logged in. · Lost password · Register
Forum: agsXMPP RSS
One Server, One Client, Many Accounts
Avatar
Jinno #1
Member since Jun 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
Subject: Running Multiple Accounts on One Client
Alright, sorry to post asking for help again so soon, but after successfully getting a connection, I've run across another problem that is foiling my plans of successfully doing what I want to do. As opposed to vaguely stating things here's an actual description of what I would like to do with my program:

For the project I'm currently working on I need to allow connection from the website to the Jabber server. My boss didn't want to allow a direct connection from the website, so we're going to make it so that the website writes it's messages to an XML page, and a client on the server will read that XML page, parse the data, and send it through an instance of a Jabber client connected to a guest account. When messages are receive that client instance would write XML to an outbound document that the web-page would read on an AJAX polling request synced to its cache and update that chat info accordingly.

My problem is that when I run the client, it will not process as two instances. It'll only process the first client, and not the second one. Here's the code of both parts:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace chatProg
{
    public partial class main : Form
    {
      
        public main()
        {
            InitializeComponent();
            notifyIcon1.MouseDown += new MouseEventHandler(notifyIcon1_MouseDown);
            this.Hide();
            Client one = new Client(1);
            Client two = new Client(2);
        }

        void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
        {
            contextMenuStrip1.Show(MousePosition);
        }
        [STAThread]
        static void Main()
        {
            Application.Run(new main());
        }
        private void CloseConnection()
        {
            //throw new NotImplementedException();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Then the client code...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.Collections;
using agsXMPP.protocol.iq.roster;

namespace chatProg
{
    public partial class Client : Form
    {
            static bool _bWait;
        public Client(int num)
        {
            InitializeComponent();
            connect(num);
        }
        static void connect(int num)
        {
            XmppClientConnection xmppCon = new XmppClientConnection();

            Jid jid = new Jid("guest" + num + "@domain.com");
            xmppCon.Password = (generic password); 
            xmppCon.AutoResolveConnectServer = false;
            xmppCon.Username = jid.User;
            xmppCon.Server = jid.Server;
            xmppCon.ConnectServer = (server ip);
            xmppCon.AutoAgents = false;
            xmppCon.AutoPresence = true;
            xmppCon.AutoRoster = true;
            // Connect to the server now
            // !!! this is asynchronous !!!
            try
            {
                xmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
                xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
                xmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
                xmppCon.OnPresence += new PresenceHandler(xmppCon_OnPresence);
                xmppCon.OnMessage += new MessageHandler(xmppCon_OnMessage);
                xmppCon.OnLogin += new ObjectHandler(xmppCon_OnLogin);
                xmppCon.OnSaslStart += new agsXMPP.sasl.SaslEventHandler(xmppCon_OnSaslStart);

                xmppCon.Open();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Wait("Login to server, please wait");

            PrintCommands();

            bool bQuit = false;

            while (!bQuit)
            {
                string command = "msg sys.admin@domain.com This is a test post, foo.";
                string[] commands = command.Split(' ');

                switch (commands[0].ToLower())
                {
                    case "help":
                        PrintCommands();
                        break;
                    case "quit":
                        bQuit = true;
                        break;
                    case "msg":
                        string msg = command.Substring(command.IndexOf(commands[2]));
                        xmppCon.Send(new agsXMPP.protocol.client.Message(new Jid(commands[1]), MessageType.chat, msg));
                        break;
                    case "status":
                        switch (commands[1])
                        {
                            case "online":
                                xmppCon.Show = ShowType.NONE;
                                break;
                            case "away":
                                xmppCon.Show = ShowType.away;
                                break;
                            case "xa":
                                xmppCon.Show = ShowType.xa;
                                break;
                            case "chat":
                                xmppCon.Show = ShowType.chat;
                                break;
                        }
                        string status = command.Substring(command.IndexOf(commands[2]));
                        xmppCon.Status = status;
                        xmppCon.SendMyPresence();
                        break;
                }
            }

            // close connection
            xmppCon.Close();
        }

        static void xmppCon_OnSaslStart(object sender, agsXMPP.sasl.SaslEventArgs args)
        {
            args.Auto = false;
            args.Mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(agsXMPP.protocol.sasl.MechanismType.PLAIN);
        }

        private static void PrintCommands()
        {
            PrintHelp("You are logged in to the server now.");
            PrintHelp("");
            PrintHelp("Available commands are:");
            PrintHelp("msg toJid text");
            PrintHelp("status show{online, away, xa, chat} status");
            PrintHelp("help");
            PrintHelp("quit");
            PrintHelp("");
            PrintHelp("Examples:");
            PrintHelp("msg test@server.com Hello World");
            PrintHelp("msg test@server.com/Office Hello World");
            PrintHelp("status chat free for chat");
            PrintHelp("");
        }

        private static void Wait(string statusMessage)
        {
            int i = 0;
            _bWait = true;

            while (_bWait)
            {
                i++;
                if (i == 60)
                    _bWait = false;

                Thread.Sleep(500);
            }
        }

        static void xmppCon_OnLogin(object sender)
        {
            Console.WriteLine();
            PrintEvent("Logged in to server");
        }

        static void xmppCon_OnRosterEnd(object sender)
        {
            _bWait = false;
            Console.WriteLine();
            PrintInfo("All contacts received");
        }

        static void xmppCon_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            PrintInfo(String.Format("Got contact: {0}", item.Jid));
        }

        static void xmppCon_OnRosterStart(object sender)
        {
            PrintEvent("Getting contacts now");
        }

        static void xmppCon_OnPresence(object sender, Presence pres)
        {
            PrintInfo(String.Format("Got presence from: {0}", pres.From.ToString()));
            PrintInfo(String.Format("type: {0}", pres.Type.ToString()));
            PrintInfo(String.Format("status: {0}", pres.Status));
            PrintInfo("");
        }

        static void xmppCon_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
        {

            if (msg.Body != null)
            {
                PrintEvent(String.Format("Got Message from: {0}", msg.From.ToString()));
                PrintEvent("Message: " + msg.Body);
                PrintInfo("");
            }
        }

        static void PrintEvent(string msg)
        {
            ConsoleColor current = Console.BackgroundColor;

            Console.BackgroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine(msg);

            Console.BackgroundColor = current;
        }

        static void PrintInfo(string msg)
        {
            ConsoleColor current = Console.BackgroundColor;

            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine(msg);

            Console.BackgroundColor = current;
        }

        static void PrintHelp(string msg)
        {
            ConsoleColor current = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(msg);

            Console.ForegroundColor = current;
        }
    }
}
(Ignore the fact that I haven't changed it out of a loop yet. I've found it entertaining to spam our system administrator in my frustration.)
Avatar
Alex #2
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by Jinno:
Alright, sorry to post asking for help again so soon, but after successfully getting a connection ....

no problem, this is the idea of this forum ;-)
However if you need professional support services you should consider a commercial license.

I have not tried your code. Your client class is synchronous and so is your main class. Is it possible that Client1 never disconnects in the connect(int num) function and hangs in the loop forever?

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS