socket.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const LOG_DEBUG = false;
  2. export default new class {
  3. constructor() {
  4. this.handlers = {};
  5. this.queue = [];
  6. this.status = false;
  7. this.timer = null;
  8. this.timer1 = null;
  9. this.timer2 =null;
  10. this.url='';
  11. }
  12. connect(url) {
  13. this.url=url;
  14. uni.connectSocket({
  15. url,
  16. fail: () => {
  17. this.status=false;
  18. if(this.timer1!=null)clearInterval(this.timer1)
  19. this.timer1=setInterval(()=>{
  20. if(!this.status) {
  21. this.connect(this.url);
  22. }
  23. }, 3* 1000);
  24. },
  25. });
  26. if(this.timer!=null)clearInterval(this.timer)
  27. this.timer = setInterval(()=>{
  28. if(!this.status) {
  29. this.connect(url);
  30. LOG_DEBUG && console.log('[socket] reconnect');
  31. }
  32. }, 5 * 1000);
  33. uni.onSocketOpen(() => {
  34. this.status = true;
  35. while (this.queue.length) this.send(this.queue.pop());
  36. LOG_DEBUG && console.log('[socket] init');
  37. clearInterval(this.timer2)
  38. this.timer2=setInterval(()=>{
  39. if(this.status) {
  40. this.send({
  41. type: 'ping'
  42. });
  43. }
  44. }, 5* 1000);
  45. this.emit('init');
  46. });
  47. uni.onSocketError(() => {
  48. this.status = false;
  49. LOG_DEBUG && console.log('[socket] error');
  50. this.emit('error');
  51. });
  52. uni.onSocketMessage((res) => {
  53. let result = JSON.parse(res.data);
  54. if (result) {
  55. LOG_DEBUG && console.log('[socket] recevice : ', result);
  56. switch (result.type) {
  57. case 'ping':
  58. // console.log(result);
  59. this.send({
  60. type: 'pong'
  61. });
  62. break;
  63. case 'message':
  64. this.send({
  65. type: 'commit',
  66. data: result.commit_id
  67. });
  68. this.emit('message', result.data);
  69. break;
  70. default:
  71. this.emit(result.type, result);
  72. break;
  73. }
  74. }
  75. });
  76. uni.onSocketClose(() => {
  77. this.status = false;
  78. LOG_DEBUG && console.log('[socket] close');
  79. this.emit('close');
  80. });
  81. }
  82. send(data) {
  83. if (this.status) {
  84. LOG_DEBUG && console.log('[socket] send', data);
  85. uni.sendSocketMessage({
  86. data: typeof(data) == 'string' ? data : JSON.stringify(data),
  87. success: () => {
  88. },
  89. fail: () => {
  90. this.status = false;
  91. this.connect(this.url);
  92. this.queue.push(data);
  93. },
  94. });
  95. } else {
  96. this.queue.push(data);
  97. this.connect(this.url);
  98. }
  99. }
  100. on(eventType, handler) {
  101. var self = this;
  102. if (!(eventType in self.handlers)) {
  103. self.handlers[eventType] = [];
  104. }
  105. self.handlers[eventType].push(handler);
  106. return this;
  107. }
  108. emit(eventType) {
  109. var self = this;
  110. var handlerArgs = Array.prototype.slice.call(arguments, 1);
  111. if (!self.handlers[eventType]) return self;
  112. for (var i = 0; i < self.handlers[eventType].length; i++) {
  113. self.handlers[eventType][i].apply(self, handlerArgs);
  114. }
  115. return self;
  116. }
  117. off(eventType, handler) {
  118. var currentEvent = this.handlers[eventType];
  119. var len = 0;
  120. if (currentEvent) {
  121. len = currentEvent.length;
  122. for (var i = len - 1; i >= 0; i--) {
  123. if (currentEvent[i] === handler) {
  124. currentEvent.splice(i, 1);
  125. }
  126. }
  127. }
  128. return this;
  129. }
  130. };