【参考】RabbitMQ使用场景练习:STOMP plugin

  • STOMP plugin

     Stomp是一个简单的消息文本协议(不重点介绍,没仔细研究过)。RabbitMQ中STOMP plugin的集成,实现了由浏览器通过WebSocket协议访问消息队列,SockJS作为后备(旧版的浏览器不支持WebSocket协议) 

 

启用stomp plugin命令代码  收藏代码

  1. rabbitmq-plugins enable rabbitmq_web_stomp  
  2. rabbitmq-plugins enable rabbitmq_web_stomp_examples  

通过链接:http://127.0.0.1:15670/web-stomp-examples可以访问演示样例,实际上没法用,因为引用的jquery.min.js是google的资源。 

 

关键代码  收藏代码

  1. var mqStompUrl="http://192.168.174.131:15674/stomp";  
  2. var ws = new SockJS(mqStompUrl);//使用socket  
  3. //var ws = new WebSocket("ws://192.168.174.131:15674/ws");//使用websocket  
  4. var client = Stomp.over(ws);  
  5. // SockJS does not support heart-beat: disable heart-beats  
  6. client.heartbeat.incoming = 0;  
  7. client.heartbeat.outgoing = 0;  
  8.   
  9. client.debug = function(e) {  
  10.    console.log(e);  
  11. };  
  12.   
  13. // default receive callback to get message from temporary queues  
  14. client.onreceive = function(m) {  
  15.    console.log(m)  
  16. }  
  17.   
  18. var on_connect = function(x) {  
  19.    id = client.subscribe("/queue/hehe",function(m) {  
  20.       //...  
  21.    }});  
  22. };  
  23. var on_error =  function() {  
  24.    console.log('error');  
  25. };  
  26. client.connect('sheungxin''123456', on_connect, on_error, '/');  
  27.   
  28. client.send("/queue/hehe",{"content-type":"text/plain"}, text);  

使用websocket时报以下错误:failed: Error during WebSocket handshake: Unexpected response code: 404,暂时未找到原因 

可参考:http://www.rabbitmq.com/web-stomp.html

 

 

  • 简单总结

/queue/queuename:使用默认转发器订阅/发布消息,默认由stomp自动创建一个持久化队列 

/amq/queue/queuename:与/queue/queuename的区别在于队列不由stomp自动进行创建,队列不存在失败 

/topic/routing_key:通过amq.topic转发器订阅/发布消息,订阅时默认创建一个临时队列,通过routing_key与topic进行绑定 

/temp-queue/xxx:创建一个临时队列(只能在headers中的属性reply-to中使用),可用于发送消息后通过临时队列接收回复消息,接收通过client.onreceive 

/exchange/exchangename/[routing_key]:通过转发器订阅/发布消息,转发器需要手动创建 

client.subscribe(destination,callback,headers) :订阅消息 

client.send(destination,headers,body):发布消息 

client.unsubscribe(id):取消订阅,id为订阅时返回的编号 

client.onreceive:默认接收回调从临时队列获取消息 

 

 

 

 

  • Queue Properties的支持

需要版本的支持,3.6.0以后大多都可以支持,具体具体参考:http://www.rabbitmq.com/stomp.html 

 

 

 

 

  • 中文消息无法发送问题

中文消息,stomp协议存在编码的问题,发送不出去,会报错关闭掉。可以对中文消息进行encodeURI(data),接收消息时decodeURI(d.body)。官网文章中提供,消息编码必须为UTF-8。

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值