stomp.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // import Vue from 'vue'
  2. import _ from 'lodash'
  3. import Stomp from 'stompjs'
  4. const Plugin = {
  5. install (vue, options) {
  6. // 4. 添加实例方法
  7. vue.prototype.$stomp = function (subscribes = {}) {
  8. let msg
  9. // 获得Stomp client对象
  10. const ws = new WebSocket(options.host)
  11. const client = Stomp.over(ws)
  12. // 成功回调
  13. const connectCallback = function (x) {
  14. // 返回结果
  15. // '/exchange/qrcode.topic/qrcode'
  16. Object.keys(subscribes)
  17. .filter(p => _.isFunction(subscribes[p]))
  18. .forEach(key => {
  19. client.subscribe(key, subscribes[key])
  20. })
  21. }
  22. // 失败回调
  23. const errorCallback = function (error) {
  24. msg = error
  25. }
  26. client.heartbeat.outgoing = options.heartbeatOutgoing
  27. client.heartbeat.incoming = options.heartbeatIncoming
  28. // client.reconnect.delay = options.reconnectDelay
  29. // 添加参数 回调函数
  30. client.connect(options.connectHeaders, connectCallback, errorCallback)
  31. if (options.debug) {
  32. client.debug = function (str) {
  33. console.log(str, 'debug')
  34. }
  35. }
  36. return msg
  37. }
  38. }
  39. }
  40. export default Plugin