Server.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ping(){
  34. let that = this;
  35. this.timer = setInterval(()=>{
  36. that.send({type:'ping'});
  37. }, PINGINTERVAL);
  38. }
  39. /**
  40. * 关闭链接
  41. */
  42. close(){
  43. clearInterval(this.timer);
  44. this.SocketTask.close();
  45. this.connectStatus = false;
  46. }
  47. /**
  48. * 发送消息
  49. */
  50. send(data){
  51. let that = this;
  52. return new Promise((reslove, reject)=>{
  53. that.SocketTask.send({
  54. data: JSON.stringify(data),
  55. success:reslove,
  56. fail:reject,
  57. });
  58. });
  59. }
  60. onMessage(res){
  61. this.debug && console.log(res);
  62. const { type, data = {} } = JSON.parse(res.data);
  63. this.$emit(type, data);
  64. }
  65. onClose(){
  66. this.close();
  67. }
  68. onError(res){
  69. this.debug && console.log(res);
  70. this.$emit('socket_error', res);
  71. this.connectStatus = false;
  72. }
  73. onOpen(res){
  74. this.debug && console.log('链接成功');
  75. this.connectStatus = true;
  76. if(this.app.globalData === undefined)
  77. console.error('无法获取token登录失败');
  78. else
  79. this.send({ type: 'login', data: this.app.globalData.token});
  80. this.ping();
  81. }
  82. /**
  83. * 注册事件
  84. * @param string name 事件名称
  85. * @param callback successFun 回调函数
  86. */
  87. $on(name, successFun) {
  88. let taht = this;
  89. if (typeof name === 'object') {
  90. name.forEach(item => {
  91. if (!taht.watcherList[item]) this.watcherList[name] = [];
  92. taht.watcherList[item].push(successFun);
  93. })
  94. } else {
  95. if (!this.watcherList[name]) {
  96. this.watcherList[name] = []
  97. }
  98. this.watcherList[name].push(successFun);
  99. }
  100. }
  101. /**
  102. * 执行事件
  103. * @param string type 事件名称
  104. * @param array data 参数
  105. */
  106. $emit(type, data){
  107. let onCallbacks = this.watcherList[type]
  108. onCallbacks.forEach(element => {
  109. element.apply(this, arguments,data)
  110. });
  111. }
  112. }