initWebSocket.js 14 KB

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