RabbitMQ usage scenario exercise: STOMP plugin

  • STOMP plugin
     Stomp is a simple message text protocol (not highlighted, not carefully studied). The integration of the STOMP plugin in RabbitMQ enables the browser to access the message queue through the WebSocket protocol, with SockJS as the backup (older browsers do not support the WebSocket protocol)

rabbitmq-plugins enable rabbitmq_web_stomp
rabbitmq-plugins enable rabbitmq_web_stomp_examples
The demo example can be accessed through the link: http://127.0.0.1:15670/web-stomp-examples , but it is actually useless because the referenced jquery.min.js is a resource of 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);
The following error is reported when using websocket: failed: Error during WebSocket handshake: Unexpected response code: 404 , the reason

has . Please refer to: http://www.rabbitmq.com/web-stomp.html

  • simple summary
/queue/queuename: Use the default forwarder to subscribe/publish messages, and a persistent queue is automatically created by stomp by default

/amq/queue/queuename: The difference from /queue/queuename is that the queue is not automatically created by stomp, and the queue does not exist. Failure

/ topic/routing_key: subscribe/publish messages through the amq.topic forwarder, create a temporary queue by default when subscribing, and bind the topic with routing_key

/temp-queue/xxx: create a temporary queue (only in the attribute reply in headers -to), which can be used to receive reply messages through a temporary queue after sending a message, and receive through client.onreceive

/exchange/exchangename/[routing_key]: subscribe/publish messages through the forwarder, the forwarder needs to manually create

client.subscribe(destination ,callback,headers): subscribe message

client.send(destination,headers,body): publish message

client.unsubscribe(id): cancel subscription, id is the number returned when subscribed

client.onreceive: default receive callback to get message from temporary queue

  • Queue Properties support
Version support is required, and most of them can be supported after 3.6.0. For specific reference: http://www.rabbitmq.com/stomp.html

  • Chinese message cannot be sent
For Chinese messages, the stomp protocol has a coding problem. If it cannot be sent out, it will report an error and close it. You can perform encodeURI(data) on Chinese messages, and decodeURI(d.body) when receiving messages. Provided in the official website article, the message encoding must be UTF-8.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802822&siteId=291194637