initWebSocket.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. },
  150. userCode: this.settings.letUserCode,
  151. // socketCode: this.socketCode,
  152. // webTitle: document.title // 视频存在的tab业标题不能重复
  153. };
  154. console.log(params);
  155. this.webSocketSend(JSON.stringify(params));
  156. this.timer();
  157. }
  158. UpdateWebTitle() {
  159. //切换路由的的时候需要调用此方法更新页面标题。路由标题名不能重复。
  160. // this.checkSocket();
  161. const params = {
  162. loginIp: this.settings.letLoginIp,
  163. method: 'UpdateWebTitle',
  164. params: {
  165. webTitle: document.title,
  166. },
  167. userCode: this.settings.letUserCode,
  168. socketCode: this.socketCode,
  169. };
  170. this.webSocketSend(JSON.stringify(params));
  171. }
  172. // 客户端心跳
  173. timer() {
  174. this.timeId = setInterval(() => {
  175. const params = {
  176. loginIp: this.settings.letLoginIp,
  177. method: 'heartbeat',
  178. userCode: this.settings.letUserCode,
  179. // socketCode: this.socketCode
  180. };
  181. if (this.websocket) {
  182. this.webSocketSend(JSON.stringify(params));
  183. }
  184. }, 10000);
  185. }
  186. // 停止socket连接
  187. stopWebSocket() {
  188. if (this.websocket) {
  189. this.websocket.close();
  190. }
  191. this.websocket = null;
  192. this.timeId && clearInterval(this.timeId);
  193. }
  194. //检查socket状态,通过oncheckSocket回调参数,可以判断本地有木有安装客户端
  195. checkSocket() {
  196. if (this.websocket != null) {
  197. let stateStr;
  198. // console.log(this.websocket.readyState);
  199. switch (this.websocket.readyState) {
  200. case 0: {
  201. stateStr = 'CONNECTING';
  202. break;
  203. }
  204. case 1: {
  205. stateStr = 'OPEN';
  206. break;
  207. }
  208. case 2: {
  209. stateStr = 'CLOSING';
  210. break;
  211. }
  212. case 3: {
  213. stateStr = 'CLOSED';
  214. break;
  215. }
  216. default: {
  217. stateStr = 'UNKNOW';
  218. break;
  219. }
  220. }
  221. this.settings.oncheckSocket &&
  222. this.settings.oncheckSocket(stateStr === 'OPEN');
  223. } else {
  224. //
  225. }
  226. }
  227. //获取本地客户端版本号
  228. startNotice() {
  229. const params = JSON.stringify({
  230. loginIp: this.settings.letLoginIp,
  231. method: 'getVersion',
  232. userCode: this.settings.letUserCode,
  233. // socketCode: this.socketCode,
  234. });
  235. this.webSocketSend(params);
  236. }
  237. // 升级客户端方法
  238. setClientDownInfo() {
  239. const origin = location.origin;
  240. const params = JSON.stringify({
  241. loginIp: this.settings.letLoginIp,
  242. method: 'setClientDownInfo',
  243. params: {
  244. newClientVersion: this.ClientLocalVersion, // 获取的本地版本号
  245. clientDownUrl: `${origin}/data/VSL/DSSEnterpriseClient/DSS_LightWeight_Client.zip`, // 客户端在服务器上的地址,本地调试放开下面你的getCookie("ip")
  246. // "clientDownUrl": `http://${getCookie("ip")}:8314/TheNextWebApp/resources/DSS_LightWeight_Client.zip` // 客户端在服务器上的地址文件
  247. },
  248. userCode: this.settings.letUserCode,
  249. socketCode: this.socketCode,
  250. });
  251. this.webSocketSend(params);
  252. }
  253. closeClient() {
  254. // const params = {
  255. // loginIp: this.settings.letLoginIp,
  256. // method: "logout",
  257. // userCode: this.settings.letUserCode,
  258. // socketCode: this.socketCode
  259. // };
  260. // this.webSocketSend(JSON.stringify(params));
  261. // this.stopWebSocket();
  262. // this.websocket = null;
  263. }
  264. // 通用方法第三方传方法名和参数体
  265. commonUse(method, paramsObj) {
  266. const params = JSON.stringify({
  267. loginIp: this.settings.letLoginIp,
  268. method: method,
  269. userCode: this.settings.letUserCode,
  270. params: paramsObj,
  271. socketCode: this.socketCode,
  272. });
  273. this.webSocketSend(params);
  274. }
  275. // 创建群聊界面
  276. openCreateGroupChatDlg() {
  277. const params = JSON.stringify({
  278. loginIp: this.settings.letLoginIp,
  279. method: 'openCreateGroupChatDlg',
  280. userCode: this.settings.letUserCode,
  281. socketCode: this.socketCode,
  282. });
  283. this.webSocketSend(params);
  284. }
  285. // 创建群聊会商-邀请一个用户进去会商
  286. createGroupChat(obj) {
  287. const ccp1 = JSON.stringify({
  288. loginIp: this.settings.letLoginIp,
  289. method: 'registerSoftPhone',
  290. userCode: this.settings.letUserCode,
  291. });
  292. this.webSocketSend(ccp1);
  293. setTimeout(() => {
  294. const ccp = JSON.stringify({
  295. loginIp: this.settings.letLoginIp,
  296. method: 'createGroupChat',
  297. params: obj,
  298. userCode: this.settings.letUserCode,
  299. });
  300. this.webSocketSend(ccp);
  301. }, 1000);
  302. // const params = JSON.stringify({
  303. // loginIp: this.settings.letLoginIp,
  304. // method: 'createGroupChat',
  305. // userCode: this.settings.letUserCode,
  306. // socketCode: this.socketCode,
  307. // params: {
  308. // chatName: obj.chatName,
  309. // bOpenDirect: obj.bOpenDirect, // 是否打开音视频通话标志,true 创建成功后打开音视频通话,false 创建成功后不打开音视频通话
  310. // memberInfo: [
  311. // // 成员信息
  312. // {
  313. // memberId: obj.memberId, // 成员id
  314. // memberName: obj.memberName, // 成员名称
  315. // regionId: obj.regionId, // 成员的pass域id
  316. // memberType: obj.memberType, // 成员类型,Integer ,1 单兵;2 普通电话;3 对讲机;4 app|客户端;6 可是话机; 7 汇接话机(PSTN、手机号码等); 8 车载;9 无人机喊话云台; 10 无人机地面站对讲平台; 11 ipc; 12 NVR
  317. // memberCode: obj.memberCode, // 成员设备编码
  318. // memberNumber: obj.memberNumber, // 成员号码
  319. // },
  320. // ],
  321. // },
  322. // });
  323. }
  324. // 打开客户端指定模块
  325. openClientModule(moduleID) {
  326. const params = JSON.stringify({
  327. loginIp: this.settings.letLoginIp,
  328. method: 'openClientModule',
  329. userCode: this.settings.letUserCode,
  330. params: {
  331. moduleID: moduleID, // 模块ID,0 视频监控;1 录像回放;2 视频上墙;3 下载中心
  332. },
  333. socketCode: this.socketCode,
  334. });
  335. this.webSocketSend(params);
  336. }
  337. }
  338. window.InitWebSocketClass = InitWebSocketClass;