gpt4 book ai didi

java - AndroidAsync websockets 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:14 26 4
gpt4 key购买 nike

我正在使用这个 AndroidSync koush 的库,用于创建 websocket(服务器/客户端)并在两个 android 设备之间传输数据。这两个设备通过 wifi 连接(一个是 Wifi AP,另一个连接到它)。发送请求 4-5 秒后,客户端设备出现 TimeoutException。这是我到目前为止所做的..

ServerActivity.java

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);

mSockets = new ArrayList<WebSocket>();
mAsyncHttpServer = new AsyncHttpServer();
mWebSocketCallback = new AsyncHttpServer.WebSocketRequestCallback() {
@Override
public void onConnected(final WebSocket webSocket, RequestHeaders headers) {
mSockets.add(webSocket);
webSocket.send("Welcome Client");
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
try {
if (ex != null)
Log.e("WebSocket", "Error");
} finally {
mSockets.remove(webSocket);
}
}
});
webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
Log.d("SERVERTAG",s);
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
});
}
};

mAsyncHttpServer.websocket("/",mWebSocketCallback);
mAsyncHttpServer.listen(Utils.PORT_NUMBER);

Button sendButton = (Button) findViewById(R.id.sendButtonS);
sendButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
for(WebSocket socket : mSockets) {
socket.send("Server sent a string");
}
}
});

}

ClientActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

//Resolve IP address
int ipAddress = mWifiManager.getConnectionInfo().getIpAddress();
String hostAddress = Formatter.formatIpAddress(ipAddress);
hostAddress = "http://" + hostAddress + ":" +Utils.PORT_NUMBER;
Log.d("CLIENTTAG", "address is " + hostAddress);

mWebSocketConnectCallback = new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
return;
}
webSocket.send("Hello Server");
webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
Log.d("CLIENTTAG",s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
});
}
};
mAsyncHttpClient = AsyncHttpClient.getDefaultInstance();
mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback);

}

这是我在客户端设备的 logcat 中得到的。

10-21 19:50:49.289      742-945/com.haloappstudio.musichub W/System.err﹕ java.util.concurrent.TimeoutException
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.http.AsyncHttpClient$2.run(AsyncHttpClient.java:240)
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:683)
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:700)
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:608)
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37)
10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:557)

我以前没有真正做过套接字编程。任何人都可以帮我解决这个问题吗?

感谢任何帮助。

最佳答案

我发现了问题,因为@jrandaz 说问题出在服务器的 IP 地址上。

结果

WifiManager.getConnectionInfo().getIpAddress()

返回设备自己的 IP 地址,而不是它所连接的 wifi 热点设备的地址。我使用了 192.168.43.1,它是 android 中 wifi 热点的默认 IP 地址,并且有效。

关于java - AndroidAsync websockets 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com