有人可以告诉我如何从 android 客户端连接到 spring stomp web socket。
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();
}
}
PushMessageNotifier.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 客户端来连接到这个套接字和主题。 提前致谢
最佳答案
自问这个问题以来已经很久了,但它出现在谷歌上并且没有收到任何被接受的答案,因此......
- 这可以使用以下库轻松实现 https://github.com/NaikSoftware/StompProtocolAndroid
- 重要说明:哪些文档未能有效传达:
在
SockJS
的情况下,假设我们必须使用像http://server-host:8080/stompEndpoint
这样的 URL,但在 android 中它必须是ws://服务器主机:8080/stompEndpoint/websocket
。区别在于 1)http
vsws
2) 在 URL 的 android 变体中附加/websocket
关于android - 适用于 Android 的 Spring stomp web 套接字客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059553/