Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Page:  1  2  next
nir #1
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
Subject: Xamarin Android Example Client not working
Hi, I'm trying to implement a Xamarin.Forms client using Matrix vNext.

At first I used the example Xamarin.Android MiniClientAndroid, but I got:
[libc] Access denied finding property "net.dns1"
[libc] Access denied finding property "net.dns2"
[libc] Access denied finding property "net.dns3"
[libc] Access denied finding property "net.dns4"

Then I tried using Matrix vNext, Matrix Services, Matrix Extensions nugget packages on a Xamarin Forms project.
On UWP everything works as expected.
On Android it isn't connecting and I get again:
[libc] Access denied finding property "net.dns1"
[libc] Access denied finding property "net.dns2"
[libc] Access denied finding property "net.dns3"
[libc] Access denied finding property "net.dns4"
[libc] Access denied finding property "net.dns5"
[libc] Access denied finding property "net.dns6"
[libc] Access denied finding property "net.dns7"
[libc] Access denied finding property "net.dns8"

Example of my code:
  1. public async Task InitMessagingService()
  2.         {
  3.             // setup XmppClient with some properties
  4.             XmppClient = new XmppClient
  5.             {
  6.                 Username = "nrall",
  7.                 Password = "testtest",
  8.                 XmppDomain = "0day.im",
  9.                 // setting the resolver to use the Srv resolver is optional, but recommended
  10.                 HostnameResolver = new SrvNameResolver()
  11.             };
  12.  
  13.             // connect so the server
  14.             await XmppClient.ConnectAsync();
  15.  
  16.  
  17.             //// request roster (contact list). This is optional,
  18.             //// but most chat clients do this on startup
  19.             var rosterIqResult = await XmppClient.RequestRosterAsync();
  20.  
  21.             var rosterItems
  22.                 = rosterIqResult
  23.                     .Query
  24.                     .Cast<Roster>()
  25.                     .GetRoster();
  26.  
  27.             // enumerate over the items and build your contact list or GUI
  28.             foreach (var ri in rosterItems)
  29.             {
  30.                 // we use AutoMapper here to map the XMPP
  31.                 // rosterItem to our Contact ViewModel
  32.                 var contact = ri;
  33.                 //contacts.AddOrReplace(contact, c => c.Jid == contact.Jid);
  34.  
  35.                 // Dump some info
  36.                 Debug.WriteLine($"Jid: {ri.Jid}");
  37.                 Debug.WriteLine($"Name: {ri.Name}");
  38.             }

Any help is greatly appreciated.
Nicholas
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
For MatriX vNext I would highly suggest to use the code from the net5 branch.
Looks like your problem is related to the SRV resolving code. You can either disable SRV resolving and pass your hostname and port manual, or implement your own SRV resolver which works on Xamarin Android as well. But I assume it will work with the net5 branch.

For legacy MatriX there specific Nuget packages for Xamarin Android and iOS. But we do not support the Android and iOS builds anymore for legacy Matrix.

Alex
nir #3
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
Thank you for your fast response,

I am now using the net5 branch. I am using a StaticNameResolver and there is great speed up in the initial connection. On UWP it still works fine but on Android there is no connection.
Also I don't get any error messages as before.

Thanks
Nicholas
Avatar
Alex #4
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Have you tried to debug the TCP transport code on Android?
Do you have network permissions set correctly in your Android project?

Alex
nir #5
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
I have set the network permissions correctly,
Is there any xamarin (MatriX vNext) example to clone?

Thanks,
Nicholas
Avatar
Alex #6
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Nicholas, there is no sample to clone.
Since Xamarin is netcore/netstandard compatible all code should be 100% portable. Except of the permissions which are required in Xamarin projects


Alex
nir #7
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
I added a logger:
  1. InternalLoggerFactory.DefaultFactory.AddProvider(new DebugLoggerProvider());
  2.             var pipelineInitializerAction = new Action<IChannelPipeline>(pipeline =>
  3.             {
  4.                 pipeline.AddFirst(new LoggingHandler());
  5.             });

I have the following two logs that might be useful:
[0:] DotNetty.Transport.Channels.DefaultChannelPipeline: Debug: Discarded inbound message null that reached at the tail of the pipeline. Please check your pipeline configuration.

[0:] DotNetty.Transport.Channels.DefaultChannelId: Warning: Failed to find a usable hardware address from the network interfaces; using random bytes: B3:23:16:45:42:7E:15:3E
[0:] DotNetty.Transport.Channels.DefaultChannelId: Debug: -Dio.netty.machineId: B3:23:16:45:42:7E:15:3E (auto-detected)

My MainActivity:
  1. [Activity(Label = "Chat", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
  2.     public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  3.     {
  4.         protected override void OnCreate(Bundle savedInstanceState)
  5.         {
  6.             base.OnCreate(savedInstanceState);
  7.  
  8.             Xamarin.Essentials.Platform.Init(this, savedInstanceState);
  9.             global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
  10.             LoadApplication(new App());
  11.         }
  12.         public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
  13.         {
  14.             Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  15.  
  16.             base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  17.         }
  18.     }

And I have added the Internet Permission
I'm compiling using Android 11

My company also has a license from the previous version of the library Matrix XMPP, do I have to use it somewhere?

Am I missing something?

Thanks in advance,
Nicholas
Avatar
Alex #8
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Nicholas,

Quote by nir:
I have the following two logs that might be useful:

I do not see anything useful in your logs. Also you seem to use the old DotNetty based code and not the net5 branch.

Quote by nir:
And I have added the Internet Permission
I'm compiling using Android 11

yes, this was the only required permission in the past. See also here:
https://gitlab.com/matrix-xmpp/samples/-/blob/master/cshar…

Quote by nir:
My company also has a license from the previous version of the library Matrix XMPP, do I have to use it somewhere?
Am I missing something?

No, MatriX vnext is fully open source and does not require any license keys.

I have not worked myself with Xamarin for a while. I will give it a try later and come back to you. But when you browse the forum here you can see that many developers use it with success on iOS and Android.

Alex
Avatar
Alex #9
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I opened an issue here on GitHub. Feel free to join the discussion:
https://github.com/xamarin/xamarin-android/issues/6274

Alex
Avatar
Alex #10
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I have made more tests and found a possible solution for you.

There seems to be an issue in Monos TLS implementation using BoringSSL when plain sockets are upgraded to TLS. Or when there was plain traffic on the socket before.

What works fine for me is when I connect to XMPP Servers with Direct TLS.
See also:
https://xmpp.org/extensions/xep-0368.html

This means we establish a secure connection immediately, without using a plain socket before and then the StartTls command.
Of course your server needs to be configured correctly for Direct Tls. When you also have the correct SRV records in place then MatriX should connect automatically with Direct Tls.

When you do not have them in place you can pass a TCPS Uri with a static name resolver like:
  1. var xmppClient = new XmppClient(
  2.     conf =>
  3.     {
  4.         conf.UseSocketTransport();
  5.         conf.WithResolver(new StaticNameResolver(new Uri("tcps://myserver.com:5223")));
  6.     }    
  7. )

I tested the code above with a Xamarin forms application and it worked fine on our XMPP servers which all support direct TLS.

Alex
nir #11
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
Thanks for the update.

On the Xamarin forms app did you target net standard 2.1?
Avatar
Alex #12
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
yes, I did the following:

1) create a new Xamarin Forms project in Visual Studio
2) By default forms project target .NET Standard 2.0, so u upgraded to 2.1
3) Added references to MatriX, Matrix.Core, Matrix.Extensions, Matrix.Srv and Matrix.Transport.Socket
4) Added the code I posted before to a button command and ran it with success

Alex
nir #13
Member since Aug 2021 · 11 posts
Group memberships: Members
Show profile · Link to this post
In reply to post #11
It finally works on Android. Thanks for you support!

Xamarin UWP doesn't support net standard 2.1 though, is there any conditional hack to support all the platforms with the same runtime
or I should wait for UWP to support 2.1?

Thanks,
Nicholas
Avatar
Alex #14
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by nir:
Xamarin UWP doesn't support net standard 2.1 though, is there any conditional hack to support all the platforms with the same runtime
or I should wait for UWP to support 2.1?

I think to make Xamarin UWP work you probably have to add a lower .Net version to the TargetFrameworks in Matrix. Not sure if this will work without some code changes and DEFINEs for the older framework. Or use the old master branch which I would not suggest.

Also not sure if its worth the effort and when MAUI or netStandard 2.1 for Xamarin UWP is landing. Also depends on which devices and platforms you want to use UWP.

Alex
Avatar
Alex #15
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Just wanted to give you a small update on netStandard 2.0 compatibility.

I looked into it yesterday. It would be possible, and there are not too many changes required. Most of the changes would be required in the socket transport codes. There we use some new APIs that are not available in netStandard 2.0.
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