app-im-delegate.js 925 B

123456789101112131415161718192021222324252627282930313233343536
  1. import {getIMHandlerFactory} from "../libs/im-sdk/im-factory";
  2. import { IM_HOST } from './../config/config'
  3. import EventEmitter from './../utils/eventEmitter'
  4. export default class AppIMDelegate {
  5. constructor(app) {
  6. this._app = app;
  7. }
  8. onLaunch(options) {
  9. this.iIMHandler = getIMHandlerFactory;
  10. EventEmitter.on('login', () => {
  11. this.connectSocket()
  12. })
  13. EventEmitter.on('logout', () => {
  14. this.iIMHandler.closeConnection()
  15. })
  16. }
  17. onShow(options) {
  18. this.connectSocket()
  19. }
  20. onHide() {
  21. this.iIMHandler.closeConnection()
  22. }
  23. getIMHandlerDelegate() {
  24. return this.iIMHandler;
  25. }
  26. connectSocket () {
  27. const app = getApp()
  28. if (app.globalData.token) {
  29. this.iIMHandler.createConnection({options: {url: `${IM_HOST}/im/${app.globalData.userinfo.viewerId}`}});
  30. }
  31. }
  32. }