i have following websocket config:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/mu-autotest");
}
}
This is my WebsocketController:
@MessageMapping("/submitTask")
public void submitTask(SubmitTaskRequest request, @AuthenticationPrincipal Principal principal, @Headers Map<String, String> headers) throws InterruptedException {
log.info("headers: {}", headers);
String sessionId = headers.get("simpSessionId");
log.info("sessionId: {}", sessionId);
OAuth2AuthenticationToken oAuth2AuthenticationToken = (OAuth2AuthenticationToken) principal;
OAuth2User oAuth2User = oAuth2AuthenticationToken.getPrincipal();
String login = oAuth2User.getAttribute("login").toString();
log.info("oAuth2User login name: {}", login);
Thread.sleep(1000);
simpMessagingTemplate.convertAndSendToUser(login, "/queue/attempts", new SubmitTaskRequest(1L));
}
logged headers:
headers: {simpMessageType=MESSAGE, stompCommand=SEND, nativeHeaders={destination=[/app/submitTask], content-length=[11]}, simpSessionAttributes={}, simpHeartbeat=[J@5b8f30eb, simpUser=OAuth2AuthenticationToken [Principal=uz.mu.autotest.model.CustomOAuth2User@66ec02aa, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=CF808D518A7A09283969495D810BAF9D], Granted Authorities=[OAUTH2_USER, SCOPE_repo,workflow]], lookupDestination=/submitTask, simpSessionId=658beebd-6e98-f906-5b54-89f31f3b43cb, simpDestination=/app/submitTask}
logged sessionId:
sessionId: 658beebd-6e98-f906-5b54-89f31f3b43cb
logged login:
login name: eshonkulov-asliddin
here is how i am connecting from client:
const stompClient = new StompJs.Client({
brokerURL: 'ws://localhost:9091/mu-autotest'
});
function submitTask(callback) {
stompClient.activate();
// submit task websocket connection
stompClient.onConnect = (frame) => {
console.log('Connected: ' + frame);
stompClient.subscribe('/user/queue/attempts', (response) => {
console.log(JSON.parse(response.body));
addAccordionItem(JSON.parse(response.body))
disconnect();
});
buildAndTestRepository();
};
stompClient.onWebSocketError = (error) => {
console.error('Error with websocket', error);
};
stompClient.onStompError = (frame) => {
console.error('Broker reported error: ' + frame.headers['message']);
console.error('Additional details: ' + frame.body);
};
}
When i change controller to below code and use @SendToUser annotation, sending message working fine.
@MessageMapping("/submitTask")
@SendToUser("/queue/attempts")
public SubmitTaskRequest submitTask(SubmitTaskRequest request, @AuthenticationPrincipal Principal principal,@Payload String username, @Headers Map<String, String> headers) throws InterruptedException {
Thread.sleep(1000);
return new SubmitTaskRequest(1L);
}
But, I need a way to send message to client with simpMessagingTemplate.convertAndSendToUser()
In addition i tried to use sessionId to send message like:
convertAndSendToUser(sessionId, ..) but i didn't worked.
Asliddin Eshonkulov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.