`
sheungxin
  • 浏览: 107010 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

RabbitMQ使用场景练习:STOMP plugin

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

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

  • 简单总结
/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。
分享到:
评论

相关推荐

    docker-rabbitmq-stomp:使用Stomp插件为RabbitMQ创建Docker镜像的Dockerfile

    使用STOMP插件运行RabbitMQ的基本映像 感谢tumtumcloud! 这些内容基于出色的https://github.com/tutumcloud/rabbitmq图像。 建立它 docker build -t yourid/rabbitmq:latest . 运行 docker run --name=...

    rabbitmq-web-stomp-examples-master.zip_The Bind_rabbitmq stomp_r

    This project contains few basic examples of [RabbitMQ Web STOMP plugin] usage. Once installed the server will bind to port 15670 and serve few static HTML files on port 15670 (e.g. ...

    RabbitMq 集群搭建linux

    - **61613, 61614 (STOMP)**:STOMP协议端口。 - **1883, 8883 (MQTT)**:MQTT协议端口。 对于防火墙的设置,可以通过以下命令开启所需的端口(以node139为例): ```bash # 开启端口 firewall-cmd --permanent --...

    rabbitmq_server-3.8.14.zip

    - `LICENSE-APL2-Stomp-Websocket`可能涉及Stomp协议和WebSocket的支持,这允许客户端通过WebSocket连接到RabbitMQ服务器。 5. **安全与监控**: - RabbitMQ支持TLS/SSL加密,确保通信安全。 - 通过监控工具和...

    java中间件之rabbitmq

    - **多协议支持(Multi-Protocol)**:除了AMQP之外,RabbitMQ还支持STOMP、MQTT等多种消息队列协议。 - **多语言客户端支持(Many Clients)**:几乎所有的主流编程语言都有相应的客户端库,方便开发者集成使用。 -...

    rabbit-plugs.zip

    2. **rabbitmq_web_stomp** 和 **rabbitmq_stomp** - 支持STOMP(Simple Text Oriented Messaging Protocol),使得非Erlang客户端可以方便地连接到RabbitMQ。 3. **rabbitmq_mqtt** - 添加了对MQTT(Message ...

Global site tag (gtag.js) - Google Analytics