initWebSocket.js 13 KB

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