如何使用Spring Web套接字向特定的登录用户发送消息

时间:2019-03-28 07:47:56

标签: spring spring-boot spring-security websocket spring-websocket

我将使用mongodb来存储用户之间的聊天记录。 我为每个用户注册时都分配了一个UUID,并且每个用户登录后都会获得用于会话身份验证的JWT令牌。

用例:

用户将首先向他想聊天的用户发送聊天请求。如果用户接受聊天请求,则可以1-1聊天。


import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/chatWS") // ws connection url
        .setAllowedOrigins("*")
        .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
         registry.setApplicationDestinationPrefixes("/app")// to send data to ws
         .enableSimpleBroker("/topic"); // to subscribe for real time feed
    }

}

1 个答案:

答案 0 :(得分:0)

<base href="./"> 类中使用 ngOnInit(): void { alert('ngOnInit'); //not working } 方法,该方法在springframework中可用,用于向特定用户发送消息。

convertAndSendToUser

有关更多详细信息,请参阅以下link

相关问题