chat.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // import { url } from '@src/utils/axios'
  2. const DEFAULT_ADDRESS = process.env.NODE_ENV === 'production' ? 'ws://mall.huaml.com:8282' : 'ws://mall.huaml.com:8282'
  3. const TYPE = {
  4. CART: 1
  5. }
  6. Object.freeze(TYPE)
  7. const init = (address = DEFAULT_ADDRESS) => {
  8. let ws = new WebSocket(address)
  9. // 当socket连接打开时
  10. ws.onopen = function (e) {
  11. console.log('websocket握手成功')
  12. // onMessage(e)
  13. }
  14. // 当有消息时根据消息类型显示不同信息
  15. ws.onmessage = function (e) {
  16. let data
  17. try {
  18. data = JSON.parse(e.data)
  19. } catch (e) {
  20. data = e.data
  21. }
  22. let collection = Watcher.collection || {}
  23. Object.keys(collection).forEach(watcherId => {
  24. let watcher = collection[watcherId]
  25. if (watcher == null) return
  26. watcher.receive(data)
  27. })
  28. }
  29. ws.onclose = function () {
  30. console.log('连接关闭,定时重连')
  31. // init(address)
  32. }
  33. ws.onerror = function () {
  34. console.log('出现错误')
  35. }
  36. return ws
  37. }
  38. let watcherId = 1000
  39. const isValidType = type => {
  40. // return type == null || type == '' || _.isString(type)
  41. if (type == null || type == '') return true
  42. let i = -1
  43. Object.keys(TYPE).forEach((key, index) => {
  44. if (TYPE[key] === type) return (i = index)
  45. })
  46. return i === -1 ? false : true
  47. }
  48. class Watcher {
  49. constructor(types, cb, filter) {
  50. this.cb = cb
  51. this.filter = filter
  52. this._id = watcherId++
  53. this.types = []
  54. this._register()
  55. }
  56. _register() {
  57. Watcher.collection[this._id] = this
  58. }
  59. // _destroy() {
  60. // let collection = Watcher.collection
  61. // collection[this._id] = null
  62. // }
  63. _isNeedReceiveMessage(type, data, source, target) {
  64. if (this.types.length == 0) return true
  65. let index = this.types.findIndex(_type => {
  66. return _type == type
  67. })
  68. if (index == -1) return false
  69. if (!_.isFunction(this.filter)) {
  70. return true
  71. }
  72. try {
  73. return this.filter.call(null, type, data, source, target)
  74. } catch (e) {
  75. throw new Error(e)
  76. }
  77. }
  78. receive({ type, data, source, target } = {}) {
  79. let _need = this._isNeedReceiveMessage(type, data, source, target)
  80. if (!_need) {
  81. return
  82. }
  83. this.cb.call(null, type, data, source, target)
  84. }
  85. }
  86. //收集器
  87. Watcher.collection = {}
  88. const handler = () => {
  89. let watcher
  90. let ws
  91. return {
  92. MESSAGE_TYPE: TYPE,
  93. watch(types, cb, filter) {
  94. ws = init()
  95. watcher = new Watcher(types, cb, filter)
  96. return watcher
  97. },
  98. destroy() {
  99. // watcher._destroy()
  100. ws.close()
  101. watcher = null
  102. ws = null
  103. }
  104. }
  105. }
  106. export default handler