@dooy
2022-08-31T01:38:03.000000Z
字数 5369
阅读 84
JS
配置RabbitMQ 参考 需要启用stomp 插件
然后在 exchanges
建立一个 名为pen_test 类型为direct 的exchange
<!doctype html>
<html lang="zh-CN">
<head>
<!-- 必须的 meta 标签 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@stomp/stompjs@5.0.0/bundles/stomp.umd.min.js"></script>
<title>Stomp MQ DEMO</title>
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
.b-example-divider {
height: 3rem;
background-color: rgba(0, 0, 0, .1);
border: solid rgba(0, 0, 0, .15);
border-width: 1px 0;
box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15);
}
.form-control-dark {
color: #fff;
background-color: var(--bs-dark);
border-color: var(--bs-gray);
}
.form-control-dark:focus {
color: #fff;
background-color: var(--bs-dark);
border-color: #fff;
box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .25);
}
.bi {
vertical-align: -.125em;
fill: currentColor;
}
.text-small {
font-size: 85%;
}
.dropdown-toggle {
outline: 0;
}
.progress{margin-top:5px;margin-bottom:5px;}
#cards { display:flex;flex:column}
#cards .card{ max-width:300px; margin:5px 5px 5px 0; }
</style>
</head>
<body>
<main>
<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none">
<svg class="bi me-2" width="40" height="32"><use xlink:href="#bootstrap"/></svg>
<span class="fs-4">Stomp MQ examples</span>
</a>
<ul class="nav nav-pills">
<li class="nav-item"><a href="#" class="nav-link active" aria-current="page">Home</a></li>
<li class="nav-item"><a href="#" class="nav-link">Features</a></li>
<li class="nav-item"><a href="#" class="nav-link">Pricing</a></li>
<li class="nav-item"><a href="#" class="nav-link">FAQs</a></li>
<li class="nav-item"><a href="#" class="nav-link">About</a></li>
</ul>
</header>
</div>
<div class="container">
<div class="input-group mb-3">
<input type="text" v-model="smsg" class="form-control" placeholder="请输入发送内容" aria-label="请输入发送内容" aria-describedby="button-addon2">
<button class="btn btn-success" type="button" id="button-addon2" v-on:click="send()" v-bind:disabled="!mqConfig.isConnect">发送</button>
</div>
</div>
<div class="container" >
<div class="card" style="margin:10px 0;">
<div class="card-header">
队列名称 {{mqConfig.exchange}} <span v-if="log.length>0">收到<b v-text="log.length"></b>条信息</span>
</div>
<ul class="list-group list-group-flush" v-if="log.length>0">
<li class="list-group-item" v-for="v in log"><b v-text="v.t"></b> <span v-text="v.s"></span></li>
</ul>
<div class="card-body" v-else>
<h5 class="card-title">看状态</h5>
<p class="card-text" v-html="message">...</p>
</div>
</div>
</div>
<div class="b-example-divider"></div>
</main>
<script>
var app = new Vue({
el: 'main',
data: {
message: '等待链接!',
smsg:'',
log:[],
mqConfig:{
exchange:'/exchange/pen_test/test' //exchange为pen_test 肉Routing Key 为test
,isConnect:false
},
stompClient:null
},
created: function () {
this.hello();
},
mounted:function () {
this.start();
},
methods:{
start:function (){
console.log('开始>>');
const stompConfig = {
// Typically login, passcode and vhost
// Adjust these for your broker
connectHeaders: {
login: "MQ的user",
passcode: "*****",
key:'mytest'
},
// Broker URL, should start with ws:// or wss:// - adjust for your broker setup
brokerURL: "ws://ap.penly.cn:15674/ws",
// Keep it off for production, it can be quit verbose
// Skip this key to disable
debug: (str)=> {
console.log('['+this.getFormatDate() +']STOMP: ' + str);
},
// If disconnected, it will retry after 200ms
reconnectDelay: 200,
// Subscriptions should be done inside onConnect as those need to reinstated when the broker reconnects
onConnect: (frame)=> {
console.log('onConnect');
this.message='链接成功正在等待收集数据!' ;
// The return object has a method called `unsubscribe`
//const subscription = stompClient.subscribe('/exchange/pen/stroke', function (message) {
const subscription = this.stompClient.subscribe( this.mqConfig.exchange, this.displayIncomingMessageNew );
this.mqConfig.isConnect=true;
},
onStompError :(f)=>{
console.log('error',f);
this.message='错误>>'+f.body;
},
onError:(f)=>{
console.log('error',f);
}
};
// Create an instance
this.stompClient= new StompJs.Client(stompConfig);
// You can set additional configuration here
// Attempt to connect
this.stompClient.activate();
},
displayIncomingMessageNew:function (message){
console.log('收到数据啊>>' , message);
//this.log.push({t:this.getFormatDate(),s:message.body });
//this.log.splice( {t:this.getFormatDate(),s:message.body });
this.log.splice(0,0,{t:this.getFormatDate(),s:message.body });
console.log('log length>>',this.log.length );
},
hello: function () {
console.log('hello from mixin!')
},
send:function ( ){
if( ! this.mqConfig.isConnect ) return;
if( !this.smsg) return ;
this.stompClient.publish({
destination: this.mqConfig.exchange,
body: this.smsg
});
this.smsg='';
},
getFormatDate:function() {
var date = new Date();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
return date.getFullYear() + "-" + month + "-" + strDate
+ " " + date.getHours() + ":" + minutes + ":" + seconds;
}
}
});
</script>
</body>
</html>