我需要在 Android 应用程序中管理单个 WebSocket 连接。为此,我实现了一个 Web 应用程序,其中使用 Spring 设置了 WebSocket 消息代理,如其 quick start .
问题是我无法在 Android 应用程序中建立连接。我正在使用Autobahn Android ,但我无法连接订阅和发布主题(例如带有 STOMP 的 SockJS)。
服务器( Spring ):
<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 处理程序而不是消息代理进行连接,但这限制了我每个连接只能“监听”一个端点...有人可以帮助我吗?
最佳答案
AutobahnAndroid 实现了 WebSocket 和 WAMP ,而不是踩踏。与STOMP不同,WAMP同时提供发布和订阅以及远程过程调用。
要使用 WAMP,您需要一个 WAMP 路由器。您可以找到 WAMP here 的客户端和路由器实现.
关于java - WebSocket 消息代理的 Android 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26540065/