WebIM.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 'use strict'
  2. import '../Sdk/dist/strophe-1.2.8.js'
  3. import websdk from '../Sdk'
  4. import xmldom from 'xmldom'
  5. import config from './WebIMConfig'
  6. // for rest api -> http
  7. import Api from '../Services/Api'
  8. // init DOMParser / document for strophe and sdk
  9. // window.WebIM.config.isDebug = true
  10. console = console || {}
  11. console.group = console.group || function () {
  12. }
  13. console.groupEnd = console.groupEnd || function () {
  14. }
  15. let WebIM = window.WebIM = websdk
  16. window.WebIM.config = config
  17. window.DOMParser = xmldom.DOMParser
  18. let document = window.document = new DOMParser().parseFromString("<?xml version='1.0'?>\n", 'text/xml')
  19. if (WebIM.config.isDebug) {
  20. function ts() {
  21. var d = new Date()
  22. var Hours = d.getHours() // 获取当前小时数(0-23)
  23. var Minutes = d.getMinutes() // 获取当前分钟数(0-59)
  24. var Seconds = d.getSeconds() // 获取当前秒数(0-59)
  25. return (Hours < 10 ? '0' + Hours : Hours) + ':' + (Minutes < 10 ? '0' + Minutes : Minutes) + ':' + (Seconds < 10 ? '0' + Seconds : Seconds) + ' '
  26. }
  27. window.Strophe.log = function (level, msg) {
  28. try {
  29. // console.group('%crecv # ' + ts(), 'color: blue; font-size: large')
  30. console.log('%c ' + ts() + ' recv: ' + msg, 'color: green')
  31. // console.groupEnd()
  32. } catch (e) {
  33. }
  34. }
  35. window.Strophe.Connection.prototype.rawOutput = function (data) {
  36. try {
  37. // console.group('%csend # ' + ts(), 'color: blue; font-size: large')
  38. console.log('%c ' + ts() + ' send: ' + data, 'color: blue')
  39. // console.groupEnd()
  40. } catch (e) {
  41. }
  42. }
  43. }
  44. /**
  45. * Set autoSignIn as true (autoSignInName and autoSignInPwd are configured below),
  46. * You can auto signed in each time when you refresh the page in dev model.
  47. */
  48. WebIM.config.autoSignIn = false
  49. if (WebIM.config.autoSignIn) {
  50. WebIM.config.autoSignInName = 'liuwz'
  51. WebIM.config.autoSignInPwd = '1'
  52. }
  53. // var stropheConn = new window.Strophe.Connection("ws://im-api.easemob.com/ws/", {
  54. // inactivity: 30,
  55. // maxRetries: 5,
  56. // pollingTime: 4500
  57. // });
  58. //
  59. // stropheConn.connect(
  60. // 'easemob-demo#chatdemoui_liuwz@easemob.com',
  61. // '$t$' + 'YWMtmbQEBKKIEeaGmMtXyg5n1wAAAVlkQvGO2WOJGlMCEJKM4VV9GCMnb_XLCXU',
  62. // function() {
  63. // console.log(arguments, 'ggogogo');
  64. // }, stropheConn.wait, stropheConn.hold);
  65. WebIM.conn = new WebIM.connection({
  66. isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
  67. https: WebIM.config.https,
  68. url: WebIM.config.xmppURL,
  69. isAutoLogin: true,
  70. heartBeatWait: WebIM.config.heartBeatWait,
  71. autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
  72. autoReconnectInterval: WebIM.config.autoReconnectInterval
  73. })
  74. //https://a1.easemob.com/easemob-demo/chatdemoui/users
  75. let appKeyPair = WebIM.config.appkey.split('#')
  76. export let api = Api.create(`${WebIM.config.apiURL}/${appKeyPair[0]}/${appKeyPair[1]}`)
  77. WebIM.api = api
  78. export default WebIM