I read Spring docs about user destinations.
I would like to use convertAndSendToUser
method to send a message only to a particular user.
This is the java code:
@Controller
public class WebsocketTest {
@Autowired
public SimpMessageSendingOperations messagingTemplate;
@PostConstruct
public void init(){
ScheduledExecutorService statusTimerExecutor=Executors.newSingleThreadScheduledExecutor();
statusTimerExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
messagingTemplate.convertAndSendToUser("myuser","/queue/test", new Return("test"));
}
}, 5000,5000, TimeUnit.MILLISECONDS);
}
}
This is the client js code:
var socket = new WebSocket('ws://localhost:8080/hello');
stompClient = Stomp.over(socket);
stompClient.connect("myuser","mypass", function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function(greeting){
showGreeting(JSON.parse(greeting.body).value);
});
stompClient.subscribe('/user/queue/test', function(greeting){
showGreeting(JSON.parse(greeting.body).value);
});
});
First of all is it correct to conside the user value this one?
stompClient.connect("myuser",...
Why this test doesn't work? This user did not receveive any message.
If I switch destination to /topic/greetings
and change method to convertAndSend()
this works, but obviously as broadcast and not only to particular user as requested.
Little update
I tried to setup a reply to single user with this code:
@MessageMapping("/hello")
@SendToUser("/queue/test")
public Return test(){
return new Return("test");
}
This works, this is not what I need becase this reply only to a message from client. Seems that I cannot use convertAndSendToUser()
for unsollicited messaged from server.