记一次使用vue连接rabbitMq

时间:2024-07-16 07:03:55
import Stomp from 'stompjs' onConnected(frame) { // 绑定交换机exchange_pushmsg是交换机的名字rk_pushmsg是绑定的路由key var exchange = this.rabbitMqexchange || 'queue.device.zzzz' // 创建随机队列用上面的路由key绑定交换机,放入收到消息后的回调函数和失败的回调函数 this.client.subscribe(exchange, this.responseCallback, this.onFailed) }, onFailed(frame) { console.log(frame) }, responseCallback(frame) { if (frame.body != '' && frame.body != null) { const formatter = (data, params) => { this.dataOldChart = data; if (typeof this.dataFormatter === 'function') { try { data = this.dataFormatter(data, params, this.getItemRefs()); } catch (err) { console.log(new Error(err)) data = err + '' } } this.handleCommonBind(data, -1, 'dataAfterFormatter') return data } console.log('-----[' + frame.body); let result = typeof frame.body == 'string' ? JSON.parse(frame.body) : frame.body this.dataChart = formatter(result, this.dataParams) setTimeout(() => { this.updateChart() }, 100) } }, connect() { this.client = Stomp.client(this.rabbitMqUrl || 'ws://172.16.0.97:15670') // 填写你rabbitMQ登录的用户名和密码 var headers = { 'login': this.rabbitMqUser || '', 'passcode': this.rabbitMqPass || '', // 虚拟主机,默认“/” 'host': this.rabbitMqHost || '/' } let safe = this // 创建连接,放入连接成功和失败回调函数 this.client.connect(headers, this.onConnected, this.onFailed) this.client.debug = (message) => { console.log('对接:' + message) this.mqDockingMessage = message } },