Java 在spring boot和angular中使用WebSocket向客户端发送触发器
我想在spring boot中启动一个TriggerMay,它是一个从backendbased发送给用户ID为xyz的特定用户的通知 我发现的一个方法是: 最初,我连接到websocket端点并订阅channel/user/Notifications/xyz 以下是我的angular typescript中的相关代码Java 在spring boot和angular中使用WebSocket向客户端发送触发器,java,angular,spring,spring-boot,spring-websocket,Java,Angular,Spring,Spring Boot,Spring Websocket,我想在spring boot中启动一个TriggerMay,它是一个从backendbased发送给用户ID为xyz的特定用户的通知 我发现的一个方法是: 最初,我连接到websocket端点并订阅channel/user/Notifications/xyz 以下是我的angular typescript中的相关代码 connectToUserWebSocket(userId) { let socket = new SockJS('http://localhost:5000/fe
connectToUserWebSocket(userId) {
let socket = new SockJS('http://localhost:5000/fellowGenius');
this.ws = Stomp.over(socket);
let that = this;
this.ws.connect(
{},
(frame) => {
that.ws.subscribe('/user/Notifications/' +userId, (message) => {
console.log("user subscribed");
});
},
(error) => {
alert('STOMP error ' + error);
}
);
}
现在,一旦我订阅了我的频道。我想向客户端发送一个触发器,该触发器由后端本身启动,因此我在java服务中运行了一段代码。
我的相关java代码是:
@SendTo("/user/Notifications/{userId}")
public String sendMeetingNotificationWebSocket(@DestinationVariable String userId) {
return "hello";
}
我的websocket配置是:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/fellowGenius").setAllowedOrigins("*").addInterceptors(new HttpSessionHandshakeInterceptor()).withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/inbox/","/user/Notifications/");
}
}
但问题是,即使我在spring引导控制台中也可以看到一个web套接字连接。
但是我没有从客户端的函数得到响应
请帮我解决这个问题。您不只是在console中多次获得用户订阅吗?我没有收到您的问题