RabbitMQ使用场景练习:STOMP plugin
程序员文章站
2022-07-13 15:02:45
...
- STOMP plugin
rabbitmq-plugins enable rabbitmq_web_stomp rabbitmq-plugins enable rabbitmq_web_stomp_examples通过链接:http://127.0.0.1:15670/web-stomp-examples可以访问演示样例,实际上没法用,因为引用的jquery.min.js是google的资源。
var mqStompUrl="http://192.168.174.131:15674/stomp"; var ws = new SockJS(mqStompUrl);//使用socket //var ws = new WebSocket("ws://192.168.174.131:15674/ws");//使用websocket var client = Stomp.over(ws); // SockJS does not support heart-beat: disable heart-beats client.heartbeat.incoming = 0; client.heartbeat.outgoing = 0; client.debug = function(e) { console.log(e); }; // default receive callback to get message from temporary queues client.onreceive = function(m) { console.log(m) } var on_connect = function(x) { id = client.subscribe("/queue/hehe",function(m) { //... }}); }; var on_error = function() { console.log('error'); }; client.connect('sheungxin', '123456', on_connect, on_error, '/'); 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
- 简单总结
/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的支持
- 中文消息无法发送问题