Android上的Websocket服务器

时间:2015-01-22 22:14:12

标签: android websocket server

我正在尝试在Android设备上实现简单的websocket服务器。 我想使用https://github.com/TooTallNate/Java-WebSocket但是在长时间超时后连接失败。我看到它已经超过两年了,你在问题等方面有答案。你有任何建议或类似经历吗?你有其他选择吗?

谢谢!

代码:

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());


}
}

    String ipAddress = "10.0.0.140"
    InetSocketAddress inetSockAddress = new InetSocketAddress(ipAddress, 38301);
    WebsocketServer wsServer = new WebsocketServer(inetSockAddress);
    wsServer.run();

js client - 它与python中的另一个ws服务器一起工作:

    var ws;

    $("#connect").click(function(e)
    {

            var ip = $("#address").val();
            ws = new WebSocket("ws://" + ip);
            ws.onopen = function()
            {
                 alert("connected!");
            };

    });

2 个答案:

答案 0 :(得分:1)

我找到了隐藏的答案there

我改变了:

wsServer.run();

wsServer.start(); 它的工作原理!

答案 1 :(得分:0)

这可以在android设备上使用,以运行Websocket客户端控制多个ESP8266-01