rabbitmq message sending and receiving & stomp channel test

Newcomers are welcome to communicate and point out their mistakes~~

Problems found:

After installing Erlang and rabbitmq clients, enable the plug-in rabbitmq plugins enable rabbitmq_ web_ Unable to access after Stomp http://localhost:15674/stomp/info , baidu still couldn't solve this access problem after a long time, and once thought that its stomp channel had not been established successfully

terms of settlement:

Later, after careful study, through stomp HTML to access the messages in the queue, test the communication function, and attach the code

<!DOCTYPE HTML>
<html>
<head>
    <title>My WebSocket</title>
</head>
 
<body>
Welcome<br/>
</body>
<script src="https://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
<script src="https://cdn.bootcss.com/sockjs-client/1.1.4/sockjs.js"></script>
<script type="text/javascript">
 
if (typeof WebSocket == 'undefined') {
    console.log('I won't support it websocket')
}
 
// Initialize ws object
 
//var ws = new WebSocket('ws: / / native IP: 15674 / WS')# IP hiding method
var ws = new WebSocket('ws://127.0.0.1:15674/ws');
// Get Stomp client object
var client = Stomp.over(ws);
 
// SockJS does not support heart-beat: disable heart-beats
//client.heartbeat.outgoing = 0;
//client.heartbeat.incoming = 0;
//client.debug = pipe('#second');
 
// Define connection success callback function
var on_connect = function(x) {
    //data.body is the received data
    client.subscribe("/queue/queue1", function(data) {
        var msg = data.body;
        alert("Data received:" + msg);
    });
};
 
// Define callback function on error
var on_error =  function() {
    console.log('error');
};
 
// Connect RabbitMQ
client.connect('guest', 'guest', on_connect, on_error, '/');
console.log(">>>Connect http://localhost:15674");
</script>
</html>

Notes:

1. The following part is to test whether the browser supports websocket, which should be supported by general browsers.

WebSocket is a new communication protocol added to html5. At present, popular browsers support this protocol, such as Chrome,Safari,Firefox,Opera,IE, etc. the earliest support for this protocol should be chrome

if (typeof WebSocket == 'undefined') {
    console.log('I won't support it websocket')
}

2. As follows, the first line of code accesses the rabbitmq corresponding to the local ip and the second line of code corresponds to the rabbitmq of localhost. You can experience the difference between the two by accessing the rabbitmq management page

adopt http://127.0.0.1:15672/ To access the rabbitmq management page is different from accessing the rabbitmq management page through http: / / native ip:15672 /. Different ip addresses, accounts, passwords, message queues, etc. may be different

//var ws = new WebSocket('ws: / / native ip:15674/ws');
var ws = new WebSocket('ws://127.0.0.1:15674/ws');

3. The / queue/queue1 here refers to the queue / queue name, that is, the queue named queue1. Pay attention to visit the management page of rabbitmq to see if there is this queue.

 client.subscribe("/queue/queue1", function(data)

4. As we all know, 'guest' and 'guest' refer to the user name and password respectively

client.connect('guest', 'guest', on_connect, on_error, '/');

Test part

At first, open stomp directly in the browser HTML, you may not receive the message from the queue on this page, of course, because you have not added the message to the queue, as follows:

1. Open http://localhost:15672/#/queues ,

As can be seen from the figure, the message in queue queue1 is empty and needs to be added

  1. Add a message to queue 1. Next, I only add the message header and message content. The message selection is 1 Non persistent type

    Click send~

  2. At stomp HTML page, check for messages

    As shown in the figure, a message is received, indicating that the stomp channel connection is successful!

  • Next, through the actual operation of maven project, see how the message producers and consumers connect directly. This is another blog post.

Tags: RabbitMQ queue

Posted by Duell on Thu, 19 May 2022 11:09:09 +0300