kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
Apache License 2.0
764 stars 273 forks source link

Xamarin.Android throws not implemented exception #108

Open rusty21 opened 6 years ago

rusty21 commented 6 years ago

On Xamarin Android when attempting to open a connection when there is no internet connection causes the app to crash with a System.NotImplementedException. I've put this code in my MainActivity in the OnCreate() method

       var  _websocket = new WebSocket("wss://ws-us2.pusher.com/app/1?protocol=5&client=pusher-dotnet-client&version=0.0.1")
        {
            EnableAutoSendPing = true,
            AutoSendPingInterval = 8
        };
        _websocket.Error += (sender, args) =>
        {
        };
        _websocket.Open();

I'd be happy to provide additional information if necessary.

kerryjiang commented 6 years ago

Do you have the stacktrace of this exception?

rusty21 commented 6 years ago
Xamarin caused by: android.runtime.JavaProxyThrowable: System.NotImplementedException: The method or operation is not implemented.
System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(Exception exception, int bytesTransferred, SocketFlags flags)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.AsyncFail(Exception e)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.Fail(bool sync, Exception e)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.DoDnsCallback(IAsyncResult result, bool sync)<6c708cf596db438ebfc6b7e012659eee>:0
System.Net.Sockets.MultipleConnectAsync.DnsCallback(IAsyncResult result)<6c708cf596db438ebfc6b7e012659eee>:0
System.Runtime.Remoting.Messaging.AsyncResult.Invoke(AsyncResult)(wrapper managed-to-native)
System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()<896ad1d315ca4ba7b117efb8dacaedcf>:0
System.Threading.ThreadPoolWorkQueue.Dispatch()<896ad1d315ca4ba7b117efb8dacaedcf>:0
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()<896ad1d315ca4ba7b117efb8dacaedcf>:0
kerryjiang commented 6 years ago

Which target of the library do you use? I though it is .netstandard one because I didn't release assemblies for 0.15 in the Xamarin platform.

rusty21 commented 6 years ago

Looks like it's monoandroid10, or is that not what you were looking for?

kerryjiang commented 6 years ago

Are you sue if you use WebSocket4net 0.15?

rusty21 commented 6 years ago

Installing 0.15 seems to want to target netstandard 1.3, which causes the error, downgrading to 0.14.1 seems to target monoandroid23 which does seem to work. Although when downgrading I also had to uninstall SuperSocket.ClientEngine.Core to get it to work.

kerryjiang commented 6 years ago

So the version you have problem now is 0.14.1?

rusty21 commented 6 years ago

Installing version 0.14.1 works, but installing version 0.15 does NOT work

kerryjiang commented 6 years ago

So the exception was thrown when you used websocket4net 0.14.1?

rusty21 commented 6 years ago

No the exception is thrown on version 0.15

kerryjiang commented 6 years ago

That means you use websocket4net 0.15 .nerstandard version in Xamarin. Is my understanding correct?

rusty21 commented 6 years ago

That is true

kerryjiang commented 6 years ago

Ok, let me see if we have a work around for this issue.

kerryjiang commented 6 years ago

I did a fix.

Could you build the websocket4net by your own with the latest source code?

And set the environment before you start the connection like the code below:

Environment.SetEnvironmentVariable("PREFER_DNS_IN_ADVANCE", "true");

rusty21 commented 6 years ago

I downloaded the source today built the WebSocket4Net.Xamarin solution in release mode. I then referenced the MonoDroid version of the dll in a test project. Put this code in the Main Activity

        System.Environment.SetEnvironmentVariable("PREFER_DNS_IN_ADVANCE", "true");
        var _websocket = new WebSocket("wss://ws-us2.pusher.com/app/1?protocol=5&client=pusher-dotnet-client&version=0.0.1")
        {
            EnableAutoSendPing = true,
            AutoSendPingInterval = 8
        };
        _websocket.Error += (sender, args) =>
        {
        };
        _websocket.Open();

I disabled wifi and mobile data on the device before launching the application. The error still persists. Same stack trace.

kerryjiang commented 6 years ago

Could you check the version numver of the dll you use? And Could you send me the stacktrace again? IT might be different with the old one.

kerryjiang commented 6 years ago

Can you use the .net standard version?

berdon commented 6 years ago

We too see this issue - it's most easily repeated when attempting to connect to an invalid host from Android.

I'll try adding the PREFER_DNS_IN_ADVANCE and see what happens.

berdon commented 6 years ago

PREFER_DNS_IN_ADVANCE had no appreciable effect; this is running off of:

The project is all using .NET Standard so I'm assuming it's using the .NET Standard versions of WebSocket4Net and SuperSocket. The wrapping project that consumes these packages are at .NET Standard 1.6.

Here's the stack trace:

The method or operation is not implemented.
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure (System.Exception exception, System.Int32 bytesTransferred, System.Net.Sockets.SocketFlags flags) [0x00000] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.MultipleConnectAsync.AsyncFail (System.Exception e) [0x0001a] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.MultipleConnectAsync.InternalConnectCallback (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs args) [0x000b7] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) [0x0000e] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.SocketAsyncEventArgs.Complete () [0x00000] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.Socket+<>c.<.cctor>b__306_3 (System.IAsyncResult ares) [0x00057] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Net.Sockets.SocketAsyncResult+<>c.<Complete>b__27_0 (System.Object state) [0x0000b] in <6c708cf596db438ebfc6b7e012659eee>:0 
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
migueldeicaza commented 6 years ago

There is a patch for Mono that we need to review, but would love a test case:

https://github.com/mono/mono/pull/6431

chrswgnr commented 6 years ago

i used the websocket4net 14.1 for a .net framework projekt and switched to .net standard now to use it within xamarin.android and xamarin.ios as well. Which just doesn't work with my implementation on android and ios because of the issue.

Any news on this? Or steps to get it work for the moment? Any help would be appreciated

psharrma commented 6 years ago

Hi,

This issue results in instant application crash on both android & iOS, I believe anyone using current version 0.15.2 will stumble upon this issue.

Any update or when can we get a fix for this issue? Also would really appreciate if we can get a workaround this issue.

kerryjiang commented 6 years ago

Ok, let me check this issue again.

chrswgnr commented 6 years ago

Well i updated visual studio and xamarin for my project recently and somehow i dont get that error anymore

psharrma commented 6 years ago

@NeroBoron ,

Are you testing on real device (Android/iOS) or emulator? Also I encountered this issue if i try to open a web-socket while device is in airplane mode.

chrswgnr commented 6 years ago

On real ones, iPhone 8 and Samsung Galaxy A5 2017 to be specific. Can test it with airplane mode in about a week

berdon commented 6 years ago

Fwiw, we no longer receive this exception after updating mono.

psharrma commented 6 years ago

This issue is resolved after upgrading mono.