I am trying to implement simple websocket server on Android device. I would like to use https://github.com/TooTallNate/Java-WebSocket but connection fails after a long timeout. I see that it is more than two years old withou answers in issues etc. Do you have any suggestions or similar experience? Do you have any alternatives?
Thanks!
code:
import java.net.InetSocketAddress;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class WebsocketServer extends WebSocketServer
{
public WebsocketServer(InetSocketAddress address) {
super(address);
// TODO Auto-generated constructor stub
}
@Override
public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) {
// TODO Auto-generated method stub
}
@Override
public void onError(WebSocket arg0, Exception arg1) {
// TODO Auto-generated method stub
System.out.println(arg1.getStackTrace());
}
@Override
public void onMessage(WebSocket arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onOpen(WebSocket arg0, ClientHandshake arg1) {
// TODO Auto-generated method stub
System.out.println("new connection to " + arg0.getRemoteSocketAddress());
}
}
main
String ipAddress = "10.0.0.140"
InetSocketAddress inetSockAddress = new InetSocketAddress(ipAddress, 38301);
WebsocketServer wsServer = new WebsocketServer(inetSockAddress);
wsServer.run();
js client - it works with another ws server in python:
var ws;
$("#connect").click(function(e)
{
var ip = $("#address").val();
ws = new WebSocket("ws://" + ip);
ws.onopen = function()
{
alert("connected!");
};
});
I found hidden answer there.
I changed:
wsServer.run();
to
wsServer.start();
and it works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With