我使用spring + stomp向websocket客户端广播消息,但我怎么知道结果是成功的?我使用setSendTimeout方法并捕获Exception来检测convertAndSend是否成功,但它不起作用! 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
<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>
答案 0 :(得分:-1)
WebSockets是TCP over HTTP的抽象实现。 TCP是可靠的,并保证将数据包传递给收件人。因此,您不必担心从应用程序控制器到前端Javascript的交付。