I need to manage a single WebSocket connection in a Android application. For this I implemented a web application where set up a WebSocket Message Broker using Spring, as its 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...
}
});
I managed to connect using simple handlers of spring instead of message broker, but that limits me to "listen" only one endpoint per connection... Could anyone help me please?
AutobahnAndroid implements WebSocket and WAMP, not STOMP. Different from STOMP, WAMP provides both Publish & Subcribe and Remote Procedure Calls.
For using WAMP, you will need a WAMP Router. You can find client and router implementations for WAMP here.