Server.js 2.8 KB

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