android - 适用于 Android 的 Spring stomp web 套接字客户端

标签 android websocket push-notification spring-boot stomp

有人可以告诉我如何从 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 vs ws 2) 在 URL 的 android 变体中附加 /websocket

关于android - 适用于 Android 的 Spring stomp web 套接字客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059553/

相关文章:

android - AchartEngine Bar Graph Android 中Repaint 的使用方法

ios - 推送通知在 UIApplication LaunchOptions RemoteNotificationKey 中不起作用

javascript - 如何向网络应用程序和浏览器发送推送通知?

ios - 推送解析 - didFinishLaunchingWithOptions 中的 launchOptions 为 null

java - 在 XML 资源中存储 R.id.elements

android - 如何在Grails中接收HTTPS发布请求?

java - 无法使用 asynchTask 从服务器获取响应

javascript - 如何使用 Websockets 连接到 MQTT Broker?

angular - E2E Angular 6 websocket 模拟

node.js - Websocket 传输可靠性(重新连接期间的 Socket.io 数据丢失)