无法连接到SockJS

时间:2018-11-21 19:05:36

标签: java typescript spring-boot angular6

我正在尝试连接Web套接字与Spring Boot和Sock&Stompjs(Angular 6) 以下是我的配置
console errors [1]

这是我的spring代码配置:

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
    stompEndpointRegistry.addEndpoint("/socket")
            .setAllowedOrigins("*")
            .withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/chat");
    registry.setApplicationDestinationPrefixes("/app");
}

这是我的控制器:

private final SimpMessagingTemplate template;

@Autowired
public WebSocketController(SimpMessagingTemplate template) {
    this.template = template;
}

@GetMapping("/notify")
public void onRecieveMessage(String message){

    this.template.convertAndSend("/chat", new SimpleDateFormat("HH:MM:SS").format(new Date())+"-"+message );
}

我以前用来连接的Angular方法:

  initializeWebSocketConnection() {
    const socket = new SockJs('http://localhost:8080/socket');
    this.stompClient2 = Stomp.over(socket);
    const that = this;
    this.stompClient2.connect('', '', function(frame) {
        that.stompClient2.subscribe('/chat', (message) => {
            if (message.body) {
                $('.chat').append('<div class=\'message\'>' + message.body + '</div>');
                console.log(message.body);
            }
        });
    });
}

0 个答案:

没有答案