// import Vue from 'vue' import _ from 'lodash' import Stomp from 'stompjs' const Plugin = { install (vue, options) { // 4. 添加实例方法 vue.prototype.$stomp = function (subscribes = {}) { let msg // 获得Stomp client对象 const ws = new WebSocket(options.host) const client = Stomp.over(ws) // 成功回调 const connectCallback = function (x) { // 返回结果 // '/exchange/qrcode.topic/qrcode' Object.keys(subscribes) .filter(p => _.isFunction(subscribes[p])) .forEach(key => { client.subscribe(key, subscribes[key]) }) } // 失败回调 const errorCallback = function (error) { msg = error } client.heartbeat.outgoing = options.heartbeatOutgoing client.heartbeat.incoming = options.heartbeatIncoming // client.reconnect.delay = options.reconnectDelay // 添加参数 回调函数 client.connect(options.connectHeaders, connectCallback, errorCallback) if (options.debug) { client.debug = function (str) { console.log(str, 'debug') } } return msg } } } export default Plugin