#全部代码
import Stomp from "stompjs";
import { storeToRefs } from "pinia"; //引入pinia转换
import MqStore from '@/store/mq/index';
import {
Cartesian3
} from 'cesium'
import CesiumStore from "@/store/cesium/index"; //引入仓库
import type { IDeduceDataRow } from '@/store/cesium/type'
import emitter from '@/utils/mitt';
const mqStore = MqStore();
const cesiumStore = CesiumStore();
const { client } = storeToRefs(mqStore);
import {
service,
username,
password,
host,
incoming,
outgoing,
} from './config'
export const connect = () => {
// 连接时判断是否重复
if (client.value) {
client.value.disconnect(() => { })
client.value = null
}
//这里填你rabbitMQ的连接ip地址直接替换localhost:15674就好其它的不用改
client.value = Stomp.over(new WebSocket(service))
client.value.debug = null;
client.value.heartbeat.incoming = incoming;
client.value.heartbeat.outgoing = outgoing;
//创建连接,放入连接成功和失败回调函数
client.value.connect(username, password, onConnected, onFailed, host);
}
//webSocket连接成功后回调函数
const onConnected = (frame) => {
console.log("Connected: " + frame);
onSubscribe()
}
const onFailed = (frame) => {
console.log("Failed: " + frame);
}
const onSubscribe = () => {
//exchange交换机模式 hszz交换机名称 testQueueB_1_1 routingKey
// const exchange = "/exchange/hszz/testQueueB_1_1";
const exchange = "/exchange/hszz-1/testQueueB_1_1";
// x-queue-name 指定队列名称
const headers = { 'x-queue-name': 'testQueueB_1_1' };
client.value.subscribe(exchange, (frame) => {
if (frame.body) {
interface IMsg {
content: IDeduceDataRow[]
programmeId: number
type: string
}
const msg: IMsg = JSON.parse(frame.body)
emitter.emit('msgContentMitt', msg.content)
msg.content.forEach(item => {
cesiumStore.addDeduceData(item, item.unitId)
const time = new Date(item.time)
const posi = Cartesian3.fromDegrees(
item.unitLongitude,
item.unitLatitude,
0
)
cesiumStore.addDeduceSample(posi, item.unitId, time)
})
}
}, headers);
}