spring websocket SimpMessageTemplate不执行任何操作

9jyewag0  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(217)

这里发生了什么?

@Controller
public class NotificaController {

  @Autowired
  private SimpMessagingTemplate messageSender;

  @MessageMapping("/chat")
  public void greeting(String message) {
    System.out.println("HelloWorld");
    messageSender.convertAndSend("/topic/messages", new Greeting("Hello")); // <-- Nothing Here!
  }

}

输出:

websocket配置。班

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

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

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

js客户端

export function connect() {
    var socket = new SockJS("http://192.168.1.63:5001/chat");
    stompClient = Stomp.over(socket);
    //stompClient.debug = null;
    stompClient.connect({}, (frame) => {
      stompClient.subscribe("/topic/messages", messageOutput => {
      console.log(messageOutput);  // <-- Nothing here!
    });
  });
}

export function disconnect() {
    if (stompClient != null) {
    stompClient.disconnect();
    }
    console.log("Disconnected");
}

export function sendMessage() {
  stompClient.send(
  "/app/chat",
  {},
  "this is only a test!"
  );
}

我已经尝试包围.convertandsend()以捕获某种异常,但似乎一切正常,只是什么也没发生。我已经读过很多类似的问题,但每个问题的回答都很差。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题