简体   繁体   English

如何使用spring + stomp + sockjs获得websocket响应

[英]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.

相关问题 带有STOMP,SOCKJS和ACTIVEMQ的Spring WebSocket - Spring websocket with STOMP, SOCKJS and ACTIVEMQ WebSocket与Sockjs和Spring 4但没有Stomp - WebSocket with Sockjs & Spring 4 but without Stomp Spring Websocket(stomp,sockjs):控制器未获取全部数据 - Spring websocket(stomp, sockjs): Controller didn't get whole data 带有Spring的基本websocket没有STOMP和SockJS - Basic websocket with Spring without STOMP and SockJS 带有Spring MVC,Stomp,Sockjs,Angular JS的Websocket - Websocket with Spring mvc, stomp, sockjs, angular JS 弹簧websocket与sockjs和stomp客户端设计 - Spring websocket with sockjs and stomp client side design 如何将Spring Websocket与Spring Session自定义HttpSession一起使用,并且没有Stomp / SockJS? - How to use Spring Websocket with the Spring Session custom HttpSession and without Stomp/SockJS? 如何使用 SockJs 通过 STOMP Java 客户端对 Spring 非 Web 套接字进行身份验证? - How authenticate a Spring non-web Websocket over STOMP Java client using SockJs? 使用sock.js在套接字上踩脚无法与Spring 4 WebSocket连接 - Stomp over socket using sockjs can't connect with Spring 4 WebSocket 通过spring websocket,sockJs和STOMP向经过身份验证的用户发送通知 - Send notification to authenticated user over spring websocket, sockJs and STOMP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM