SimpMessagingTemplate不在spring boot中发送消息

Zah*_*sar 7 java spring stomp spring-boot

大家好我正在尝试向Stomp Endpoints发送消息,但我没有得到任何.我正在使用弹簧启动和stomp以下是我的课程

@Controller
public class GreetingController {

  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(HelloMessage message) throws Exception {
    System.out.println(message.getName());
    Thread.sleep(13000); // simulated delay
    return new Greeting("Hello, " + message.getName() + "!");
  }

}

@Controller                                                
public class Testcont {

  @Autowired
  private SimpMessagingTemplate messageSender;

  @RequestMapping(value="/Users/get",method=RequestMethod.POST)
  @ResponseBody
  public String getUser(@RequestParam(value = "userId") String userId, @RequestParam(value = "password") String password, @RequestParam(value="port") String port, HttpServletRequest request) {
    HelloMessage mess=new HelloMessage();
    mess.setName(userId);
    messageSender.convertAndSend("/app/hello",mess);
    return "Success";

}
Run Code Online (Sandbox Code Playgroud)

和我的websocket配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

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

}
Run Code Online (Sandbox Code Playgroud)

我没有在控制台中出现任何错误.上述代码适用于Web浏览器.

小智 8

我在使用 WebSocket 时遇到同样的问题。对于这篇文章来说为时已晚,但对于正在寻找答案的人来说可能有用。

\n\n

SimpMessagingTemplate -> 使用“主题”而不是“应用程序”\n所以 \nmessageSender.convertAndSend("/topic/hello",mess); 将是一个解决方案。

\n\n

来自上面提到的 WebSocket 文档:

\n\n

"clientInboundChannel"\xe2\x80\x89\xe2\x80\x94\xe2\x80\x89用于传递从 WebSocket 客户端收到的消息。

\n\n

"clientOutboundChannel"\xe2\x80\x89\xe2\x80\x94\xe2\x80\x89用于将服务器消息发送到 WebSocket 客户端。

\n\n

"brokerChannel"\xe2\x80\x89\xe2\x80\x94\xe2\x80\x89,用于从服务器端应用程序代码中向消息代理发送消息。

\n\n
    \n
  1. 客户端连接到“ http://localhost:8080/portfolio ”,一旦建立 WebSocket 连接,STOMP 帧就开始在其上流动。(在您的情况下:\nregistry.addEndpoint("/hello").withSockJS(),您使用“hello”,在文档中,它使用“portfolio”。)

  2. \n
  3. 客户端发送带有目标标头“/topic/hello”的 SUBSCRIBE 帧。接收并解码后,消息将发送到“clientInboundChannel”,然后路由到存储客户端订阅的消息代理。(这是订阅 WebSocket 的 UI,这里没有相关代码。没关系。)

  4. \n
  5. 客户端发送 SEND 帧到“/app/hello”。“/app”前缀有助于将其路由到带注释的控制器。去除“/app”前缀后,目标的剩余“/hello”部分将映射到 GreetingController 中的 @MessageMapping 方法。(在您的代码中:@MessageMapping(“/ hello”)将接收帧。)

  6. \n
  7. 从 GreetingController 返回的值将转换为 Spring 消息,其负载基于返回值和默认目标标头“/topic/hello”(源自输入目标,其中“/app”替换为“/topic”)。生成的消息被发送到“brokerChannel”并由消息代理处理。(因此,当您尝试将数据放入服务器中的WebSocket时:messageSender.convertAndSend("/app/hello",mess); 应该更改为messageSender.convertAndSend("/topic/hello",mess),因为它已经在服务器中,我们不需要目的地前缀,但我们需要代理前缀。)

  8. \n
  9. 消息代理找到所有匹配的订阅者,并通过“clientOutboundChannel”向每个订阅者发送一个 MESSAGE 帧,消息在“clientOutboundChannel”中被编码为 STOMP 帧并在 WebSocket 连接上发送。

  10. \n
\n


Art*_*lan 7

SimpMessagingTemplatebean是完全相同的经纪部分(AbstractMessageBrokerConfiguration):

@Bean
public SimpMessagingTemplate brokerMessagingTemplate() {
    SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel());
    String prefix = getBrokerRegistry().getUserDestinationPrefix();
    if (prefix != null) {
        template.setUserDestinationPrefix(prefix);
    }
Run Code Online (Sandbox Code Playgroud)

由于您不将消息发送到代理目的地(/app/在您的情况下),因此该消息将被忽略AbstractBrokerMessageHandler.checkDestinationPrefix(destination).

如果你想通过相同的方式处理内部消息@MessageMapping,你应该clientInboundChannel直接使用,它由以下提供SimpAnnotationMethodMessageHandler:

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());
Run Code Online (Sandbox Code Playgroud)

我猜你可以创建自己的SimpMessagingTemplate实例clientInboundChannel,类似于那个brokerMessagingTemplatebean.你会没事的.

  • 我看到了类似的问题,答案和后续问题都不清楚. (2认同)