Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot SimpMessageTemplate.convertAndSend不起作用_Spring Boot_Websocket_Spring Websocket - Fatal编程技术网

Spring boot SimpMessageTemplate.convertAndSend不起作用

Spring boot SimpMessageTemplate.convertAndSend不起作用,spring-boot,websocket,spring-websocket,Spring Boot,Websocket,Spring Websocket,我试图用SpringStarterWebSocket向订阅用户发送消息,但看起来convertAndSend根本不发送消息@SendTo工作 我的配置: @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { private final HttpHandshakeInterceptor httpHandshake

我试图用SpringStarterWebSocket向订阅用户发送消息,但看起来convertAndSend根本不发送消息@SendTo工作
我的配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final HttpHandshakeInterceptor httpHandshakeInterceptor;

public WebSocketConfig(HttpHandshakeInterceptor httpHandshakeInterceptor) {
    this.httpHandshakeInterceptor = httpHandshakeInterceptor;
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS().setInterceptors(httpHandshakeInterceptor);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/notifications");
    registry.setApplicationDestinationPrefixes("/app");
}
@Controller
public class NotificationController {

    private final NotificationService notificationService;
    private final SimpMessagingTemplate template;

    public NotificationController(NotificationService notificationService, SimpMessagingTemplate template) {
        this.notificationService = notificationService;
        this.template = template;
    }

    @MessageMapping("/echo")
    public void send(String text) {
        template.convertAndSend("/notifications", text);
    }
}
@Autowired
    NotificationService notificationService;

    @Test
    public void testSubscribe() throws ExecutionException, InterruptedException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer " + token);
        StompSession stompSession = stompClient.connect(WEBSOCKET_URI, new WebSocketHttpHeaders(httpHeaders),
                new StompSessionHandlerAdapter(){}).get();

        stompSession.subscribe(WEBSOCKET_TOPIC, new DefaultStompFrameHandler());

        stompSession.send("/app/echo", "Test".getBytes());
        Assert.assertEquals("Test", blockingQueue.poll(1, SECONDS));
    }

    class DefaultStompFrameHandler implements StompFrameHandler {
        @Override
        public Type getPayloadType(StompHeaders stompHeaders) {
            return byte[].class;
        }

        @Override
        public void handleFrame(StompHeaders stompHeaders, Object o) {
            blockingQueue.offer(new String((byte[]) o));
        }
    }
控制器:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final HttpHandshakeInterceptor httpHandshakeInterceptor;

public WebSocketConfig(HttpHandshakeInterceptor httpHandshakeInterceptor) {
    this.httpHandshakeInterceptor = httpHandshakeInterceptor;
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS().setInterceptors(httpHandshakeInterceptor);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/notifications");
    registry.setApplicationDestinationPrefixes("/app");
}
@Controller
public class NotificationController {

    private final NotificationService notificationService;
    private final SimpMessagingTemplate template;

    public NotificationController(NotificationService notificationService, SimpMessagingTemplate template) {
        this.notificationService = notificationService;
        this.template = template;
    }

    @MessageMapping("/echo")
    public void send(String text) {
        template.convertAndSend("/notifications", text);
    }
}
@Autowired
    NotificationService notificationService;

    @Test
    public void testSubscribe() throws ExecutionException, InterruptedException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer " + token);
        StompSession stompSession = stompClient.connect(WEBSOCKET_URI, new WebSocketHttpHeaders(httpHeaders),
                new StompSessionHandlerAdapter(){}).get();

        stompSession.subscribe(WEBSOCKET_TOPIC, new DefaultStompFrameHandler());

        stompSession.send("/app/echo", "Test".getBytes());
        Assert.assertEquals("Test", blockingQueue.poll(1, SECONDS));
    }

    class DefaultStompFrameHandler implements StompFrameHandler {
        @Override
        public Type getPayloadType(StompHeaders stompHeaders) {
            return byte[].class;
        }

        @Override
        public void handleFrame(StompHeaders stompHeaders, Object o) {
            blockingQueue.offer(new String((byte[]) o));
        }
    }
测试用例:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final HttpHandshakeInterceptor httpHandshakeInterceptor;

public WebSocketConfig(HttpHandshakeInterceptor httpHandshakeInterceptor) {
    this.httpHandshakeInterceptor = httpHandshakeInterceptor;
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS().setInterceptors(httpHandshakeInterceptor);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/notifications");
    registry.setApplicationDestinationPrefixes("/app");
}
@Controller
public class NotificationController {

    private final NotificationService notificationService;
    private final SimpMessagingTemplate template;

    public NotificationController(NotificationService notificationService, SimpMessagingTemplate template) {
        this.notificationService = notificationService;
        this.template = template;
    }

    @MessageMapping("/echo")
    public void send(String text) {
        template.convertAndSend("/notifications", text);
    }
}
@Autowired
    NotificationService notificationService;

    @Test
    public void testSubscribe() throws ExecutionException, InterruptedException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer " + token);
        StompSession stompSession = stompClient.connect(WEBSOCKET_URI, new WebSocketHttpHeaders(httpHeaders),
                new StompSessionHandlerAdapter(){}).get();

        stompSession.subscribe(WEBSOCKET_TOPIC, new DefaultStompFrameHandler());

        stompSession.send("/app/echo", "Test".getBytes());
        Assert.assertEquals("Test", blockingQueue.poll(1, SECONDS));
    }

    class DefaultStompFrameHandler implements StompFrameHandler {
        @Override
        public Type getPayloadType(StompHeaders stompHeaders) {
            return byte[].class;
        }

        @Override
        public void handleFrame(StompHeaders stompHeaders, Object o) {
            blockingQueue.offer(new String((byte[]) o));
        }
    }
因此,我有一个完整的队列@SentTo工作完美

UPD:convertAndSend工作,但只能从控制器发送


另外,我的英语很抱歉,希望你们能理解问题:-)

我也有同样的问题。我将
SimpMessagingTemplate
注入到我的其他类中,它不会发送。它通过
@Controller
类工作。你找到解决办法了吗,或者问题了吗?没有。当我找到解决方案时,我会把它贴在这里。我也有同样的问题。我将
SimpMessagingTemplate
注入到我的其他类中,它不会发送。它通过
@Controller
类工作。你找到解决办法了吗,或者问题了吗?没有。当我找到它时,我会在这里发布解决方案。