Not logged in. · Lost password · Register
Forum: agsXMPP RSS
custom packets
Page:  1  2  next
Avatar
surjith #1
Member since Apr 2006 · 7 posts · Location: Pondicherry
Group memberships: Members
Show profile · Link to this post
Subject: How to Create Custom packets
If any one using agsXMPP namespace kindly tell me of how to create custom packets and send it. If there is any supporting source code pls help me. thanks in advance
Avatar
Jabberer #2
Member since Feb 2006 · 249 posts
Group memberships: Members
Show profile · Link to this post
Example:
We want to create our own XMPP extension to send weather information over the XMPP network. The easiest way to do that is to embed the data with a message stanza. Our raw XML message with embedded weather information will look like that:

  1. <message xmlns="jabber:client" to="romeo@montage.net">
  2.    <weather xmlns="agsoftware:weather">
  3.       <humidity>90</humidity>
  4.       <temperature>57</temperature>
  5.    </weather>
  6. </message>

We created a new namespace for our own protocol and the 3 new elements weather, humidity and temperature.
First we create a new class weather.cs for our custom XML-Element and derive from agsXMPP.Xml.Dom.Element

  1. using System;
  2. using agsXMPP.Xml.Dom;
  3.  
  4. namespace MiniClient
  5. {
  6.     public class Weather : Element
  7.     {
  8.         public Weather()
  9.         {
  10.             this.TagName = "weather";
  11.             this.Namespace = "agsoftware:weather";
  12.         }
  13.  
  14.         public Weather(int humidity, int temperature) : this()
  15.         {
  16.             this.Humidity       = humidity;           
  17.             this.Temperature    = temperature;
  18.         }
  19.  
  20.         public int Humidity
  21.         {
  22.             get { return GetTagInt("humidity"); }
  23.             set { SetTag("humidity", value.ToString()); }
  24.         }
  25.  
  26.         public int Temperature
  27.         {
  28.             get { return GetTagInt("temperature"); }
  29.             set { SetTag("temperature", value.ToString()); }
  30.         }
  31.  
  32.     }
  33. }

We have to register the new class in the ElementFactory. Without registering the class the XML-Parser is not able to build or weather objects while parsing the XML-Stream. We register the class with the following line of code:

  1. agsXMPP.Factory.ElementFactory.AddElementType("weather", "agsoftware:weather", typeof(Weather));

You should register your own Elements before doing anything else with agsXMPP.

Now we can build our weather message and send it:

  1. Weather weather = new Weather(90, 57);
  2.  
  3. Jid to = new Jid("romeo@montage.net");
  4. Message msg = new Message();
  5. msg.To = to;
  6. // Add our weather Element
  7. msg.AddChild(weather);
  8. // Send the message
  9. XmppCon.Send(msg);

Another application which receives this message can access our custom data like in this OnMessage handler:

  1. private void XmppCon_OnMessage(object sender, Message msg)
  2. {
  3.     if (msg.HasTag(typeof(Weather)))
  4.     {
  5.         Weather weather = msg.SelectSingleElement(typeof(Weather)) as Weather;
  6.         Console.WriteLine(weather.Temperature.ToString());
  7.         Console.WriteLine(weather.Humidity.ToString());
  8.     }
  9. }

i hope this tutorial will help you and is useful for other developers too.
Software Developer
AG-Software
Avatar
surjith #3
Member since Apr 2006 · 7 posts · Location: Pondicherry
Group memberships: Members
Show profile · Link to this post
Subject: regarding ur example
Fine very nice example. thank u so much.
Avatar
simons #4
User title: Simon shaw
Member since May 2006 · 30 posts
Group memberships: Members
Show profile · Link to this post
Subject: Weather example
I copy pasted your example to my application and I successfully manage to send the WeatherIQ from one side to the other with the following XML

<iq xmlns="jabber:client" id="agsXMPP_14" type="get" to="simons@xmppserver/Bust" from="simonhamelech@xmppserver/Bust">
<query xmlns="agsoftware:weather">
<zip>12345</zip>
</query>
</iq>

However, when the message is received by the remote side using the following code, bWeather remains false as the query type would seem to be agsXMPP.Xml.Dom.Element and not agsXMPP.protocol.extensions.Weather.Weather:

        private void XmppCon_OnIq(object sender, agsXMPP.Xml.Dom.Node e)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke               
                BeginInvoke(new StreamHandler(XmppCon_OnIq), new object[] { sender, e });
                return;
            }

            agsXMPP.protocol.client.IQ iq = e as IQ;

            if (iq != null)
            {
                Element query = iq.Query;

                if (query != null)
                {
                        bool bWeather = false;
                        if (query.GetType() == typeof(agsXMPP.protocol.extensions.Weather.Weather))
                            bWeather = true;
                }
           }
        }

Any ideas?

Thanks,

Simon
Avatar
Alex #5
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello simons,

you have to register the weather classes in the ElementFactory with it's tagnames and namespaces.

Alex
Avatar
simons #6
User title: Simon shaw
Member since May 2006 · 30 posts
Group memberships: Members
Show profile · Link to this post
Hi Alex,

I have added the following line to \agsXMPP\Factory\ElementFactory.cs.

AddElementType("weather",           Uri.WEATHER,                typeof(agsXMPP.protocol.extensions.Weather.Weather));

and the following line to agsXMPP\Uri.cs

public const string WEATHER             = "agsoftware:weather";

but it does not seem to help.

Any more ideas?

Thanks,

Simon
Avatar
Alex #7
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello,

in the sample above the tagname is query, not weather
So use:

  1. AddElementType("query",           Uri.WEATHER,                typeof(agsXMPP.protocol.extensions.Weather.Weather));

Alex
Avatar
simons #8
User title: Simon shaw
Member since May 2006 · 30 posts
Group memberships: Members
Show profile · Link to this post
Hi Alex,

That did the trick.

Thanks,

Simon
Avatar
jabberfans #9
User title: jabberfans
Member since Dec 2006 · 2 posts
Group memberships: Members
Show profile · Link to this post
In reply to post #7
Subject: the query can't  get
HI,Alex:
        i use this example ,agsXMPP add

AddElementType("query", Uri.WEATHER, typeof(agsXMPP.protocol.extensions.sim.Weather));

client as following:

           WeatherIq wIq = new WeatherIq(IqType.get);
            wIq.To = new Jid("weather.mortagne.net");
            wIq.From = new Jid("romeo@montagne.net");
             wIq.Query.Zip = 74080;
              XmppCon.Send(wIq);

server can't get query
 Element query = iq.Query;

            if (query != null)
            {
                string type = query.GetType().ToString();

            }
the query is null, and the client build this xml:
<iq xmlns="jabber:client" id="agsXMPP_6" type="get" to="weather.mortagne.net" from="romeo@montagne.net"><weather xmlns="agsoftware:weather"><zip>74080</zip></weather></iq>


your write example buile the xml
<iq to=’romeo@montagne.net’ from=’weather.mortagne.net’ type=’result’
id=’agsXMPP_1’>
<query xmlns=’agsoftware:weather’>
<humidity>90</humidity >
<temperature>57</temperature>
<zip>74080</zip>
</query>
</iq>


any idear? thanks
Avatar
Alex #10
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
simons had the same problems. Please find the answer in this thread 2 posts above.
Avatar
jabberfans #11
User title: jabberfans
Member since Dec 2006 · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: find the problem
hi alex:
i found the problem
if use the example 2
the Weather.cs 's TagName changed "query"
  
   this.TagName = "query";
Avatar
blorecrafter #12
Member since Aug 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
In reply to post #10
Subject: Another issue
Hi Alex
I use openfire server and when i try the same example i get the foll. error

<iq xmlns="jabber:client" from="test@localxmppserver" to="sample@localxmppserver/agsXMPP" type="error" id="agsXMPP_4"><query xmlns="agsoftware:weather"><zip>74080</zip></query><error code="503" type="cancel"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanza...

ofcourse i have registered weather (with "query" as tagname) with the element factory.
Any idea why is it so? is it the openfire server or something else.

Thanks
blorecrafter
Avatar
Alex #13
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello,

service-unavailable normally indicates that the entity the stanzas should be delivered to is unavailable.
Is sample@localxmppserver/agsXMPP available when you are sending the IQ?

Alex
Avatar
blorecrafter #14
Member since Aug 2008 · 3 posts
Group memberships: Members
Show profile · Link to this post
Hi Alex
I have just written a simple client application to login using XMPPClientConnection. It handles sending/receiving messages and IQs.
So far i have tried out sending  iqs addressed to server and had no problem with the same.
Just then i tried sending a custom iq (weatheriq) from 'test' user to 'sample' user by using the same simple clients - one each for the respective user.
note: I have not taken care of any presence stanza information as i'm not aware whether those are needed.
Thanks
blorecrafter
Avatar
Alex #15
Member since Feb 2003 · 4317 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
in your case Iqs get delivered from client to client. Unlike messages they are not stored for offline delivery by the server. They can be only delivered when the user is online. And the Iqs must be sent to the full Jids.
So you need the presence information to get the resources and deliver the iq to the correct full jid. Otherwise you get the error messages you posted before.

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:
Page:  1  2  next
Forum: agsXMPP RSS