initWebSocket.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. function getRandomString(len = 10) {
  2. const chars =
  3. "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  4. const maxPos = chars.length;
  5. let randomString = "";
  6. for (let i = 0; i < len; i++) {
  7. randomString += chars.charAt(Math.floor(Math.random() * maxPos));
  8. }
  9. return randomString;
  10. }
  11. class InitWebSocketClass {
  12. // console.log(1111);
  13. // 为了防止user获取不到,从外面调用ras/user/info接口传入userCode,不要从cookie读,否则可能第一次出现读取不到的状况
  14. constructor(
  15. userCode = localStorage.getItem('userId'),
  16. token = localStorage.getItem('token'),
  17. options,
  18. socketCode = getRandomString(),
  19. ) {
  20. this.websocket = null; // 初始化websocket
  21. this.socketCode = socketCode;
  22. this.defaults = {
  23. // 默认配置,可以通过option覆盖
  24. protocol: defaultConfig.protocol, // 协议
  25. letLoginIp: defaultConfig.loginIp, // 环境配置
  26. letPort: defaultConfig.port, // 端口配置
  27. letUserCode: userCode, // 用户code
  28. letUserName: localStorage.getItem('userName'), // 环境登陆账号
  29. wsUri: 'ws://localhost:1234',
  30. token: token, // token
  31. getVersion: null, // option里面的获取版本信息的回调函数通知
  32. loginSuccess: null, // 登陆成功会调通知
  33. errorInfo: null, // 异常回调通知
  34. onCreateVideoSuccess: false, // 客户端被拉起通知
  35. oncheckSocket: null,
  36. onCreateGroupChatSuccess: null, // 群聊创建成功通知
  37. onSocketBackInfos: null, // 客户端返回信息
  38. };
  39. this.settings = Object.assign({}, this.defaults, options); // 覆盖默认配置操作
  40. this.websocketOnerror = false;
  41. this.ClientLocalVersion = 0; // 获取本地版本号
  42. this.timeId = null;
  43. }
  44. getSettings() {
  45. return this.settings;
  46. }
  47. webSocketSend(data) {
  48. // console.log(data);
  49. // console.log(this.websocket);
  50. if (this.websocket == null) {
  51. return;
  52. }
  53. if (this.websocket.readyState === 1) {
  54. // console.log(this.websocket.readyState)
  55. this.websocket.send(data);
  56. } else {
  57. setTimeout(() => {
  58. this.webSocketSend(data);
  59. }, 1000);
  60. }
  61. }
  62. initWebSocket() {
  63. // 初始化WebSocket
  64. return new Promise((resolve, reject) => {
  65. try {
  66. if (typeof WebSocket === 'function') {
  67. this.WebSocket = WebSocket;
  68. }
  69. if (this.websocket && this.websocket.readyState === 1) {
  70. this.websocket.close();
  71. }
  72. const settings = this.settings;
  73. window.initWebSocket = this.websocket = new WebSocket(settings.wsUri);
  74. this.websocket.onopen = () => {
  75. this.login();
  76. };
  77. this.websocket.onclose = () => {
  78. // DISCONNECTED
  79. };
  80. this.websocket.onmessage = (evt) => {
  81. // console.log(evt);
  82. if (evt && evt.data) {
  83. if (evt.data === 'client is runing!') {
  84. // 用隐藏方法,只断连接去拉视频后的返回
  85. this.startNotice();
  86. this.settings.loginSuccess &&
  87. this.settings.loginSuccess('success');
  88. resolve('success');
  89. return;
  90. }
  91. const data = JSON.parse(evt.data);
  92. // console.log(data);
  93. if (data && data.params && data.params.loginResult === 0) {
  94. this.startNotice(); // 去获取本地版本号
  95. this.settings.loginSuccess &&
  96. this.settings.loginSuccess('success');
  97. resolve('success');
  98. } else if (data && data.method === 'notifyClientLocalVersion') {
  99. this.ClientLocalVersion = Number(
  100. JSON.parse(evt.data).params.clientVersion,
  101. );
  102. this.settings.getVersion &&
  103. this.settings.getVersion(
  104. Number(JSON.parse(evt.data).params.clientVersion),
  105. );
  106. } else if (data && data.method === 'createVideoDialogReuslt') {
  107. // 客户端被拉起通知
  108. const createVideoReuslt = Number(
  109. JSON.parse(evt.data).params.result,
  110. );
  111. const flag = createVideoReuslt === 0;
  112. this.settings.onCreateVideoSuccess &&
  113. this.settings.onCreateVideoSuccess(flag);
  114. } else if (data && data.method === 'runinfo') {
  115. // 运行异常状态通知
  116. this.settings.errorInfo &&
  117. this.settings.errorInfo(data.params.info);
  118. } else {
  119. //
  120. }
  121. // 重点:统一分发客户端ws消息
  122. this.settings.onSocketBackInfos &&
  123. this.settings.onSocketBackInfos(data);
  124. }
  125. };
  126. this.websocket.onerror = () => {
  127. this.websocketOnerror = true;
  128. reject('error');
  129. };
  130. } catch (exception) {
  131. // this.debug('ERROR: ' + exception);
  132. }
  133. });
  134. }
  135. login() {
  136. this.checkSocket();
  137. const params = {
  138. loginIp: this.settings.letLoginIp,
  139. method: 'login',
  140. params: {
  141. loginPort: '8320',
  142. // protocol: this.settings.protocol,
  143. token: this.settings.token,
  144. userName: this.settings.letUserName,
  145. userId: this.settings.letUserCode,
  146. browser: 'chrome',
  147. version: '80.0.3987.100',
  148. platform: 'win64',
  149. softphoneEnable: 1,
  150. https:1
  151. },
  152. userCode: this.settings.letUserCode,
  153. // socketCode: this.socketCode,
  154. // webTitle: document.title // 视频存在的tab业标题不能重复
  155. };
  156. console.log(params);
  157. this.webSocketSend(JSON.stringify(params));
  158. this.timer();
  159. }
  160. UpdateWebTitle() {
  161. //切换路由的的时候需要调用此方法更新页面标题。路由标题名不能重复。
  162. // this.checkSocket();
  163. const params = {
  164. loginIp: this.settings.letLoginIp,
  165. method: 'UpdateWebTitle',
  166. params: {
  167. webTitle: document.title,
  168. },
  169. userCode: this.settings.letUserCode,
  170. socketCode: this.socketCode,
  171. };
  172. this.webSocketSend(JSON.stringify(params));
  173. }
  174. // 客户端心跳
  175. timer() {
  176. this.timeId = setInterval(() => {
  177. const params = {
  178. loginIp: this.settings.letLoginIp,
  179. method: 'heartbeat',
  180. userCode: this.settings.letUserCode,
  181. // socketCode: this.socketCode
  182. };
  183. if (this.websocket) {
  184. this.webSocketSend(JSON.stringify(params));
  185. }
  186. }, 10000);
  187. }
  188. // 停止socket连接
  189. stopWebSocket() {
  190. if (this.websocket) {
  191. this.websocket.close();
  192. }
  193. this.websocket = null;
  194. this.timeId && clearInterval(this.timeId);
  195. }
  196. //检查socket状态,通过oncheckSocket回调参数,可以判断本地有木有安装客户端
  197. checkSocket() {
  198. if (this.websocket != null) {
  199. let stateStr;
  200. // console.log(this.websocket.readyState);
  201. switch (this.websocket.readyState) {
  202. case 0: {
  203. stateStr = 'CONNECTING';
  204. break;
  205. }
  206. case 1: {
  207. stateStr = 'OPEN';
  208. break;
  209. }
  210. case 2: {
  211. stateStr = 'CLOSING';
  212. break;
  213. }
  214. case 3: {
  215. stateStr = 'CLOSED';
  216. break;
  217. }
  218. default: {
  219. stateStr = 'UNKNOW';
  220. break;
  221. }
  222. }
  223. this.settings.oncheckSocket &&
  224. this.settings.oncheckSocket(stateStr === 'OPEN');
  225. } else {
  226. //
  227. }
  228. }
  229. //获取本地客户端版本号
  230. startNotice() {
  231. const params = JSON.stringify({
  232. loginIp: this.settings.letLoginIp,
  233. method: 'getVersion',
  234. userCode: this.settings.letUserCode,
  235. // socketCode: this.socketCode,
  236. });
  237. this.webSocketSend(params);
  238. }
  239. // 升级客户端方法
  240. setClientDownInfo() {
  241. const origin = location.origin;
  242. const params = JSON.stringify({
  243. loginIp: this.settings.letLoginIp,
  244. method: 'setClientDownInfo',
  245. params: {
  246. newClientVersion: this.ClientLocalVersion, // 获取的本地版本号
  247. clientDownUrl: `${origin}/data/VSL/DSSEnterpriseClient/DSS_LightWeight_Client.zip`, // 客户端在服务器上的地址,本地调试放开下面你的getCookie("ip")
  248. // "clientDownUrl": `http://${getCookie("ip")}:8314/TheNextWebApp/resources/DSS_LightWeight_Client.zip` // 客户端在服务器上的地址文件
  249. },
  250. userCode: this.settings.letUserCode,
  251. socketCode: this.socketCode,
  252. });
  253. this.webSocketSend(params);
  254. }
  255. closeClient() {
  256. // const params = {
  257. // loginIp: this.settings.letLoginIp,
  258. // method: "logout",
  259. // userCode: this.settings.letUserCode,
  260. // socketCode: this.socketCode
  261. // };
  262. // this.webSocketSend(JSON.stringify(params));
  263. // this.stopWebSocket();
  264. // this.websocket = null;
  265. }
  266. // 通用方法第三方传方法名和参数体
  267. commonUse(method, paramsObj) {
  268. const params = JSON.stringify({
  269. loginIp: this.settings.letLoginIp,
  270. method: method,
  271. userCode: this.settings.letUserCode,
  272. params: paramsObj,
  273. socketCode: this.socketCode,
  274. });
  275. this.webSocketSend(params);
  276. }
  277. // 创建群聊界面
  278. openCreateGroupChatDlg() {
  279. const params = JSON.stringify({
  280. loginIp: this.settings.letLoginIp,
  281. method: 'openCreateGroupChatDlg',
  282. userCode: this.settings.letUserCode,
  283. socketCode: this.socketCode,
  284. });
  285. this.webSocketSend(params);
  286. }
  287. // 创建群聊会商-邀请一个用户进去会商
  288. createGroupChat(obj) {
  289. // const ccp1 = JSON.stringify({
  290. // loginIp: this.settings.letLoginIp,
  291. // method: 'registerSoftPhone',
  292. // userCode: this.settings.letUserCode,
  293. // });
  294. // this.webSocketSend(ccp1);
  295. setTimeout(() => {
  296. const ccp = JSON.stringify({
  297. loginIp: this.settings.letLoginIp,
  298. method: 'createGroupChat',
  299. params: obj,
  300. userCode: this.settings.letUserCode,
  301. });
  302. this.webSocketSend(ccp);
  303. }, 1000);
  304. // const params = JSON.stringify({
  305. // loginIp: this.settings.letLoginIp,
  306. // method: 'createGroupChat',
  307. // userCode: this.settings.letUserCode,
  308. // socketCode: this.socketCode,
  309. // params: {
  310. // chatName: obj.chatName,
  311. // bOpenDirect: obj.bOpenDirect, // 是否打开音视频通话标志,true 创建成功后打开音视频通话,false 创建成功后不打开音视频通话
  312. // memberInfo: [
  313. // // 成员信息
  314. // {
  315. // memberId: obj.memberId, // 成员id
  316. // memberName: obj.memberName, // 成员名称
  317. // regionId: obj.regionId, // 成员的pass域id
  318. // memberType: obj.memberType, // 成员类型,Integer ,1 单兵;2 普通电话;3 对讲机;4 app|客户端;6 可是话机; 7 汇接话机(PSTN、手机号码等); 8 车载;9 无人机喊话云台; 10 无人机地面站对讲平台; 11 ipc; 12 NVR
  319. // memberCode: obj.memberCode, // 成员设备编码
  320. // memberNumber: obj.memberNumber, // 成员号码
  321. // },
  322. // ],
  323. // },
  324. // });
  325. }
  326. // 打开客户端指定模块
  327. openClientModule(moduleID) {
  328. const params = JSON.stringify({
  329. loginIp: this.settings.letLoginIp,
  330. method: 'openClientModule',
  331. userCode: this.settings.letUserCode,
  332. params: {
  333. moduleID: moduleID, // 模块ID,0 视频监控;1 录像回放;2 视频上墙;3 下载中心
  334. },
  335. socketCode: this.socketCode,
  336. });
  337. this.webSocketSend(params);
  338. }
  339. }
  340. window.InitWebSocketClass = InitWebSocketClass;