[关闭]
@dooy 2022-08-31T01:38:03.000000Z 字数 5369 阅读 84

Stomp RabbitMQ websocket 样例

JS


一、配置

配置RabbitMQ 参考 需要启用stomp 插件

https://www.rabbitmq.com/web-stomp.html

然后在 exchanges
建立一个 名为pen_test 类型为direct 的exchange

二、html Demo

  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <!-- 必须的 meta 标签 -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  9. <script src="https://cdn.jsdelivr.net/npm/@stomp/stompjs@5.0.0/bundles/stomp.umd.min.js"></script>
  10. <title>Stomp MQ DEMO</title>
  11. <style>
  12. .bd-placeholder-img {
  13. font-size: 1.125rem;
  14. text-anchor: middle;
  15. -webkit-user-select: none;
  16. -moz-user-select: none;
  17. user-select: none;
  18. }
  19. @media (min-width: 768px) {
  20. .bd-placeholder-img-lg {
  21. font-size: 3.5rem;
  22. }
  23. }
  24. .b-example-divider {
  25. height: 3rem;
  26. background-color: rgba(0, 0, 0, .1);
  27. border: solid rgba(0, 0, 0, .15);
  28. border-width: 1px 0;
  29. box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15);
  30. }
  31. .form-control-dark {
  32. color: #fff;
  33. background-color: var(--bs-dark);
  34. border-color: var(--bs-gray);
  35. }
  36. .form-control-dark:focus {
  37. color: #fff;
  38. background-color: var(--bs-dark);
  39. border-color: #fff;
  40. box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .25);
  41. }
  42. .bi {
  43. vertical-align: -.125em;
  44. fill: currentColor;
  45. }
  46. .text-small {
  47. font-size: 85%;
  48. }
  49. .dropdown-toggle {
  50. outline: 0;
  51. }
  52. .progress{margin-top:5px;margin-bottom:5px;}
  53. #cards { display:flex;flex:column}
  54. #cards .card{ max-width:300px; margin:5px 5px 5px 0; }
  55. </style>
  56. </head>
  57. <body>
  58. <main>
  59. <div class="container">
  60. <header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
  61. <a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none">
  62. <svg class="bi me-2" width="40" height="32"><use xlink:href="#bootstrap"/></svg>
  63. <span class="fs-4">Stomp MQ examples</span>
  64. </a>
  65. <ul class="nav nav-pills">
  66. <li class="nav-item"><a href="#" class="nav-link active" aria-current="page">Home</a></li>
  67. <li class="nav-item"><a href="#" class="nav-link">Features</a></li>
  68. <li class="nav-item"><a href="#" class="nav-link">Pricing</a></li>
  69. <li class="nav-item"><a href="#" class="nav-link">FAQs</a></li>
  70. <li class="nav-item"><a href="#" class="nav-link">About</a></li>
  71. </ul>
  72. </header>
  73. </div>
  74. <div class="container">
  75. <div class="input-group mb-3">
  76. <input type="text" v-model="smsg" class="form-control" placeholder="请输入发送内容" aria-label="请输入发送内容" aria-describedby="button-addon2">
  77. <button class="btn btn-success" type="button" id="button-addon2" v-on:click="send()" v-bind:disabled="!mqConfig.isConnect">发送</button>
  78. </div>
  79. </div>
  80. <div class="container" >
  81. <div class="card" style="margin:10px 0;">
  82. <div class="card-header">
  83. 队列名称 {{mqConfig.exchange}} <span v-if="log.length>0">收到<b v-text="log.length"></b>条信息</span>
  84. </div>
  85. <ul class="list-group list-group-flush" v-if="log.length>0">
  86. <li class="list-group-item" v-for="v in log"><b v-text="v.t"></b> <span v-text="v.s"></span></li>
  87. </ul>
  88. <div class="card-body" v-else>
  89. <h5 class="card-title">看状态</h5>
  90. <p class="card-text" v-html="message">...</p>
  91. </div>
  92. </div>
  93. </div>
  94. <div class="b-example-divider"></div>
  95. </main>
  96. <script>
  97. var app = new Vue({
  98. el: 'main',
  99. data: {
  100. message: '等待链接!',
  101. smsg:'',
  102. log:[],
  103. mqConfig:{
  104. exchange:'/exchange/pen_test/test' //exchange为pen_test 肉Routing Key 为test
  105. ,isConnect:false
  106. },
  107. stompClient:null
  108. },
  109. created: function () {
  110. this.hello();
  111. },
  112. mounted:function () {
  113. this.start();
  114. },
  115. methods:{
  116. start:function (){
  117. console.log('开始>>');
  118. const stompConfig = {
  119. // Typically login, passcode and vhost
  120. // Adjust these for your broker
  121. connectHeaders: {
  122. login: "MQ的user",
  123. passcode: "*****",
  124. key:'mytest'
  125. },
  126. // Broker URL, should start with ws:// or wss:// - adjust for your broker setup
  127. brokerURL: "ws://ap.penly.cn:15674/ws",
  128. // Keep it off for production, it can be quit verbose
  129. // Skip this key to disable
  130. debug: (str)=> {
  131. console.log('['+this.getFormatDate() +']STOMP: ' + str);
  132. },
  133. // If disconnected, it will retry after 200ms
  134. reconnectDelay: 200,
  135. // Subscriptions should be done inside onConnect as those need to reinstated when the broker reconnects
  136. onConnect: (frame)=> {
  137. console.log('onConnect');
  138. this.message='链接成功正在等待收集数据!' ;
  139. // The return object has a method called `unsubscribe`
  140. //const subscription = stompClient.subscribe('/exchange/pen/stroke', function (message) {
  141. const subscription = this.stompClient.subscribe( this.mqConfig.exchange, this.displayIncomingMessageNew );
  142. this.mqConfig.isConnect=true;
  143. },
  144. onStompError :(f)=>{
  145. console.log('error',f);
  146. this.message='错误>>'+f.body;
  147. },
  148. onError:(f)=>{
  149. console.log('error',f);
  150. }
  151. };
  152. // Create an instance
  153. this.stompClient= new StompJs.Client(stompConfig);
  154. // You can set additional configuration here
  155. // Attempt to connect
  156. this.stompClient.activate();
  157. },
  158. displayIncomingMessageNew:function (message){
  159. console.log('收到数据啊>>' , message);
  160. //this.log.push({t:this.getFormatDate(),s:message.body });
  161. //this.log.splice( {t:this.getFormatDate(),s:message.body });
  162. this.log.splice(0,0,{t:this.getFormatDate(),s:message.body });
  163. console.log('log length>>',this.log.length );
  164. },
  165. hello: function () {
  166. console.log('hello from mixin!')
  167. },
  168. send:function ( ){
  169. if( ! this.mqConfig.isConnect ) return;
  170. if( !this.smsg) return ;
  171. this.stompClient.publish({
  172. destination: this.mqConfig.exchange,
  173. body: this.smsg
  174. });
  175. this.smsg='';
  176. },
  177. getFormatDate:function() {
  178. var date = new Date();
  179. var month = date.getMonth() + 1;
  180. var strDate = date.getDate();
  181. var hours = date.getHours();
  182. var minutes = date.getMinutes();
  183. var seconds = date.getSeconds();
  184. if (month >= 1 && month <= 9) {
  185. month = "0" + month;
  186. }
  187. if (strDate >= 0 && strDate <= 9) {
  188. strDate = "0" + strDate;
  189. }
  190. if (hours >= 0 && hours <= 9) {
  191. hours = "0" + hours;
  192. }
  193. if (minutes >= 0 && minutes <= 9) {
  194. minutes = "0" + minutes;
  195. }
  196. if (seconds >= 0 && seconds <= 9) {
  197. seconds = "0" + seconds;
  198. }
  199. return date.getFullYear() + "-" + month + "-" + strDate
  200. + " " + date.getHours() + ":" + minutes + ":" + seconds;
  201. }
  202. }
  203. });
  204. </script>
  205. </body>
  206. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注