crossbario / autobahn-java

WebSocket & WAMP in Java for Android and Java 8
https://crossbar.io/autobahn
MIT License
1.52k stars 426 forks source link

Crash on Android when try to connect an websocket server not initialized #478

Closed lujx1024 closed 4 years ago

lujx1024 commented 4 years ago

Here is the problem I encountered,when I try to established a connect to the websocket server,of course the client app runs on android,I have read all the issues and googleed for answer ,found nothing ,the connect code like this

        webSocketOptions.setReconnectInterval(3);
        webSocketConnection.setOptions(webSocketOptions);       
        webSocketConnection.connect(hostUrl, new IWebSocketConnectionHandler() {
            @Override
            public void onConnect(ConnectionResponse response) {                
            }

            @Override
            public void onOpen() {              
            }

            @Override
            public void onClose(int code, String reason) {
                }

            @Override
            public void onMessage(String payload) {
            }

            @Override
            public void onMessage(byte[] payload, boolean isBinary) {
            }

            @Override
            public void onPing() {              
            }

            @Override
            public void onPing(byte[] payload) {
            }

            @Override
            public void onPong() {
            }

            @Override
            public void onPong(byte[] payload) {
            }
            @Override
            public void setConnection(WebSocketConnection connection) {
        }
        });
    }

when the server is not initialized or the host url is wrong(but is a valid url),the app is crashed,and the crash reason is NullPointException,like this

java.lang.NullPointerException: Attempt to invoke interface method 'void java.util.concurrent.ExecutorService.shutdown()' on a null object reference
    at io.crossbar.autobahn.websocket.WebSocketConnection.closeWriterThread(WebSocketConnection.java:264)
    at io.crossbar.autobahn.websocket.WebSocketConnection.failConnection(WebSocketConnection.java:277)
    at io.crossbar.autobahn.websocket.WebSocketConnection.access$2800(WebSocketConnection.java:60)
    at io.crossbar.autobahn.websocket.WebSocketConnection$2.handleMessage(WebSocketConnection.java:653)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5280)
    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:963)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

I don't know if it is a bug or just me use it not correctly.if this is a bug,please fix it ,or please tell me how to use it correctly, I will be very appreciated

om26er commented 4 years ago

Thanks for reporting that issue. Can you check which version of autobahn-android you are using ? Looking at the stacktrace it seems you might be using an old version of the library. That specific issue that you reported was most likely fixed by https://github.com/crossbario/autobahn-java/pull/473/files

om26er commented 4 years ago

Version 20.2.1 fixed that issue. Please add that line to your project's gradle config.

implementation 'io.crossbar.autobahn:autobahn-android:20.2.1'
lujx1024 commented 4 years ago

thanks for your advise ,I used the 20.1.1 version I read from README file,after I change to 20.2.1,I got an onClose callback and a close reason matched CLOSE_CANNOT_CONNECT defined at the IWebSocketConnectionHandler. well , this may cause another problem, in this circumstance,normally I should wait some time and recall the connect method scheduled until the server is completely initialized and running. I tryed this way,but at the third time the client established the connection , the app crashed again,and this is the crash log java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@31a18629 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@55ccdae[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793) at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:298) at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:543) at java.util.concurrent.Executors$DelegatedScheduledExecutorService.scheduleAtFixedRate(Executors.java:650) at io.crossbar.autobahn.websocket.WebSocketConnection.setOptions(WebSocketConnection.java:531) at com.aoto.interaction.service.WebSocketClientService.initWebSocket(WebSocketClientService.java:94) at com.aoto.interaction.service.WebSocketClientService.connect(WebSocketClientService.java:210) at com.aoto.interaction.InteractionWebSocketClient.connect(InteractionWebSocketClient.java:113) at com.aoto.rd.voice.MainActivity$2.onClick(MainActivity.java:96) at android.view.View.performClick(View.java:4813) at android.view.View$PerformClick.run(View.java:20015) 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:5280) 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:963) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758) so, would you please tell me if I call reconnect method wrong ? or what is your suggestion to do that, anthing will be appreciated,and thanks again for you reply

lujx1024 commented 4 years ago

i avoid this error by use some strateges,thanks for your support