websocket - Why Spring SendToUser does not work -
i followed instruction spring send response particular user, not broadcast message instead.but in end, no response message can sent back.
this js code:
var stompclient = null; function setconnected(connected) { document.getelementbyid('connect').disabled = connected; document.getelementbyid('disconnect').disabled = !connected; document.getelementbyid('conversationdiv').style.visibility = connected ? 'visible': 'hidden'; document.getelementbyid('response').innerhtml = ''; } function connect() { var socket = new sockjs('reqsample'); stompclient = stomp.over(socket); stompclient.connect({}, function(frame) { setconnected(true); console.log('connected: ' + frame); stompclient.subscribe('user/queue/resp', function(resp) { var body = json.parse(resp.body); showresp('cmid:' + body.cmid + ',reqname:' + body.reqname + ',custno:' + body.custno + ',qty:' + body.quantity); }); stompclient.subscribe('user/queue/errors', function(resp) { var body = json.parse(resp.body); showresp(body); }); }); } function disconnect() { stompclient.disconnect(); setconnected(false); console.log("disconnected"); } function sendname() { var name = document.getelementbyid('name').value; stompclient.send("/app/req", {}, json.stringify({ 'name' : name })); }
this controller:
@controller public class messagecontroller { @messagemapping("/req") @sendtouser("/queue/resp") public respmessage greeting(reqmessage message, principal pc) throws exception { system.out.println("---- received message: " + message == null ? "null" : message.getname()); system.out.println("---- received user info: " + pc.getname()); respmessage rm = new respmessage(); rm.setcmid("cm_01"); rm.setcustno("cust_02"); rm.setquantity("1000"); rm.setreqname(message.getname()); return rm; } @messageexceptionhandler @sendtouser("/queues/errors") public string handleexception(exception ex) { system.out.println(ex); return ex.getmessage(); } }
this spring configuration:
<context:component-scan base-package="wx.poc7" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/js/**" location="/js/" /> <websocket:message-broker application-destination-prefix="/app" user-destination-prefix="/user"> <websocket:stomp-endpoint path="reqsample"> <websocket:sockjs /> </websocket:stomp-endpoint> <websocket:simple-broker prefix="/queue, /topic" /> </websocket:message-broker>
pls help. thx in advance.
i have tried use @sendtouser ,@sendtouser("/queue/resp") , simpmessagingtemplate well, totally cannot respond message browser.
user destinations prefix /user
seems missing /
in subscription destination. change user/queue/resp
/user/queue/resp
receive messages on client side.
Comments
Post a Comment