D / de.tavendo.autobahn.WebSocketReader:已创建 D / de.tavendo.autobahn.AutobahnReader:已创建 D / de.tavendo.autobahn.AutobahnConnection:读者创建并开始 D / de.tavendo.autobahn.WebSocketReader:正在运行 D / de.tavendo.autobahn.WebSocketWriter:已创建 D / de.tavendo.autobahn.AutobahnWriter:已创建 D / de.tavendo.autobahn.AutobahnConnection:作家创建并开始 D / de.tavendo.autobahn.AutobahnReader:无效的WAMP消息:丢失 数组关闭或无效的其他参数 D / de.tavendo.autobahn.WebSocketConnection:收到开放握手 D / de.tavendo.autobahn.AutobahnConnection:WAMP会议 163056330058b5c9094c0d3240526863成立 D / de.tavendo.autobahn.WebSocketConnection:收到WebSockets关闭 (1007 - null)D / de.tavendo.autobahn.WebSocketReader:run(): ConnectionLost D / de.tavendo.autobahn.WebSocketReader:已结束 D / de.tavendo.autobahn.WebSocketConnection:失败连接[code = 3, reason = WebSockets连接丢失 D / de.tavendo.autobahn.WebSocketReader:退出 D / de.tavendo.autobahn.WebSocketWriter:结束了D / MAIN_ACT__CHAT_CLOSE: WebSockets连接丢失D / de.tavendo.autobahn.WebSocketConnection: 工作线程已停止
public class MainActivity extends AppCompatActivity {
private final AutobahnConnection autobahnConnection = new AutobahnConnection();
private static class WebSocketPubSubEvent {
public int num;
public String name;
public boolean flag;
public Date created;
public double rand;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autobahnConnection.connect("ws://95.213.194.229:9000", new Autobahn.SessionHandler() {
@Override
public void onOpen() {
autobahnConnection.subscribe("chat/topic",
WebSocketPubSubEvent.class,
new Autobahn.EventHandler() {
@Override
public void onEvent(String topic, Object event) {
WebSocketPubSubEvent evt = (WebSocketPubSubEvent) event;
Log.d("MAIN_ACTIVITI__CHAT", evt.name);
Log.d("MAIN_ACTIVITI__CHAT", new Integer(evt.num).toString());
Log.d("MAIN_ACTIVITI__CHAT", evt.flag ? "TRUE" : "FALSE");
Log.d("MAIN_ACTIVITI__CHAT", new Integer(evt.created.getHours()).toString());
Log.d("MAIN_ACTIVITI__CHAT", new Double(evt.rand).toString());
}
}
);
autobahnConnection.sendTextMessage("Hello from DANIEL!!!");
}
@Override
public void onClose(int i, String s) {
Log.d("MAIN_ACT__CHAT_CLOSE", s);
}
});
}
}
如果删除autobahnConnection.sendTextMessage("Hello from DANIEL!!!");
,它就可以正常工作并且连接没有丢失:
D / de.tavendo.autobahn.WebSocketReader:已创建 D / de.tavendo.autobahn.AutobahnReader:已创建 D / de.tavendo.autobahn.AutobahnConnection:读者创建并开始 D / de.tavendo.autobahn.WebSocketReader:正在运行 D / de.tavendo.autobahn.WebSocketWriter:已创建 D / de.tavendo.autobahn.AutobahnWriter:已创建 D / de.tavendo.autobahn.AutobahnConnection:作家创建并开始 D / de.tavendo.autobahn.AutobahnReader:无效的WAMP消息:丢失 数组关闭或无效的其他参数 D / de.tavendo.autobahn.WebSocketConnection:收到开放握手 D / de.tavendo.autobahn.AutobahnConnection:WAMP会议 212228657158b64424d3300506265704成立
但是如果我从javascript客户端发送字符串消息也可以。
答案 0 :(得分:0)
为了向websocket服务器发送消息,请在订阅过程后写入:
autobahnConnection.publish("chat/topic", new Object() {
final String msg = "Message for you, ... <3";
});