【发布时间】:2014-10-24 01:21:01
【问题描述】:
我需要在 Android 应用程序中管理单个 WebSocket 连接。为此,我实现了一个 Web 应用程序,其中使用 Spring 设置了一个 WebSocket 消息代理,作为它的quick start。
问题是我无法在我的 Android 应用程序中建立连接。我正在使用Autobahn Android,但我无法连接订阅和发布主题(例如带有 STOMP 的 SockJS)。
服务器(春季):
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/ws"></websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
@Controller
public class MessageController {
@MessageMapping("/ws")
@SendTo("/topic/poc")
public MyEntity proofOfConcept(String message) throws Exception {
return new MyEntity(message);
}
}
客户端(Autobahn Android):
final String wsuri = "ws://" + HOSTNAME + ":" + PORT + "/myapp/ws";
mConnection.connect(wsuri, new Wamp.ConnectionHandler() {
@Override
public void onOpen() {
mConnection.subscribe("/myapp/ws/topic/poc", MyEntity.class, new Wamp.EventHandler() {
@Override
public void onEvent(String topicUri, Object event) { }
});
}
@Override
public void onClose(int code, String reason) {
// ERROR: Could not connect to /HOSTNAME...
}
});
我设法使用简单的 spring 处理程序而不是消息代理进行连接,但这限制了我每个连接只能“侦听”一个端点...有人可以帮我吗?
【问题讨论】:
标签: java android spring websocket autobahn