我想在
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客户端 – 它与python中的另一个ws服务器一起使用:
var ws;
$("#connect").click(function(e)
{
var ip = $("#address").val();
ws = new WebSocket("ws://" + ip);
ws.onopen = function()
{
alert("connected!");
};
});