initWebSocket.js 14 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: "system", // 环境登陆账号
  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(
  74. settings.wsUri
  75. );
  76. this.websocket.onopen = () => {
  77. this.login();
  78. };
  79. this.websocket.onclose = () => {
  80. // DISCONNECTED
  81. };
  82. this.websocket.onmessage = evt => {
  83. // console.log(evt);
  84. if (evt && evt.data) {
  85. if (evt.data === "client is runing!") {
  86. // 用隐藏方法,只断连接去拉视频后的返回
  87. this.startNotice();
  88. this.settings.loginSuccess &&
  89. this.settings.loginSuccess("success");
  90. resolve("success");
  91. return;
  92. }
  93. const data = JSON.parse(evt.data);
  94. // console.log(data);
  95. if (
  96. data &&
  97. data.params &&
  98. data.params.loginResult === 0
  99. ) {
  100. this.startNotice(); // 去获取本地版本号
  101. this.settings.loginSuccess &&
  102. this.settings.loginSuccess("success");
  103. resolve("success");
  104. } else if (
  105. data &&
  106. data.method === "notifyClientLocalVersion"
  107. ) {
  108. this.ClientLocalVersion = Number(
  109. JSON.parse(evt.data).params.clientVersion
  110. );
  111. this.settings.getVersion &&
  112. this.settings.getVersion(
  113. Number(
  114. JSON.parse(evt.data).params
  115. .clientVersion
  116. )
  117. );
  118. } else if (
  119. data &&
  120. data.method === "createVideoDialogReuslt"
  121. ) {
  122. // 客户端被拉起通知
  123. const createVideoReuslt = Number(
  124. JSON.parse(evt.data).params.result
  125. );
  126. const flag = createVideoReuslt === 0;
  127. this.settings.onCreateVideoSuccess &&
  128. this.settings.onCreateVideoSuccess(flag);
  129. } else if (data && data.method === "runinfo") {
  130. // 运行异常状态通知
  131. this.settings.errorInfo &&
  132. this.settings.errorInfo(data.params.info);
  133. } else {
  134. //
  135. }
  136. // 重点:统一分发客户端ws消息
  137. this.settings.onSocketBackInfos &&
  138. this.settings.onSocketBackInfos(data);
  139. }
  140. };
  141. this.websocket.onerror = () => {
  142. this.websocketOnerror = true;
  143. reject("error");
  144. };
  145. } catch (exception) {
  146. // this.debug('ERROR: ' + exception);
  147. }
  148. });
  149. }
  150. login() {
  151. this.checkSocket();
  152. const params = {
  153. loginIp: this.settings.letLoginIp,
  154. method: "login",
  155. params: {
  156. loginPort: "8320",
  157. // protocol: this.settings.protocol,
  158. token: this.settings.token,
  159. userName: this.settings.letUserName,
  160. userId: this.settings.letUserCode,
  161. browser: "chrome",
  162. version: "80.0.3987.100",
  163. platform: "win64",
  164. },
  165. userCode: this.settings.letUserCode,
  166. // socketCode: this.socketCode,
  167. // webTitle: document.title // 视频存在的tab业标题不能重复
  168. };
  169. console.log(params);
  170. this.webSocketSend(JSON.stringify(params));
  171. this.timer();
  172. }
  173. UpdateWebTitle() {
  174. //切换路由的的时候需要调用此方法更新页面标题。路由标题名不能重复。
  175. // this.checkSocket();
  176. const params = {
  177. loginIp: this.settings.letLoginIp,
  178. method: "UpdateWebTitle",
  179. params: {
  180. webTitle: document.title
  181. },
  182. userCode: this.settings.letUserCode,
  183. socketCode: this.socketCode
  184. };
  185. this.webSocketSend(JSON.stringify(params));
  186. }
  187. // 客户端心跳
  188. timer() {
  189. this.timeId = setInterval(() => {
  190. const params = {
  191. loginIp: this.settings.letLoginIp,
  192. method: "heartbeat",
  193. userCode: this.settings.letUserCode,
  194. // socketCode: this.socketCode
  195. };
  196. if (this.websocket) {
  197. this.webSocketSend(JSON.stringify(params));
  198. }
  199. }, 10000);
  200. }
  201. // 停止socket连接
  202. stopWebSocket() {
  203. if (this.websocket) {
  204. this.websocket.close();
  205. }
  206. this.websocket = null;
  207. this.timeId && clearInterval(this.timeId);
  208. }
  209. //检查socket状态,通过oncheckSocket回调参数,可以判断本地有木有安装客户端
  210. checkSocket() {
  211. if (this.websocket != null) {
  212. let stateStr;
  213. // console.log(this.websocket.readyState);
  214. switch (this.websocket.readyState) {
  215. case 0:
  216. {
  217. stateStr = "CONNECTING";
  218. break;
  219. }
  220. case 1:
  221. {
  222. stateStr = "OPEN";
  223. break;
  224. }
  225. case 2:
  226. {
  227. stateStr = "CLOSING";
  228. break;
  229. }
  230. case 3:
  231. {
  232. stateStr = "CLOSED";
  233. break;
  234. }
  235. default:
  236. {
  237. stateStr = "UNKNOW";
  238. break;
  239. }
  240. }
  241. this.settings.oncheckSocket &&
  242. this.settings.oncheckSocket(stateStr === "OPEN");
  243. } else {
  244. //
  245. }
  246. }
  247. //获取本地客户端版本号
  248. startNotice() {
  249. const params = JSON.stringify({
  250. loginIp: this.settings.letLoginIp,
  251. method: "getClientVersion",
  252. userCode: this.settings.letUserCode,
  253. socketCode: this.socketCode
  254. });
  255. this.webSocketSend(params);
  256. }
  257. // 升级客户端方法
  258. setClientDownInfo() {
  259. const origin = location.origin;
  260. const params = JSON.stringify({
  261. loginIp: this.settings.letLoginIp,
  262. method: "setClientDownInfo",
  263. params: {
  264. newClientVersion: this.ClientLocalVersion, // 获取的本地版本号
  265. clientDownUrl: `${origin}/data/VSL/DSSEnterpriseClient/DSS_LightWeight_Client.zip` // 客户端在服务器上的地址,本地调试放开下面你的getCookie("ip")
  266. // "clientDownUrl": `http://${getCookie("ip")}:8314/TheNextWebApp/resources/DSS_LightWeight_Client.zip` // 客户端在服务器上的地址文件
  267. },
  268. userCode: this.settings.letUserCode,
  269. socketCode: this.socketCode
  270. });
  271. this.webSocketSend(params);
  272. }
  273. closeClient() {
  274. const params = {
  275. loginIp: this.settings.letLoginIp,
  276. method: "logout",
  277. userCode: this.settings.letUserCode,
  278. socketCode: this.socketCode
  279. };
  280. this.webSocketSend(JSON.stringify(params));
  281. this.stopWebSocket();
  282. this.websocket = null;
  283. }
  284. // 通用方法第三方传方法名和参数体
  285. commonUse(method, paramsObj) {
  286. const params = JSON.stringify({
  287. loginIp: this.settings.letLoginIp,
  288. method: method,
  289. userCode: this.settings.letUserCode,
  290. params: paramsObj,
  291. socketCode: this.socketCode
  292. });
  293. this.webSocketSend(params);
  294. }
  295. // 创建群聊界面
  296. openCreateGroupChatDlg() {
  297. const params = JSON.stringify({
  298. loginIp: this.settings.letLoginIp,
  299. method: "openCreateGroupChatDlg",
  300. userCode: this.settings.letUserCode,
  301. socketCode: this.socketCode
  302. });
  303. this.webSocketSend(params);
  304. }
  305. // 创建群聊会商-邀请一个用户进去会商
  306. createGroupChat(obj) {
  307. const params = JSON.stringify({
  308. loginIp: this.settings.letLoginIp,
  309. method: "createGroupChat",
  310. userCode: this.settings.letUserCode,
  311. socketCode: this.socketCode,
  312. params: {
  313. chatName: obj.chatName,
  314. bOpenDirect: obj.bOpenDirect, // 是否打开音视频通话标志,true 创建成功后打开音视频通话,false 创建成功后不打开音视频通话
  315. memberInfo: [
  316. // 成员信息
  317. {
  318. memberId: obj.memberId, // 成员id
  319. memberName: obj.memberName, // 成员名称
  320. regionId: obj.regionId, // 成员的pass域id
  321. memberType: obj.memberType, // 成员类型,Integer ,1 单兵;2 普通电话;3 对讲机;4 app|客户端;6 可是话机; 7 汇接话机(PSTN、手机号码等); 8 车载;9 无人机喊话云台; 10 无人机地面站对讲平台; 11 ipc; 12 NVR
  322. memberCode: obj.memberCode, // 成员设备编码
  323. memberNumber: obj.memberNumber // 成员号码
  324. }
  325. ]
  326. }
  327. });
  328. this.webSocketSend(params);
  329. }
  330. // 打开客户端指定模块
  331. openClientModule(moduleID) {
  332. const params = JSON.stringify({
  333. loginIp: this.settings.letLoginIp,
  334. method: "openClientModule",
  335. userCode: this.settings.letUserCode,
  336. params: {
  337. moduleID: moduleID // 模块ID,0 视频监控;1 录像回放;2 视频上墙;3 下载中心
  338. },
  339. socketCode: this.socketCode
  340. });
  341. this.webSocketSend(params);
  342. }
  343. }
  344. window.InitWebSocketClass = InitWebSocketClass;