如何从 spring 启动将事件发布到 angular js 1.4

How to publish event to angular js 1.4 from spring boot

场景: 客户端(angular js 1.4)将调用 Rest 端点来获取数据,服务器(spring 引导)将处理文件列表并将 return 准确数据。为了处理文件列表,服务器将根据文件的数量花费时间。所以我已经实现了 STOMP 通知,以便向客户端发送通知说 "List of files have been processed and here is the bunch of files(result)"。

问题: Stomp连接建立成功,客户端也订阅了,但是当服务器发布事件时,客户端无法接收。

下面是我的代码片段:

WebSocketConfig.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").withSockJS();
  }

  @Override
  public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableSimpleBroker("/topic"); // Enables a simple in-memory broker
  }
}

SocketController.java

   @Controller
    public class SocketController {
    @SendTo("/topic/public")
        public String sendMessage() {
            LOGGER.info("====> chatMessage()");
            return "List updated successfully";
        }
    }

main.js

connect() {
                var socket = new SockJS('/ws');
                var stompClient = Stomp.over(socket);
                event.preventDefault();
                console.log("socket: ", socket);
                console.log("stompClient: ", stompClient);

                stompClient.connect({}, function (frame) {
                        console.log('Connected: ' + frame);   
                        stompClient.subscribe('/topic/public', function (payload) {
                        console.log("payload: ", payload);
                        var message = JSON.parse(payload.body);
                        console.log("message: ", message);
                        });
                    }, function (error) {
                     console.log("onError() called");
                                console.log("error: ",error);
                    });
        };

使用的脚本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

main.js 的 connect() 方法在单击按钮时被调用。我猜可能存在范围问题。

使用 SimpMessagingTemplate 将消息转换并发送到特定主题。

@Controller
public class SocketController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SocketController.class);

    @Autowired private SimpMessagingTemplate template;

    public void sendMessage(String message) {
        LOGGER.info("====> sendMessage:");
        this.template.convertAndSend("/topic/public", message);
    }
}