socket.js 2.6 KB

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