如何使用stomp.js在javascript中使用Web套接字消息?

时间:2016-06-30 13:12:04

标签: javascript websocket stomp

我需要使用stomp.js库在Javascript中使用Web套接字消息。

我有这个代码结构......

-keep

....我可以在我的网络浏览器控制台中看到返回的消息,所以像这样......

<!DOCTYPE HTML>
<html>
 <head>
   <title>Client Stomp</title>
   <script src="js/stomp.js"></script>
 </head>
 <body>
   <script>
       var urlServer;
       var topics;
       urlServer = "ws://myserver.com/ws"; 
       topics = "/topic/output.values";
       client = Stomp.client(urlServer);
       client.connect("usr" , "pwd" , connectCallBack, errorCallback);

       function connectCallBack(x) {
           client.subscribe(topics, messageCallback);
       }

       function errorCallback(x) {
         // manage connection errors 

       }

      function messageCallback(x) {
           // manage message returned 
      }
     </script>
 </body>
</html>
每10秒钟一次。

我如何在Javacript代码中使用这些消息?例如,我如何回显b_6300_Hz值?

任何建议/示例都将受到赞赏。

切萨雷

1 个答案:

答案 0 :(得分:0)

解决方案非常简单.....

x.body

...是邮件正文,所以

{"sensor":"4c5d7481-fa5e-4f2f-d26d-d4e8095b9dd2","stream":"s_01","values":[{"time":"2016-06-30T11:36:39+0200","components":{"b_6300_Hz":"56.6","b_50_Hz":"16.0","b_3150_Hz":"65.7","b_31_5_Hz":"12.9","b_500_Hz":"60.1","b_400_Hz":"54.5","b_160_Hz":"52.4","b_10000_Hz":"-11.8","b_8000_Hz":"45.6","b_80_Hz":"29.1","b_20000_Hz":"-9.5","b_5000_Hz":"59.4","b_1600_Hz":"67.4","b_1000_Hz":"61.6","b_20_Hz":"17.9","b_125_Hz":"40.5","b_40_Hz":"16.0","b_4000_Hz":"62.7","b_12500_Hz":"-10.6","b_25_Hz":"12.9","b_250_Hz":"55.5","LAeq":"75.2","b_630_Hz":"57.9","b_315_Hz":"55.7","b_200_Hz":"47.6","b_2000_Hz":"67.4","b_63_Hz":"29.1","b_100_Hz":"34.8","b_16_Hz":"17.9","b_1250_Hz":"60.6","b_800_Hz":"66.5","b_16000_Hz":"-9.3","b_2500_Hz":"62.6"}}]}