Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
mcordz #1
Member since Jul 2015 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: Xmpp IOS class  Events doesn't trigger at all
Hi,

      Scenario:

               I'm  using Xamarin Forms. And I installed first the package Matrix.Xamarin.IOS.  Then created a  simple class for testing on:
     
              1.) How to instantiate the Xmppclient.
              2.) Create a user login.
              2.) Create an events (Success or failed).


        Problem:
                When I run this code below. And pass in the parameters from the constructor of the class that calls a Connect() method. The events are not working and I'm sure what's going on to the server. It doesn't trigger at all.

                How do I know if the connection to server is success or fail? (Because the events doesn't work)

                Please let me know what's wrong on my code below.

  1. using System;
  2. using Matrix;
  3. using Matrix.Xmpp;
  4. using Matrix.Xmpp.Client;
  5. using Matrix.Xmpp.Register;
  6. using Matrix.Xmpp.XData;
  7. using System.Xml.Linq;
  8. using System.Diagnostics;
  9.  
  10. namespace XmppTest.iOS
  11. {
  12.     public class XmppClient
  13.     {
  14.         private readonly Matrix.Xmpp.Client.XmppClient xmppClient = null;
  15.  
  16.         private string _username;
  17.         private string _password;
  18.         private string _xmppDomain;
  19.         private string _hostName;
  20.  
  21.         public XmppClient (string username, string password, string domain,string hostname )
  22.         {
  23.             _username = username;
  24.             _password = password;
  25.             _xmppDomain = domain;
  26.             _hostName = hostname;
  27.  
  28.             SetLicense ();
  29.             xmppClient = new Matrix.Xmpp.Client.XmppClient ();
  30.  
  31.             xmppClient.OnMessage += xmppClient_OnMessage;
  32.             xmppClient.OnValidateCertificate += xmpp_OnValidateCertificate;
  33.             xmppClient.OnAuthError += xmppClient_OnAuthError;
  34.             xmppClient.OnLogin += xmppClient_OnLogin;
  35.             xmppClient.OnError += xmppClient_OnError;
  36.             xmppClient.OnRegister += xmppClient_OnRegister;
  37.             Connect ();
  38.         }
  39.            
  40.         private void SetLicense()
  41.         {
  42.            
  43.             string lic = @"LIC_KEY";
  44.  
  45.             Matrix.License.LicenseManager.SetLicense(lic);
  46.         }
  47.  
  48.         public void Connect()
  49.         {
  50.             xmppClient.SetUsername (_username);
  51.             xmppClient.SetXmppDomain (_xmppDomain);
  52.             xmppClient.Password = _password;
  53.  
  54.             xmppClient.Status = "ready for chat";
  55.             xmppClient.Show = Matrix.Xmpp.Show.Chat;
  56.  
  57.             //disable SRV lookups and specify the server hostname manual
  58.             xmppClient.ResolveSrvRecords = false;
  59.             xmppClient.Hostname = _hostName;
  60.  
  61.             xmppClient.Open ();
  62.  
  63.         }
  64.  
  65.         public void SendMessage()
  66.         {
  67.             var msg = new Matrix.Xmpp.Client.Message {
  68.                  Type = MessageType.Chat,
  69.                  To = "Admin@test2.myservver.com",
  70.                  Body = "Hello World"
  71.             };
  72.  
  73.             xmppClient.Send (msg);
  74.         }
  75.            
  76.         #region Events
  77.         private void xmppClient_OnMessage(object sender,MessageEventArgs e)
  78.         {
  79.             Debug.WriteLine (string.Format("OnMessage from {0}",e.Message.From));
  80.             Debug.WriteLine (string.Format("Body {0}",e.Message.Body));
  81.             Debug.WriteLine (string.Format("Type {0}", e.Message.Type));
  82.  
  83.             // we ignore Group chat messsge here
  84.             if (e.Message.Type == MessageType.GroupChat)
  85.                 return;
  86.            
  87.             if (e.Message.Type == MessageType.Error)
  88.             {
  89.                 //Handle error here
  90.                 return;
  91.             }
  92.  
  93.             if (e.Message.Body != null)
  94.             {
  95.                
  96.             }
  97.         }
  98.  
  99.         private void xmpp_OnValidateCertificate(object sender, CertificateEventArgs e)
  100.         {
  101.             // always accept cert
  102.             // e.AcceptCertificate = true
  103.  
  104.             // or let the user validate the certificate
  105.         }
  106.  
  107.         private void ValidateCertificate(CertificateEventArgs e)
  108.         {
  109.             // OnValidateCertificate
  110.  
  111.             if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None) {
  112.                 e.AcceptCertificate = true;
  113.             } else {
  114.                 //
  115.             }
  116.         }
  117.  
  118.         private void xmppClient_OnAuthError(object sender, Matrix.Xmpp.Sasl.SaslEventArgs e) {
  119.             //OnAuthError
  120.             xmppClient.Close();
  121.         }
  122.  
  123.         private void xmppClient_OnReceiveXml(object sender, TextEventArgs e) {
  124.            
  125.         }
  126.  
  127.         private void xmppClient_OnSendXml(object sender, TextEventArgs e) {
  128.            
  129.         }
  130.  
  131.         private void xmppClient_OnStreamError(object sender, StreamErrorEventArgs e) {
  132.             // OnStream Error
  133.         }
  134.  
  135.         private void xmppClient_OnError(object sender,ExceptionEventArgs e)
  136.         {
  137.             // On error
  138.         }
  139.  
  140.         private void xmppClient_OnLogin(object sender, Matrix.EventArgs e)
  141.         {
  142.             // OnLogin
  143.         }
  144.  
  145.         private void xmppClient_OnBind(object sender, JidEventArgs e)
  146.         {
  147.             // On Bind
  148.         }
  149.  
  150.         private void xmppClient_OnClose(object sender, Matrix.EventArgs e)
  151.         {
  152.             //On Close
  153.         }
  154.  
  155.         private void xmppClient_OnRosterStart(object sender, Matrix.EventArgs e)
  156.         {
  157.             // On Roster Start
  158.         }
  159.  
  160.         private void xmpClient_OnRosterEnd(object sender, Matrix.EventArgs e)
  161.         {
  162.             // On Roster end
  163.         }
  164.  
  165.         private void xmppClient_OnPresence(object sender, PresenceEventArgs e)
  166.         {
  167.            
  168.         }
  169.  
  170.         private void xmppClient_OnIq(object sender, IqEventArgs e)
  171.         {
  172.            
  173.         }
  174.  
  175.         private void xmppClient_OnRegisterInformation(object sender, RegisterEventArgs e)
  176.         {
  177.             e.Register.Username = xmppClient.Username;
  178.             e.Register.Password = xmppClient.Password;
  179.         }
  180.  
  181.         private void xmppClient_OnRegister(object sender, Matrix.EventArgs e)
  182.         {
  183.            
  184.         }
  185.  
  186.         private void xmppClient_OnRegisterError(object sender, IqEventArgs e)
  187.         {
  188.             xmppClient.Close ();
  189.         }
  190.  
  191.         #endregion
  192.     }
  193. }
This post was edited on 2015-07-04, 09:36 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4322 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I see nothing wrong with your code.
Do you get the OnError event?
Do you see any exceptions in the IDE?

Can you please also addthe events for the Xml logging as described here?
http://www.ag-software.net/matrix-xmpp-sdk/matrix-develope…

In line 31-36 you don't subscribe to all handlers you have defined below in your code.

Normally when the connect fails you should get an OnError event.

Alex
This post was edited on 2015-07-04, 09:57 by 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: MatriX RSS