java - private messages spring websocket -
i learning spring websocket , stuck on how can send messages specific users using @destinationvariable("username")
here code
configuration @configuration @enablewebsocketmessagebroker public class websocketcontextconfig extends abstractsessionwebsocketmessagebrokerconfigurer<expiringsession> { @override protected void configurestompendpoints(stompendpointregistry registry) { registry.addendpoint("/ws-cfg").withsockjs(); } @override public void configuremessagebroker(messagebrokerregistry registry) { registry.enablestompbrokerrelay("/queue/","/topic","/exchange/") .setrelayhost("localhost"); registry.setapplicationdestinationprefixes("/app"); } }
controller
@messagemapping("/chat.private.{username}") public void filterprivatemessage(@payload message message, @destinationvariable("username") string username, principal principal) { this.simpmessagingtemplate.convertandsendtouser(username, "/queue/chat.message", message); }
the client code
var stomp = null; stomp = stomp.over(new sockjs("/ws-cfg")); stomp.connect('guest', 'guest', function(frame) { stomp.subscribe("/user/queue/chat.message", function (frame) { dysplaymessage(json.parse(frame.body)); }); }) $("#sendmessage").click(function (event) { event.preventdefault(); var message = $('#text').val(); var username="user@gmail.com";// lost in part, supose must @destinationvariable("username") destination = "/app/chat.private."+to; stomp.send(destination, {}, json.stringify({'text': message})); $('#text').val(""); });
i using websocket spring security. how can set @destinationvariable("username")
on stomp.send
method. in advance.
check out spring websocket chat sample has looking for: https://github.com/salmar/spring-websocket-chat
Comments
Post a Comment