Android 为什么websocket连接会立即关闭。。。?
当我尝试在android中创建一个websocket连接以使用实现wamp时,该连接立即被创建并关闭 为什么会发生这种情况。。。。? 如何解决 下面给出了我的部分代码Android 为什么websocket连接会立即关闭。。。?,android,autobahn,autobahnws,Android,Autobahn,Autobahnws,当我尝试在android中创建一个websocket连接以使用实现wamp时,该连接立即被创建并关闭 为什么会发生这种情况。。。。? 如何解决 下面给出了我的部分代码 private void start() { // TODO Auto-generated method stub final String wsuri = "ws://192.168.0.102:8080/ws"; if (!connection.isConnected()) { Log.
private void start() {
// TODO Auto-generated method stub
final String wsuri = "ws://192.168.0.102:8080/ws";
if (!connection.isConnected()) {
Log.d("tag", "Not Connected");
}
connection.connect(wsuri, new ConnectionHandler() {
@Override
public void onOpen() {
// TODO Auto-generated method stub
Log.d("tag", "Connected to " + wsuri);
testPubSub();
}
@Override
public void onClose(int code, String reason) {
// TODO Auto-generated method stub
Log.d("tag", "disconnected");
}
});
}
protected void testPubSub() {
// TODO Auto-generated method stub
connection.subscribe(TOPIC, String.class, new EventHandler() {
@Override
public void onEvent(String topicUri, Object event) {
// TODO Auto-generated method stub
Log.d("tag", "Event Recieved");
}
});
}
很抱歉,没有更早地发布清单文件。下面给出了答案
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testwamp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
很可能您没有对manifest.xml授予权限
<uses-permission android:name="android.permission.INTERNET" />
请发布您的清单文件“为什么会发生这种情况…”您还没有发布日志,我们如何回答您?websocket握手成功吗?您可以通过检查请求头来检查这一点,您应该得到Connection:upgrade和101交换协议。如果握手正常,请检查您的网络是否有防火墙。这将有助于调试问题。此答案帮助我解决了一个类似的问题: