import {stationwebsocketurl} from "../../../public/js/url.js"; import {signedSchoolArr,clockloginInfo,initSchoolSignedData} from "../js/index.js" let wsFlag = 60; let wsTimer = null; let stationWebsocketConnect = null; function clockStationWebsocket() { return new Promise((resolve, reject) => { if (stationWebsocketConnect == null) { stationWebsocketConnect = new WebSocket(stationwebsocketurl); stationWebsocketConnect.onopen = function (evt) { if (wsTimer != null) { clearInterval(wsTimer); wsTimer = null; wsFlag=60; } resolve({ "msg": "微型站websocket连接成功", "code": 0, "stationWebsocketConnect": stationWebsocketConnect }) var messageObj = { "msgcode": 100, "msgname": "session", "message": clockloginInfo["session"] } var msg = JSON.stringify(messageObj); stationWebsocketConnect.send(msg); setInterval(()=>{ let messageObj = { "msgcode": 99, "msgname": "hb", "message":"hb" } if(stationWebsocketConnect!=null){ stationWebsocketConnect.send(JSON.stringify(messageObj)); } },5000) }; stationWebsocketConnect.onclose = function (evt) { console.log("微站的websocket已经断开onclose") stationWebsocketConnect.close(); websocketReconnection(); }; stationWebsocketConnect.onerror = function (evt) { console.log("微站的websocket已经断开onerror") stationWebsocketConnect.close(); websocketReconnection(); } stationWebsocketConnect.onmessage = function (evt) { var data = JSON.parse(evt.data); var msgcode = data["msgcode"]; var message = data["message"]; switch (msgcode) { case 1038://打卡记录 updateWebsockeClockData(message); break; default: break; } } } }) } // setTimeout(()=>{ // var message=[{ // brigade:"长沙芙蓉区", // borough:"马王堆所", // detachment: "火炬警务区", // personId: "毛毛", // personName: "毛毛", // personType: "volunteer", // signId: "毛毛_49f165ab_20236811", // signTimestamp: 1686195503129, // station: "实验小学", // tagId: "04F96E125F6680", // targetId: "49f165ab", // taskId: "non", // }] // updateWebsockeClockData(message) // },6000) var timer=null; function updateWebsockeClockData(message){ for(var i=0;i{ // timer=null; // //刷新页面 // initSchoolSignedData(); // },10000) // } //刷新页面 initSchoolSignedData(); } function websocketReconnection(){ stationWebsocketConnect = null; wsTimer = null; if (wsTimer == null) { wsTimer = setInterval(function () { connecClockWebsocket(); wsFlag--; if(wsFlag==0){ layer.msg("网络中断,请重新连接..."); clearInterval(wsTimer); wsTimer = null; } }, 5000) } } function connectClockStationWebsocket(){ //连接站点websocket clockStationWebsocket().then((res)=>{ var code=res["code"]; if(code==0){ var stationWebsocketConnect=res["stationWebsocketConnect"]; createStationWebsocket(stationWebsocketConnect); } }) function createStationWebsocket(stationWebsocketConnect){ var stationTempArr=[]; signedSchoolArr.forEach((item)=>{ var name=item.name stationTempArr.push(name); if(stationTempArr.length>=500){ sendStationList(stationWebsocketConnect,stationTempArr); stationTempArr=[]; } }) sendStationList(stationWebsocketConnect,stationTempArr); } //发送微站的数据 function sendStationList(stationWebsocketConnect,stationTempArr){ var getIot={ "msgcode":102, "msgname":"station-sensor-list", "message":stationTempArr } var msg = JSON.stringify(getIot); stationWebsocketConnect.send(msg); } } export { connectClockStationWebsocket }