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

Posted

技术标签:

【中文标题】如何使用 stomp.js 在 javascript 中使用 Web 套接字消息?【英文标题】:How to consume web socket messages in javascript using stomp.js? 【发布时间】:2016-06-30 13:12:04 【问题描述】:

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

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

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

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

message-id:ID:sdnet-mb1-45259-1466517935373-3947:1:1:1:751006
destination:/topic/output.4c5d7481-fa5e-4f2f-d26d-d4e8095b9dd2_s_01
timestamp:1467279400475
expires:0
subscription:sub-0
persistent:true
priority:4

"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"]

每 10 秒。

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

任何建议/示例将不胜感激。

切萨雷

【问题讨论】:

【参考方案1】:

解决方法很简单.....

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"]

【讨论】:

以上是关于如何使用 stomp.js 在 javascript 中使用 Web 套接字消息?的主要内容,如果未能解决你的问题,请参考以下文章

Angular2 与 Stomp.js

STOMP 上的重复事件 Socket.io 和 Node.js

带有 websocket 和 stockjs 的 JBOSS eap 6.3 beta - 使用 spring 框架的 stomp.js

stomp.js websocket广播模式

vue项目集成stomp.js接收artemis消息推送

vue项目集成stomp.js接收artemis消息推送