TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.54k stars 2.58k forks source link

NullPointerException for SSL Connection on Android #413

Closed tobiasrohloff closed 7 years ago

tobiasrohloff commented 8 years ago

Hi,

I'm using the WebSocketClient in an Android app and I receive the following NullPointerException from some users:

java.lang.NullPointerException: ssl == null
    at com.android.org.conscrypt.NativeCrypto.SSL_read_BIO(Native Method)
    at com.android.org.conscrypt.OpenSSLEngineImpl.unwrap(OpenSSLEngineImpl.java:477)
    at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:1014)
    at org.java_websocket.SSLSocketChannel2.unwrap(SSLSocketChannel2.java:150)
    at org.java_websocket.SSLSocketChannel2.processHandshake(SSLSocketChannel2.java:123)
    at org.java_websocket.SSLSocketChannel2.write(SSLSocketChannel2.java:192)
    at org.java_websocket.SocketChannelIOHelper.writeBlocking(SocketChannelIOHelper.java:76)
    at org.java_websocket.client.WebSocketClient$WebsocketWriteThread.run(WebSocketClient.java:440)
    at java.lang.Thread.run(Thread.java:818)

The exception was thrown on different devices and different versions of Android. I use the following configuration to initiate the secured WebSocket connection:

webSocketClient = new WebSocketClient(uri, new Draft_10(), headers, 0) {
    ...
}

try {
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, null, null);
    webSocketClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
    webSocketClient.connect();
} catch (Exception e) {
    ...
}

I couldn't reproduce the issue on my debug devices. Any idea how I can avoid this or why it crashes sometimes? Can I catch the exception somehow? Looks like it is deeply nested in the library or Android itself.

Thanks

erobic commented 7 years ago

We are having this exact same issue!

yasserf commented 7 years ago

Same, any idea on why this happens?

marci4 commented 7 years ago

Hello @yasserf,

could you provide me with a test application where I can try to fix this problem.

Greetings marci4

marci4 commented 7 years ago

possible cause #424

marci4 commented 7 years ago

@erobic @rhlff could you provide me a test application?

tobiasrohloff commented 7 years ago

Unfortunately, I couldn't reproduce the issue on any of my debug devices or the emulator yet. I got the error logs from the Google Play Console.

marci4 commented 7 years ago

Hello @rhlff,

I changed some code with #488.

Hope to resolve those issues with these changes!

Greetings marci4

tobiasrohloff commented 7 years ago

Hi @marci4,

I already updated our app to version 1.3.3 and I will monitor if the issue will appear again. Hope for the best :)

arpitjoshi08 commented 7 years ago

i am using socket 0.9 connection and for that i am using https://github.com/Gottox/socket.io-java-client with javewebsocket 1.3.3 jar and getting issue is some devices ;

Fatal Exception: java.lang.NullPointerExceptionssl == null Raw Text

  | com.android.org.conscrypt.NativeCrypto.SSL_write_BIO (NativeCrypto.java)   | javax.net.ssl.SSLEngine.wrap (SSLEngine.java:1093)   | org.java_websocket.SSLSocketChannel2.wrap (SSLSocketChannel2.java:141)   | org.java_websocket.SSLSocketChannel2.write (SSLSocketChannel2.java:195)   | org.java_websocket.SocketChannelIOHelper.writeBlocking (SocketChannelIOHelper.java:76)   | org.java_websocket.client.WebSocketClient$WebsocketWriteThread.run (WebSocketClient.java:440)

i am not getting the reason why is it happening , can anyone help for that to resolved this? and using 1.3.4 getting other issue like many time disconnect connection of node. getting this issue;

07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at com.sockettollnate.socket.WebsocketTransport.onMessage(WebsocketTransport.java:188) 07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:339) 07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:448) 07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:201) 07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:276) 07-29 02:43:50.077 7325-8261/com.sockettollnate W/System.err: at java.lang.Thread.run(Thread.java:841)

marci4 commented 7 years ago

Hello @arpitjoshi08,

first of all I have no idea if https://github.com/Gottox/socket.io-java-client is implementing the websocket protocol correctly. So there also may be issues there?

Could you please tell me on what specific device versions you are getting this error? Does this only happen in production or also in the emulator?

1.3.4 introduces a feature which detects if the other endpoint is not reachable any more (due to dropped internet etc), so in short all the connections got lost but the underlaying runtime did not detect it on its own.

Greetings marci4

arpitjoshi08 commented 7 years ago

hii @marci4 , i am getting issue in samsung SM-G570F (6.0) device (with 1.3.3). and if using 1.3.4 jar i am getting issue https://github.com/Gottox/socket.io-java-client/issues/4 so thats why i am using 1.3.3. and is there any sample available to connect socket using this library if avail for android , please provide me link for that

marci4 commented 7 years ago

Hello @arpitjoshi08,

I will try to test this in an emulator if I got the time on the weekend.

Apart from this, no there is no example application for the socket.io-java-client. Maybe get in contact to the maintainer to help you with your issue!

Greetings marci4

marci4 commented 7 years ago

No feedback!

If you still ave this issue, please open a separate issue! Greetings marci4