Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
MatriX doesn't execute events
Avatar
jbgomez21 #1
Member since Jan 2013 · 2 posts · Location: Uruguay
Group memberships: Members
Show profile · Link to this post
Subject: Problems with threads
What is the problem in this example?
I have a class named Client that sends a message and receives another.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Matrix.Xmpp;
using Matrix.Xmpp.Client;
using System.Threading;

namespace XMPPApplication
{
    class Client
    {
        private XmppClient xmppClient;
        private List<string> listMessages;
        private string lic;       

        public Client(string user, string pass, string sendTo, string message,string lic)
        {
            listMessages = new List<string>();
            this.lic = lic;          

            xmppClient = new XmppClient
            {
                XmppDomain = "jbgomez-pc",
                Username = user,
                Password = pass
            };
          
            xmppClient.OnRosterEnd += delegate { this.SendMessageOnRosterEnd(sendTo, message); };
            xmppClient.OnMessage += this.xmppClient_OnMessage;
        }

        void xmppClient_OnMessage(object sender, MessageEventArgs e)
        {
            Console.WriteLine("Soy el hilo de xmppClient_OnMessage: " + Thread.CurrentThread.ManagedThreadId + " " + e.Message.Body);
            listMessages.Add(e.Message.Body);
        }

        public void SendMessageOnRosterEnd(string sendTo, string message)
        {
            Console.WriteLine("Soy el hilo de SendMessageOnRosterEnd: " + Thread.CurrentThread.ManagedThreadId + " " + message);
            xmppClient.Send(new Message
            {
                To = sendTo,
                Type = MessageType.chat,
                Body = message
            });
        }

        public void Open()
        {
 
            Matrix.License.LicenseManager.SetLicense(this.lic);
            xmppClient.Open();
        }

        public void Close()
        {
            xmppClient.Close();
        }

        public string GetMessage()
        {
            string msg = null;
            if (listMessages.Count > 0)
            {
                msg = listMessages[0];
                listMessages.Remove(msg);
            }
            return msg;
        }
    }
}

The Main program is the next:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace XMPPApplication
{
    class Program
    {

        static void Main(string[] args)
        {
                       
            Client c1 = new Client("jbgomez", "xxxxxx", "jbgomez2@jbgomez-pc", "Soy el cliente 1", License.lic1);
            Client c2 = new Client("jbgomez2", "xxxxxx", "jbgomez@jbgomez-pc", "Soy el cliente 2", License.lic1);
           
            c1.Open();
            c2.Open();

            //sleep 30 sec
            Thread.Sleep(30000);
            Console.WriteLine(c1.GetMessage());
            Console.WriteLine(c2.GetMessage());


            c1.Close();
            c2.Close();

            Console.ReadLine();
        }

    }
}

When I run program only it execute one event, I get on the console:

Soy el hilo de SendMessageOnRosterEnd: 9 Soy el cliente 1

It should run four events....
Two SendMessageOnRosterEnd and two xmppClient_OnMessage

Why this events doesn't run?

I'm sorry for my bad english :-)

Regards,
Juan
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I think the problem is that you send the message in the OnRosterEndEvent. Some servers don't like that because at this point the session is not fully ready yet because you have not marked yourself as online.

I have modified your sample a bit to send the Message when we got the first presence and the session is definitely ready. This works fine for me and I get all events on an ejabberd test server.

This are the changes I made to your constructor:

  1. public Client(string user, string pass, string sendTo, string message,string lic)
  2. {
  3.     listMessages = new List<string>();
  4.     this.lic = lic;          
  5.  
  6.     xmppClient = new XmppClient
  7.     {
  8.         XmppDomain = "jbgomez-pc",
  9.         Username = user,
  10.         Password = pass
  11.     };
  12.  
  13.     bool sent = false;
  14.     xmppClient.OnPresence += (s, e) =>
  15.     {
  16.         if (sent)
  17.             return;
  18.  
  19.         xmppClient.Send(new Message
  20.         {
  21.             To = sendTo,
  22.             Type = MessageType.chat,
  23.             Body = message
  24.         });
  25.  
  26.         sent = true;
  27.     };  
  28.    
  29.     xmppClient.OnMessage += this.xmppClient_OnMessage;
  30. }


let me know if this works for you as well.

Alex
Avatar
jbgomez21 #3
Member since Jan 2013 · 2 posts · Location: Uruguay
Group memberships: Members
Show profile · Link to this post
Curiously, I have installed the same server in another PC and it works there
I'm using ejabberd-2.1.11.
Anyway, your solution worked fine on both PC.

Thanks very much for helping me and his fast reply

Regards,
Juan
Avatar
Alex #4
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I tested also with ejabberd 2.x.
With your code sometimes the message was not delivered and the server returned an error message instead. This is weird and looks like an ejabberd bug. Because when a user is offline then the message must be stored in the offline storage and delivered when you come online again.
So this looks like a server race condition because MatriX connects both your accounts at the same time. They will be online with only some milliseconds delay.

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: