Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Avatar
Alexiis #1
Member since Aug 2016 · 3 posts
Group memberships: Members
Show profile · Link to this post
Subject: Send JSON as Body
Hi,

I develop a chat application and I have to send a JSON as message's body. I can read incoming messages but I can't send if it's not like a String.

If I send my message like
  1. <message to="id@server" type="chat" id="123" xmlns="jabber:client">
  2.    <body>"JSONObject"</body>
  3. </message>
the message can't be read by other application (ignored message)

If I send my message like
  1. <message to="id@server" type="chat" id="123" xmlns="jabber:client">
  2.    <body>JSONObject</body>
  3. </message>
my Stream is closed immediatly and the message isn't sent

If the body isn't a String, I can't send it, so I want to know how can I send JSON as Body

Function that sent message
  1. /*...*/
  2. using Matrix;
  3. using Matrix.Xmpp.Client;
  4. /*...*/
  5.  
  6. XmppClient client;
  7. MyMessage message;
  8.  
  9. /*...*/
  10.  
  11. /*
  12.    client : Connection, Events ...
  13.    message : create
  14. */
  15.  
  16. private void Send_Click(object sender, RoutedEventArgs e)
  17. {
  18.    Matrix.Xmpp.Client.Message m = new Matrix.Xmpp.Client.Message();
  19.    m.To = userId + "@" + server;
  20.    m.Type = Matrix.Xmpp.MessageType.Chat;
  21.    m.Id = Guid.NewGuid().ToString();
  22.    m.Body = message.ToJSON(); //message isn't sent, Stream closed
  23.    //m.Body = "\"" + message.ToJSON() + "\""; //message sent but can't be read by other apps
  24.    m.RequestReceipt();
  25.    client.Send(m);
  26. }

Thanks
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
can you please show your XML logs?
Adding Json to a message should not be a problem at all.

Alex
Avatar
Alexiis #3
Member since Aug 2016 · 3 posts
Group memberships: Members
Show profile · Link to this post
All RECEIVE and SEND elements come from OnReceiveXml and OnSendXml
The message is sent at line 110, and, without action, Stream is closed immediatly after

  1. SEND
  2. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="domain.com" version="1.0" >   
  3.  
  4. RECEIVE
  5. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" id="4018132476" from="domain.com" version="1.0" xml:lang="en" >
  6.  
  7. RECEIVE
  8. <stream:features xmlns:stream="http://etherx.jabber.org/streams">
  9.  <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls" />
  10.  <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
  11.     <mechanism>PLAIN</mechanism>
  12.  </mechanisms>
  13.  <amp xmlns="http://jabber.org/feature/amp" />
  14.  <sm xmlns="urn:xmpp:sm:3" />
  15. </stream:features>
  16.  
  17. SEND
  18. <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls" />   
  19.  
  20. RECEIVE
  21. <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls" />
  22.  
  23. SEND
  24. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="domain.com" version="1.0" >   
  25.  
  26. RECEIVE
  27. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" id="1110196177" from="domain.com" version="1.0" xml:lang="en" >
  28.  
  29. RECEIVE
  30. <stream:features xmlns:stream="http://etherx.jabber.org/streams">
  31.  <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
  32.     <mechanism>PLAIN</mechanism>
  33.  </mechanisms>
  34.  <amp xmlns="http://jabber.org/feature/amp" />
  35.  <sm xmlns="urn:xmpp:sm:3" />
  36. </stream:features>
  37.  
  38. SEND
  39. <auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">ADg0MDMwMDkyQGNoYXQuZ3JpbmRyLmNvbQA1Y2IzZDJkNjdlYTU5NmQ2MzY2ZWEzYzY0NWQ4MjZjZDZmN2ZlYzJlOTNiNjQwZjUyMWY3NmQ4YmM3ZGNmZSDk</auth>   
  40.  
  41. RECEIVE
  42. <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />
  43.  
  44. CONTECTED
  45.  
  46. SEND
  47. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" to="domain.com" version="1.0" >   
  48.  
  49. RECEIVE
  50. <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" id="1106626071" from="domain.com" version="1.0" xml:lang="en" >
  51.  
  52. RECEIVE
  53. <stream:features xmlns:stream="http://etherx.jabber.org/streams">
  54.  <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind" />
  55.  <session xmlns="urn:ietf:params:xml:ns:xmpp-session" />
  56.  <amp xmlns="http://jabber.org/feature/amp" />
  57.  <sm xmlns="urn:xmpp:sm:3" />
  58. </stream:features>
  59.  
  60. SEND
  61. <iq id="MX_1" type="set" xmlns="jabber:client">
  62.  <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
  63.     <resource>W10Device</resource>
  64.  </bind>
  65. </iq>  
  66.  
  67. RECEIVE
  68. <iq id="MX_1" type="result" xmlns="jabber:client">
  69.  <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
  70.     <jid>id@domain.com/W10Device</jid>
  71.  </bind>
  72. </iq>
  73.  
  74. SEND
  75. <iq id="MX_2" type="set" xmlns="jabber:client">
  76.  <session xmlns="urn:ietf:params:xml:ns:xmpp-session" />
  77. </iq>  
  78.  
  79. RECEIVE
  80. <iq type="result" id="MX_2" xmlns="jabber:client">
  81.  <session xmlns="urn:ietf:params:xml:ns:xmpp-session" />
  82. </iq>
  83.  
  84. SEND
  85. <enable resume="true" xmlns="urn:xmpp:sm:3" />  
  86.  
  87. SEND
  88. <presence xmlns="jabber:client">
  89.  <show>chat</show>
  90.  <status></status>
  91.  <priority>0</priority>
  92. </presence>
  93.  
  94. RECEIVE
  95. <enabled xmlns="urn:xmpp:sm:3" id="g2gCbQAAABg4NDAzMDA5MkBjaGF0LmdyaW5kci5jb21oAmgDYgAABcBiAAXJsmIACB6lZ2QAIW1vbmdvb3NlaW1AcHJvZC1jaGF0LTIuZ3JpbmRyLmNvbQAAbv8AAA+AAw==" resume="true" />
  96.  
  97. RECEIVE
  98. <presence from="id@domain.com/W10Device" to="id@domain.com/W10Device" xmlns="jabber:client">
  99.  <show>chat</show>
  100.  <status />
  101.  <priority>0</priority>
  102. </presence>
  103.  
  104. RECEIVE
  105. <r xmlns="urn:xmpp:sm:3" />
  106.  
  107. SEND
  108. <a h="1" xmlns="urn:xmpp:sm:3" />  
  109.  
  110. SEND
  111. <message type="chat" to="id1@domain.com" id="d8b01f62-a870-4cd4-822c-2b725d9f0d0c" xmlns="jabber:client">
  112.  <body>{"body":"Hi","messageId":"d8b01f62-a870-4cd4-822c-2b725d9f0d0c","sourceProfileId":"id","taretProfileId":"id1","timestamp":1472386513,"type":"text"}</body>
  113.  <request xmlns="urn:xmpp:receipts" />
  114. </message>  
  115.  
  116. RECEIVE
  117. </stream:stream>
  118.  
  119. SEND
  120. </stream:stream>   
  121.  
  122. CLOSE

Client configuration
  1. public MainPage()
  2. {
  3.    this.InitializeComponent();
  4.    client = new XmppClient();
  5.  
  6.    /* Events Handler (OnMessage, OnBeforeSasl, OnSendXml, OnReceiveXml ... */
  7.  
  8.    client.Hostname = server;
  9.    client.AutoRoster = false;
  10.    client.AutoPresence = true;
  11.    client.XmppDomaine = server;
  12.    client.StreamManagement = true;
  13.    client.StartTls = true;
  14.    client.Username = myId + "@" + server;
  15.    client.Password = password;
  16.    client.Resource = "W10Device";
  17.  
  18.    client.Open();
  19. }
  20.  
  21. private void Client_OnBeforeSasl(object sender, Matrix.Xmpp.Sasl.SaslEventArgs e)
  22. {
  23.    e.Auto = false;
  24.    e.SaslMechanism = Matrix.Xmpp.Sasl.SaslMechanism.Plain;
  25. }
Avatar
Alexiis #4
Member since Aug 2016 · 3 posts
Group memberships: Members
Show profile · Link to this post
I Solved my problem. My JSON Object wasn't correct
Thanks for your help and I'm sorry for the inconvenience
Avatar
Alex #5
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
great to hear that its working now.

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: