I just followed up the instruction from spring to send back response to particular user, not broadcast message instead.but in the end, No response message can be sent back.
this is my 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 is 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 is 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") and SimpMessagingTemplate as well, totally cannot respond message to browser.