Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket server on Android

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!");
            };

    });
like image 972
sbx Avatar asked Jan 22 '15 22:01

sbx


1 Answers

I found hidden answer there.

I changed:

wsServer.run();

to

wsServer.start(); and it works!

like image 97
sbx Avatar answered Oct 11 '22 21:10

sbx