本文介绍了如何在连接到spring websocket时向用户发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在连接到spring websocket时向用户发送消息,我已经

I want to send message to user when he connects to spring websocket, I've

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Autowired
    private GenervicSerice<User> userService;
    @Autowired
    private SimpMessagingTemplate template;
    private CurrentUser currnetUser;
    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        // TODO Auto-generated method stub
        stompEndpointRegistry.addEndpoint("/ws").withSockJS();
    }

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

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {

        registration.setInterceptors(myChannelInterception());
        try {
            updateNotificationAndBroadcast();

        } catch (Exception e) {
            return;
        }
    }

    @Bean
    public MyChannelInterception myChannelInterception() {
        return new MyChannelInterception();
    }


    private void updateNotificationAndBroadcast() {
        try {
            template.convertAndSend("/queue/notify", "Greetings");
        } catch (Exception e) {
            System.out.println("Error message is " + e.getMessage() + "\n\n\n" + "Caused by " + e.getCause()
                    );
        }

    }

}

MyChannelInterception类是

MyChannelInterception class is

public class ImtehanChannelInterception extends ChannelInterceptorAdapter {

    private CurrentUser currnetUser;

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {

        MessageHeaders headers = message.getHeaders();
        SimpMessageType type = (SimpMessageType) headers.get("simpMessageType");
        String simpSessionId = (String) headers.get("simpSessionId");
        currnetUser = new CurrentUser();
        if (type == SimpMessageType.CONNECT) {
            Principal principal = (Principal) headers.get("simpUser");
            currnetUser.setCurrentUserEmail(principal.getName());
            System.out.println("WsSession " + simpSessionId
                    + " is connected for user " + principal.getName());
        } else if (type == SimpMessageType.DISCONNECT) {
            System.out.println("WsSession " + simpSessionId
                    + " is disconnected");
        }

        return message;
    }

}

通过这个我获得有关新的信息已连接的用户,但中的方法 updateNotificationAndBroadcast()> WebSocketConfig 未向新登录的用户发送邮件。

through this I get information about new connected user but the method updateNotificationAndBroadcast() in WebSocketConfig is not sending messages to new logged-in users.

推荐答案

我会创建 SessionSubscribeEvent 监听器并使用 SimpMessagingTemplate 里面。

I would create SessionSubscribeEvent listener and use SimpMessagingTemplate inside.

Btw, configureClientInboundChannel 只被调用一次(不是每个连接的用户都有)。所以你必须处理在拦截器内发送消息。

Btw, configureClientInboundChannel is called only once (not for every user connected). So you have to handle sending message inside interceptor.

尝试这样的事情:

@Service
public class SomeSubscribeListener {

    private SimpMessagingTemplate template;

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

    @EventListener
    public void handleSubscribeEvent(SessionSubscribeEvent event) {
        template.convertAndSendToUser(event.getUser().getName(), "/queue/notify", "GREETINGS");
    }
}

我希望这会有所帮助

这篇关于如何在连接到spring websocket时向用户发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:36