有人能告诉我如何从android客户端连接到spring-stomp网络套接字吗。
WebSocketConfig.java
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/pushticket");
config.setApplicationDestinationPrefixes("/rest");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ticket").withSockJS();
}
}
PushMessageNotificationer.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
@Service
@EnableAsync
public class PushMessageNotifier {
private SimpMessagingTemplate simpMessagingTemplate;
@Autowired
public PushMessageNotifier(SimpMessagingTemplate simpMessagingTemplate) {
this.simpMessagingTemplate = simpMessagingTemplate;
}
@Async
public Boolean pushToUI(TicketView ticketView) {
Boolean result = false;
if (null != ticketView) {
this.simpMessagingTemplate.convertAndSend("/pushticket/ticket", ticketView);
result = true;
}
return result;
}
}
请告诉我如何从android应用程序连接到这个插座?甚至我都不知道我需要使用哪个android客户端来连接到这个套接字和主题。提前感谢
这个问题被问到已经很久了,但它出现在谷歌上,没有收到任何公认的答案,因此。。。
- 这可以使用以下库轻松实现https://github.com/NaikSoftware/StompProtocolAndroid
-
重要提示:哪些文档无法有效沟通:在
SockJS
的情况下,假设我们必须使用类似http://server-host:8080/stompEndpoint
的URL,但在android中,它必须是ws://server-host:8080/stompEndpoint/websocket
。其中差异1)http
与ws
2)URL的android变体中的附加/websocket
我也遇到了同样的问题。我发现spring提供了默认的web套接字连接器库,我用它们来测试服务器端的代码。
@LocalServerPort
private int port;
private SockJsClient sockJsClient;
private WebSocketStompClient stompClient;
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
@Before
public void setup() {
List<Transport> transports = new ArrayList<>();
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
this.sockJsClient = new SockJsClient(transports);
this.stompClient = new WebSocketStompClient(sockJsClient);
this.stompClient.setMessageConverter(new MappingJackson2MessageConverter());
}
@Test
public void getGreeting() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> failure = new AtomicReference<>();
StompSessionHandler handler = new TestSessionHandler(failure) {
@Override
public void afterConnected(final StompSession session, StompHeaders connectedHeaders) {
session.subscribe("/user/testuser1/reply", new StompFrameHandler() {
@Override
public Type getPayloadType(StompHeaders headers) {
return Greeting.class;
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
MessageSendDTO greeting = (MessageSendDTO) payload;
try {
System.out.println("#################### "+greeting.getWriter());
assertEquals("testuser1", greeting.getWriter());
} catch (Throwable t) {
failure.set(t);
} finally {
session.disconnect();
latch.countDown();
}
}
});
try {
MessageSendDTO dto= new MessageSendDTO();
dto.setWriter("testuser1");
dto.setReceiver("testuser2");
dto.setMessageBody("TESTING ");
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
//session.send("/app/newMessage", new HelloMessage("Spring"));
session.send("/app/newMessage", dto);
} catch (Throwable t) {
failure.set(t);
latch.countDown();
}
}
};
this.stompClient.connect("ws://localhost:8080/newMessage", this.headers, handler, this.port);
if (latch.await(20, TimeUnit.SECONDS)) {
if (failure.get() != null) {
throw new AssertionError("", failure.get());
}
}
else {
fail("Greeting not received");
}
此代码运行良好。你可以将其用于你的android,只需很少的修改,所有的依赖项都可以从这里下载。