<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Echarts demo</title><linkrel="stylesheet"href="../../bootstrap/css/bootstrap.min.css"><!-- 引入 ECharts 文件 --><scripttype="text/javascript"src="../../js/jquery-1.12.4.min.js"></script><scripttype="text/javascript"src="./sockjs.min.js"></script><scripttype="text/javascript"src="./stomp.min.js"></script></head><body><h2style="margin-left: 15px;">测试Websocket自动刷新页面状态</h2><divclass="row"style="margin-left: 0"><divclass="col-md-3"style="font-size: large"><span>状态更新:</span><spanid="load"style="color: #5bc0de">loading...</span></div></div><scripttype="text/javascript">$(function(){console.log('starting...');// Stomp.js boilerplate
varws;if(typeof(WebSocket)==="function"){console.log('ws协议...');ws=newWebSocket('ws://192.168.217.226:15674/ws');}else{console.log('http协议...');ws=newSockJS('http://192.168.217.226:15674/stomp');}// Init Client
varclient=Stomp.over(ws);// SockJS does not support heart-beat: disable heart-beats
// client will send heartbeats every xxxms
client.heartbeat.outgoing=0;// client does not want to receive heartbeats
client.heartbeat.incoming=0;// Declare on_connect
varon_connect=function(x){client.subscribe("/exchange/rabbitmq/zjj",function(d){// update result
$("#load").empty().text(d.body);// disconnect client
// when close the brower, it will be closed automatically too
client.disconnect(function(){console.log("See you next time!");});});};// Declare on_error
varon_error=function(){console.log('error');};// Conect to RabbitMQ
client.connect('guest','guest',on_connect,on_error,'/');});</script></body></html>
刚开始打开的效果:
web页面控制台日志如下,说明已经连上了队列:
同时我去看rabbitmq管理界面,发现生成了一个随机队列:
后台python发送消息代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
importpikaimportsysconnection=pika.BlockingConnection(pika.ConnectionParameters(host='192.168.217.226',port=5673))channel=connection.channel()channel.exchange_declare(exchange='rabbitmq',type='topic')routing_key='zjj'message='[disk.info] Hello World!333'channel.basic_publish(exchange='rabbitmq',routing_key=routing_key,body=message)print(" [x] Sent %r:%r"%(routing_key,message))connection.close()