首页 文章

适用于WebSocket Message Broker的Android客户端

提问于
浏览
2

我需要在Android应用程序中管理单个WebSocket连接 . 为此,我实现了一个Web应用程序,其中使用Spring设置WebSocket Message Broker,作为quick start .

The problem is that I could not make a connection in my Android application. I'm using Autobahn Android, but I can not connect to subscribe and publish on topics (like SockJS with STOMP).

Server (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);
    }
}

Client (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而不是消息代理的简单处理程序进行连接,但这限制了我每个连接只“监听”一个 endpoints ......有人可以帮我吗?

1 回答

  • 3

    AutobahnAndroid实现了WebSocket和WAMP,而不是STOMP . 与STOMP不同,WAMP提供发布和订阅以及远程过程调用 .

    要使用WAMP,您需要一个WAMP路由器 . 您可以找到WAMP here的客户端和路由器实现 .

相关问题