TooTallNate / Java-WebSocket

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

Is it possible to use Android app as server? #1360

Closed marcelogoto91 closed 1 year ago

marcelogoto91 commented 1 year ago

Describe what you would like to know or do I'm trying to achieve a case where there's two Android (Java) apps: one is the server and the other is the client.

Describe the solution you'd considered I used the chat example in Java as reference to implement both server (https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/ChatServer.java) and client (https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/ChatClient.java). Had to make a few adjustments for Android but the core code is pretty much the same.

Additional context Problem is: server runs fine, but whenever I try to connect through the client I got the following errors:

image

java.lang.Exception: java.lang.IncompatibleClassChangeError: The method 'java.lang.Class java.lang.Object.getClass()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'org.java_websocket.drafts.Draft_6455' appears in /data/app/pt.card4b.app4mobi.mpstcp.debug-2/base.apk:classes34.dex)

Caused by: java.lang.IncompatibleClassChangeError: The method 'java.lang.Class java.lang.Object.getClass()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'org.java_websocket.drafts.Draft_6455' appears in /data/app/pt.card4b.app4mobi.mpstcp.debug-2/base.apk:classes34.dex)

Above are the errors on server side. Below the errors on client side:

W/System.err: an error occurred:java.net.ConnectException: failed to connect to /10.75.0.145 (port 38301) from /:: (port 45693): connect failed: ECONNREFUSED (Connection refused) I/System.out: closed with exit code -1 additional info: failed to connect to /10.75.0.145 (port 38301) from /:: (port 45693): connect failed: ECONNREFUSED (Connection refused)

Not sure if this is something I did wrong, or if the Java code doesn't work for Android or is a problem with the library. Could you help me please?

Below is how I implemented the code to start server and connecting with the client if helps

Server: image

Client: image

Where drafts is: private Draft[] drafts = {new Draft_6455()};

Also I already double checked and the IP / Port are the same for both server and client. Tried other random ports but it displayed the same error message. Devices are in the same Wi-Fi network.

marcelogoto91 commented 1 year ago

Just found out what happened based on https://github.com/TooTallNate/Java-WebSocket/issues/1353

Had to downgrade the version from 1.5.4 to 1.5.3 then it connected. If anyone has a similar problem just do this :)