分类目录

链接

2019 年 7 月
1234567
891011121314
15161718192021
22232425262728
293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > MQ > 正文
使用stomp连接RabbitMQ
MQ 暂无评论 阅读(384)

1.安装rabbitmq

2.开启stomp插件

  1. rabbitmq-plugins enable rabbitmq_web_stomp

3.成功后重启RabbitMQ

4.WEB代码

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>MyWebSocket</title>
  5. </head>
  6. <body>
  7. Welcome<br/>
  8. <input id="text" type="text"/>
  9. <div id="message">
  10. </div>
  11. </body>
  12. <script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
  13. <script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.js"></script>
  14. <script type="text/javascript">
  15. if(typeofWebSocket=='undefined'){
  16.         console.log('不支持websocket')
  17. }
  18. var socketServerUrl = 'http://mq.peos.cn/stomp';
    var ws = new SockJS(socketServerUrl);
    //var ws = new WebSocket(socketServerUrl);
    var client = Stomp.over(ws);

    //SockJS防断开
    client.heartbeat.outgoing = 0; // 客户端不发送
    client.heartbeat.incoming = 0; // 客户端每10s接收一次数据从服务器

  19.     var on_connect = function (){
  20.         console.log('connected to '+ socketServerUrl);
  21. //data.body是接收到的数据
  22.         client.subscribe("/topic/room_1", function (data){
  23.             var msg = data.body;
  24.             $("#msglist").append("<li>收到数据:"+ msg+"</li>");
  25. });
  26. };
  27.     var on_error = function (){
  28.         console.log('connected error');
  29. };
  30. //参数依次为:用户名,密码,连接后,出错,虚拟主机名
  31.     client.connect('用户名','密码', on_connect, on_error,'/');
  32.     function send(){
  33.         var msg = $('#msg').val();
  34. //参数依次为:目的地,消息头,消息体
  35.         client.send('/topic/room_1',{},
  36.             JSON.stringify({
  37. 'room':1,
  38. 'type':"1",//1,2
  39. 'content': msg,
  40. 'userId':"566072523",//小明
  41. 'questionId':"222299023",//题目1
  42. 'createTime':"",
  43. })
  44. );
  45. }
  46. </script>
  47. <div style="margin-top:100px;">
  48. <input type="text" id="msg" name="msg" value=""/>
  49. <input type="button" id="send" name="send" value="send" onclick="send()"/>
  50. </div>
  51. <div>
  52. <ul  id="msglist">
  53. </ul>
  54. </div>
  55. </html>

 

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:使用stomp连接RabbitMQ | Bruce's Blog

发表评论

留言无头像?