socket.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // +---------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +---------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  5. // +---------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +---------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +---------------------------------------------------------------------
  10. import { wss, getCookies, setCookies } from '@/libs/util';
  11. import Setting from '@/setting';
  12. import { getWorkermanUrl } from '@/api/kefu';
  13. import Vue from 'vue';
  14. const vm = new Vue();
  15. let wsAdminSocketUrl = getCookies('WS_ADMIN_URL') || '';
  16. let wsKefuSocketUrl = getCookies('WS_CHAT_URL') || '';
  17. class wsSocket {
  18. constructor(opt) {
  19. this.ws = null;
  20. this.opt = opt || {};
  21. this.init(opt.key);
  22. }
  23. onOpen(key = false) {
  24. this.opt.open && this.opt.open();
  25. let that = this;
  26. // this.send({
  27. // type: 'login',
  28. // data: util.cookies.get('token')
  29. // }).then(() => {
  30. // that.ping();
  31. // });
  32. that.ping();
  33. this.socketStatus = true;
  34. }
  35. init(key) {
  36. let wsUrl = '';
  37. if (key == 1) {
  38. wsUrl = wsAdminSocketUrl;
  39. }
  40. if (key == 2) {
  41. wsUrl = wsKefuSocketUrl;
  42. }
  43. if (wsUrl) {
  44. this.ws = new WebSocket(wsUrl);
  45. this.ws.onopen = this.onOpen.bind(this);
  46. this.ws.onerror = this.onError.bind(this);
  47. this.ws.onmessage = this.onMessage.bind(this);
  48. this.ws.onclose = this.onClose.bind(this);
  49. }
  50. }
  51. ping() {
  52. var that = this;
  53. this.timer = setInterval(function () {
  54. that.send({ type: 'ping' });
  55. }, 10000);
  56. }
  57. send(data) {
  58. return new Promise((resolve, reject) => {
  59. try {
  60. this.ws.send(JSON.stringify(data));
  61. resolve({ status: true });
  62. } catch (e) {
  63. reject({ status: false });
  64. }
  65. });
  66. }
  67. onMessage(res) {
  68. this.opt.message && this.opt.message(res);
  69. }
  70. onClose() {
  71. this.timer && clearInterval(this.timer);
  72. this.opt.close && this.opt.close();
  73. }
  74. onError(e) {
  75. this.opt.error && this.opt.error(e);
  76. }
  77. $on(...args) {
  78. vm.$on(...args);
  79. }
  80. }
  81. function createSocket(key) {
  82. getWorkermanUrl().then((res) => {
  83. wsAdminSocketUrl = res.data.admin;
  84. wsKefuSocketUrl = res.data.chat;
  85. setCookies('WS_ADMIN_URL', res.data.admin);
  86. setCookies('WS_CHAT_URL', res.data.chat);
  87. });
  88. return new Promise((resolve, reject) => {
  89. const ws = new wsSocket({
  90. key,
  91. open() {
  92. resolve(ws);
  93. },
  94. error(e) {
  95. reject(e);
  96. },
  97. message(res) {
  98. const { type, data = {} } = JSON.parse(res.data);
  99. vm.$emit(type, data);
  100. },
  101. close(e) {
  102. vm.$emit('close', e);
  103. },
  104. });
  105. });
  106. }
  107. export const adminSocket = createSocket(1);
  108. export const Socket = createSocket(2);