[英]how to get websocket response with spring+stomp+sockjs
I use spring+stomp to broadcast a message to websocket client, but how can I know the result is success? 我使用spring + stomp将消息广播到websocket客户端,但是我怎么知道结果是成功的呢? I use the setSendTimeout method and catch Exception to detect whether the convertAndSend is success, but it doesn't work! 我使用setSendTimeout方法并捕获Exception来检测convertAndSend是否成功,但是不起作用! java code Java代码
@Resource
private SimpMessagingTemplate simpMessagingTemplate;
@RequestMapping("/test")
public String test() {
simpMessagingTemplate.setSendTimeout(1);
String result;
try {
simpMessagingTemplate.convertAndSend("/topic/test", "haha");
result = "success!";
} catch (Exception e) {
result = "failed! \n" + e.toString();
}
return result;
}
js code js代码
<script src="/lib/websocket/sockjs.min.js"></script>
<script src="/lib/websocket/stomp.min.js"></script>
<script>
var socket = new SockJS("/kefu");
var client = Stomp.over(socket);
client.debug = function (str) {
};
client.connect(
{},
function () {
client.subscribe('/topic/test', function(msg) {
console.log(msg);
});
},
function (error) {
console.log(error);
}
);
</script>
WebSockets is an abstract implementation of TCP over HTTP. WebSockets是TCP over HTTP的抽象实现。 TCP is reliable and guarantees delivery of packets to the recipient. TCP是可靠的,并保证将数据包传递给接收者。 Thus you don't need to worry about delivery from your application Controller to Front end Javascript. 因此,您无需担心从应用程序控制器到前端Java脚本的交付。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.