socket.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import configService from '@/common/service/config.service.js';
  2. import store from '@/store/index.js';
  3. import {ACCESS_TOKEN} from '@/common/util/constants.js'
  4. class socket {
  5. constructor(options) {
  6. this.socketUrl = configService.apiUrl;
  7. this.socketStart = false;
  8. this.monitorSocketError();
  9. this.monitorSocketClose();
  10. this.socketReceive();
  11. }
  12. init(socket_type,callback) {
  13. const _this = this;
  14. if (configService.apiUrl) {
  15. if(this.socketStart){
  16. console.log('webSocket已经启动了');
  17. }else{
  18. let userid=store.state.userid?store.state.userid:store.getters.userid;
  19. let url=this.socketUrl.replace("https://","wss://").replace("http://","ws://")+"/"+socket_type+"/"+userid+"_app";
  20. console.log("启动this.socketUrl连接地址:",url);
  21. // update-begin-author:lsq date:20230506 for:[issues/497]websocket连接打开失败
  22. let token = uni.getStorageSync(ACCESS_TOKEN)
  23. uni.connectSocket({
  24. url: url,
  25. method: 'GET',
  26. protocols:[token]
  27. });
  28. // update-end-author:lsq date:20230506 for:[issues/497]websocket连接打开失败
  29. uni.onSocketOpen((res) => {
  30. this.socketStart = true;
  31. callback && callback();
  32. console.log('WebSocket连接已打开!');
  33. });
  34. /*setTimeout(() => {
  35. _this.getHeartbeat();
  36. }, 5000);*/
  37. }
  38. }else{
  39. console.log('config/baseUrl socketUrl为空');
  40. }
  41. }
  42. //Socket给服务器发送消息
  43. send(data, callback) {
  44. const _this = this;
  45. if (store.state.userid) {
  46. data.userUid =store.state.userid;
  47. }
  48. console.log(data);
  49. uni.sendSocketMessage({
  50. data: JSON.stringify(data),
  51. success: () => {
  52. callback && callback(true);
  53. },
  54. fail: () => {
  55. callback && callback(false);
  56. }
  57. });
  58. }
  59. //Socket接收服务器发送过来的消息
  60. socketReceive() {
  61. const _this = this;
  62. uni.onSocketMessage(function(res) {
  63. console.log("APP:----》收到服务器内容:",res);
  64. let data = JSON.parse(res.data);
  65. //console.log('收到服务器内容:', data);
  66. _this.acceptMessage && _this.acceptMessage(data);
  67. });
  68. }
  69. //关闭Socket
  70. closeSocket() {
  71. const _this = this;
  72. uni.closeSocket();
  73. _this.socketStart = false;
  74. }
  75. //监听Socket关闭
  76. monitorSocketClose() {
  77. const _this = this;
  78. uni.onSocketClose(function(res) {
  79. console.log('WebSocket 已关闭!');
  80. _this.socketStart = false;
  81. setTimeout(function() {
  82. //_this.init();
  83. }, 3000);
  84. });
  85. }
  86. //监听Socket错误
  87. monitorSocketError() {
  88. const _this = this;
  89. uni.onSocketError(function(res) {
  90. _this.socketStart = false;
  91. console.log('WebSocket连接打开失败,请检查!');
  92. });
  93. }
  94. //心跳
  95. getHeartbeat() {
  96. const _this = this;
  97. this.send({
  98. type: "心跳",
  99. userUid: store.state.userid
  100. }, (val) => {
  101. setTimeout(() => {
  102. if (val) {
  103. //_this.getHeartbeat();
  104. } else {
  105. if(!_this.socketStart){
  106. //_this.init();
  107. }
  108. }
  109. }, 10000);
  110. });
  111. }
  112. };
  113. const mySocket = new socket();
  114. export default mySocket;