Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
apaulus #1
Member since Aug 2017 · 12 posts
Group memberships: Members
Show profile · Link to this post
Subject: Matrix.vnext MUC support
Hi,

We want to try out the new Matrix.vnext library. Our application just needs to do two register clients, create MUC rooms and post some information in them.

We are looking for functionality which was present in the old Matrix library, such as

- MucManager.RequestRoomConfigurationAsync(roomJid)
- MucManager.SubmitRoomConfigurationAsync(roomJid, xForm)
- MucManager.EnterRoomAsync(roomJid, nickname)
- MucManager.ExitRoom(roomJid, nickname)

Can please you provide some sample code on how to achieve this with Matrix.vnext?

Keep up the good work  :-)
Avatar
Alex #2
Member since Feb 2003 · 4327 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
here is a MucManager helper class for MatriX vnext with the requested functionality.
Please let me know if this helps.

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. using Matrix;
  6. using Matrix.Xml;
  7. using Matrix.Xmpp.Client;
  8. using Matrix.Xmpp;
  9. using Matrix.Xmpp.Muc;
  10. using Matrix.Xmpp.Muc.Owner;
  11. using Matrix.Xmpp.XData;
  12. public class MucManager    {
  13.  
  14.     public MucManager(XmppClient xmppClient)
  15.     {
  16.         XmppClient = xmppClient;
  17.     }
  18.  
  19.     public XmppClient XmppClient { get; internal set; }
  20.  
  21.     public async Task<Iq> RequestRoomConfigurationAsync(Jid room)
  22.     {
  23.         var iq = new OwnerIq { Type = IqType.Get, To = room };
  24.         return await XmppClient.SendIqAsync(iq);
  25.     }
  26.  
  27.     public async Task<Iq> SubmitRoomConfigurationAsync(Jid room, Data xdata)
  28.     {
  29.         var iq = new OwnerIq { Type = IqType.Set, To = room, OwnerQuery = { XData = xdata } };
  30.         return await XmppClient.SendIqAsync(iq);
  31.     }
  32.  
  33.     public async Task<XmppXElement> EnterRoomAsync(Jid jid, string nick)
  34.     {
  35.         var createRoomStanza = CreateEnterRoomStanza(jid, nick, null, false, null);
  36.  
  37.         Func<XmppXElement, bool> predicate = e =>
  38.             e.OfType<Presence>()
  39.             && e.Cast<Presence>().From.Equals(jid, new BareJidComparer());
  40.  
  41.         return await XmppClient.SendAsync(createRoomStanza, predicate, 10000, CancellationToken.None);
  42.     }
  43.  
  44.     public async Task<XmppXElement> ExitRoomAsync(Jid jid, string nick)
  45.     {
  46.         var exitRoomStanza = CreateExitRoomStanza(jid, nick);
  47.  
  48.         Func<XmppXElement, bool> predicate = e =>
  49.             e.OfType<Presence>()
  50.             && e.Cast<Presence>().From.Equals(jid, new BareJidComparer());
  51.  
  52.         return await XmppClient.SendAsync(exitRoomStanza, predicate, 10000, CancellationToken.None);
  53.     }
  54.  
  55.     public Presence CreateEnterRoomStanza(Jid room, string nickname, string password = null, bool disableHistory = false, History history = null)
  56.     {           
  57.         var to = new Jid(room.ToString())
  58.         {
  59.             Resource = nickname
  60.         };
  61.  
  62.         var pres = new Presence
  63.         {
  64.             To = to
  65.         };
  66.  
  67.         var x = new X();
  68.         if (password != null)
  69.             x.Password = password;
  70.  
  71.         if (disableHistory)
  72.         {
  73.             var hist = new History { MaxCharacters = 0 };
  74.             x.History = hist;
  75.         }
  76.  
  77.         if (history != null)
  78.             x.History = history;
  79.  
  80.         pres.Add(x);
  81.  
  82.         return pres;
  83.     }
  84.  
  85.     public Presence CreateExitRoomStanza(Jid room, string nickname)
  86.     {
  87.         var to = new Jid(room.ToString())
  88.         {
  89.             Resource = nickname
  90.         };
  91.  
  92.         var pres = new Presence
  93.         {
  94.             To = to,
  95.             Type = PresenceType.Unavailable
  96.         };
  97.  
  98.         return pres;
  99.     }
  100. }

Alex
This post was edited on 2017-08-09, 00:53 by Alex.
Avatar
apaulus #3
Member since Aug 2017 · 12 posts
Group memberships: Members
Show profile · Link to this post
Thank you, this works like a charm :)
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