new_chat.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 $store from "@/store";
  11. import {
  12. HTTP_REQUEST_URL
  13. } from "@/config/app.js";
  14. import {
  15. VUE_APP_WS_URL
  16. } from "@/utils/index.js";
  17. import {
  18. getServerType
  19. } from '@/api/api.js';
  20. const Socket = function() {
  21. // this.ws.close(this.close.bind(this));
  22. };
  23. // #ifdef H5
  24. function wss(wsSocketUrl) {
  25. let ishttps = document.location.protocol == 'https:';
  26. if (ishttps) {
  27. return wsSocketUrl.replace('ws:', 'wss:');
  28. } else {
  29. return wsSocketUrl.replace('wss:', 'ws:');
  30. }
  31. }
  32. // #endif
  33. Socket.prototype = {
  34. // close() {
  35. // clearInterval(this.timer);
  36. // this.ws.close();
  37. // },
  38. onSocketOpen: function(my) {
  39. uni.$emit('socketOpen', my)
  40. },
  41. init: function() {
  42. var that = this;
  43. this.timer = setInterval(function() {
  44. that.send({
  45. type: "ping"
  46. });
  47. }, 10000);
  48. },
  49. send: function(data) {
  50. let datas = JSON.stringify(data)
  51. return uni.sendSocketMessage({
  52. data: datas
  53. });
  54. },
  55. onMessage: function(res) {
  56. const {
  57. type,
  58. data = {}
  59. } = JSON.parse(res.data);
  60. uni.$emit(type, data)
  61. },
  62. onClose: function() {
  63. uni.closeSocket()
  64. clearInterval(this.timer);
  65. uni.$emit("socket_close");
  66. },
  67. onError: function(e) {
  68. uni.$emit("socket_error", e);
  69. },
  70. close: function() {
  71. uni.closeSocket();
  72. },
  73. onStart: function(token, form_type) {
  74. let wssUrl = `${VUE_APP_WS_URL}`
  75. this.ws = uni.connectSocket({
  76. url: wssUrl + '?type=user&token=' + token + '&form_type=' + form_type,
  77. header: {
  78. 'content-type': 'application/json'
  79. },
  80. method: 'GET',
  81. success: (res) => {}
  82. });
  83. this.ws.onOpen(this.onSocketOpen.bind(this))
  84. this.ws.onError(this.onError.bind(this));
  85. this.ws.onMessage(this.onMessage.bind(this))
  86. this.ws.onClose(this.onClose.bind(this));
  87. }
  88. };
  89. Socket.prototype.constructor = Socket;
  90. export default Socket;