简体   繁体   中英

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? I use the setSendTimeout method and catch Exception to detect whether the convertAndSend is success, but it doesn't work! java code

@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>

WebSockets is an abstract implementation of TCP over HTTP. TCP is reliable and guarantees delivery of packets to the recipient. Thus you don't need to worry about delivery from your application Controller to Front end Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM