mattleibow/square-bindings

Android sending message with TLS

Closed this issue · 1 comments

I have a hello world example using the OKHttp-ws binding library. Everything works fine when I communicate with an unsecured server. When I communicate with a ssl / tls server I receive errors when sending messages. Here is my sending code.

  public async void Send(string message)
        {
            //IWebSocket 
            if (_socket == null)
                return;

            try
            {
                message = "\"" + message + "\"";
                var buffer = new OkBuffer();
                buffer.WriteString(message, Charset.DefaultCharset());
                _socket.SendMessage(WebSocketPayloadType.Text, buffer);
                buffer.Close();
            }
            catch(Exception ex)
            {
                //
            }
        }

Here is the exception's stack trace

{Android.OS.NetworkOnMainThreadException: Exception of type 'Android.OS.NetworkOnMainThreadException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00063] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:571
at Square.OkHttp.WS.IWebSocketInvoker.SendMessage (Square.OkHttp.WS.WebSocketPayloadType p0, Square.OkIO.OkBuffer p1) [0x0005e] in :0
at Realtime.Messaging.Droid.WebsocketConnection+d__19.MoveNext () [0x0005e] in E:\Projects\RT Debug\Realtime\Realtime.Messaging\Realtime.Messaging.Droid\WebsocketConnection.cs:137
--- End of managed exception stack trace ---
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:724)
at okio.Okio$1.write(Okio.java:80)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.squareup.okhttp.internal.ws.WebSocketWriter.writeFrame(WebSocketWriter.java:233)
at com.squareup.okhttp.internal.ws.WebSocketWriter.sendMessage(WebSocketWriter.java:180)
at com.squareup.okhttp.internal.ws.RealWebSocket.sendMessage(RealWebSocket.java:110)
at md5282f1122c1313907b9bf274dd2c2f344.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
at md5282f1122c1313907b9bf274dd2c2f344.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:29)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
}

Resolved. I needed to wrap the call in a Task.

Task.Factory.StartNew(() =>
{
});