Server.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. WSS_SERVER_URL,
  3. HEADER,
  4. SERVER_DEBUG,
  5. PINGINTERVAL
  6. } from './../config.js';
  7. export default class Server {
  8. constructor(app) {
  9. this.chatId = null // chat_id
  10. this.connectStatus = false // websocket 连接状态 false:未连接,true:已连接
  11. this.timer = null // 心跳
  12. this.watcherList = [] // 订阅者
  13. this.app = app // 方便在Chat内部操作app
  14. this.debug = SERVER_DEBUG //debug
  15. }
  16. connectSocket() {
  17. let that = this;
  18. this.chatId = new Date().getTime();
  19. this.SocketTask = wx.connectSocket({
  20. url: WSS_SERVER_URL,
  21. header: HEADER,
  22. method: 'post',
  23. success: res => {
  24. that.debug && console.log(res);
  25. },
  26. fail: err => {
  27. that.debug && console.log(err);
  28. }
  29. });
  30. wx.onSocketOpen(this.onOpen.bind(this));
  31. wx.onSocketMessage(this.onMessage.bind(this));
  32. wx.onSocketError(this.onError.bind(this));
  33. wx.onSocketClose(this.onClose.bind(this));
  34. }
  35. /**
  36. * 心跳
  37. */
  38. ping() {
  39. let that = this;
  40. this.timer = setInterval(() => {
  41. that.send({
  42. type: 'ping'
  43. });
  44. }, PINGINTERVAL);
  45. }
  46. /**
  47. * 关闭链接
  48. */
  49. close() {
  50. clearInterval(this.timer);
  51. this.SocketTask.close();
  52. this.connectStatus = false;
  53. }
  54. /**
  55. * 发送消息
  56. */
  57. send(data) {
  58. let that = this;
  59. return new Promise((reslove, reject) => {
  60. that.SocketTask.send({
  61. data: JSON.stringify(data),
  62. success: reslove,
  63. fail: reject,
  64. });
  65. });
  66. }
  67. onMessage(res) {
  68. this.debug && console.log(res);
  69. const {
  70. type,
  71. data = {}
  72. } = JSON.parse(res.data);
  73. this.$emit(type, data);
  74. }
  75. onClose() {
  76. this.close();
  77. }
  78. onError(res) {
  79. this.debug && console.log(res);
  80. this.$emit('socket_error', res);
  81. this.connectStatus = false;
  82. }
  83. onOpen(res) {
  84. this.debug && console.log('链接成功');
  85. this.connectStatus = true;
  86. if (this.app.globalData === undefined)
  87. console.error('无法获取token登录失败');
  88. else
  89. this.send({
  90. type: 'login',
  91. data: this.app.globalData.token
  92. });
  93. this.ping();
  94. }
  95. /**
  96. * 注册事件
  97. * @param string name 事件名称
  98. * @param callback successFun 回调函数
  99. */
  100. $on(name, successFun) {
  101. let taht = this;
  102. if (typeof name === 'object') {
  103. name.forEach(item => {
  104. if (!taht.watcherList[item]) this.watcherList[name] = [];
  105. taht.watcherList[item].push(successFun);
  106. })
  107. } else {
  108. if (!this.watcherList[name]) {
  109. this.watcherList[name] = []
  110. }
  111. this.watcherList[name].push(successFun);
  112. }
  113. }
  114. /**
  115. * 执行事件
  116. * @param string type 事件名称
  117. * @param array data 参数
  118. */
  119. $emit(type, data) {
  120. let onCallbacks = this.watcherList[type]
  121. onCallbacks.forEach(element => {
  122. element.apply(this, arguments, data)
  123. });
  124. }
  125. }