// 站点工况(websocket实时推送) var arrData = []; var wsUri = "wss://iot.usky.cn:55120"; var websocket; function initWebSocket() { return new Promise((res, rej) => { try { if (typeof MozWebSocket == 'function') WebSocket = MozWebSocket; websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { var json = {}; json.agentid = 'admin'; console.log("Connected."); (function() {})() console.log('websocket') console.log(websocket) websocket.send(JSON.stringify(json)); }; websocket.onclose = function(evt) { console.log("DisConnected."); (function() {})() // initWebSocket(); console.log(1111111) }; websocket.onmessage = function(evt) { var siteData = eval('(' + evt.data + ')'); console.log('siteData') console.log(siteData) // console.log('arrData') // console.log(arrData) //返回值渲染 if (siteData.time) { $('.receive-time').text('[' + siteData.time + ' 收到]') $(".receive-request").text(JSON.stringify(siteData, null, 2)); } }; websocket.onerror = function(evt) { console.log("Error:", evt.data); (function() {})() }; res() } catch (exception) { console.log("Exception:", exception); rej() } }) } //打开websocket // $('.openWebsocket').on('click', function() { // initWebSocket(); // }) // initWebSocket() function isWs(val) { if (!websocket) { initWebSocket().then(() => { isWs(val) }) } else if (websocket.readyState == 1) { websocket.send(val) } else { setTimeout(() => { isWs(val) }, 1000) } } //站点工况点击 $('.siteCondition').on('click', function() { var stationId = $('#stationId').val() var stationName = $('#stationName').val() var json = {}; json.CMD = 'getStationInfo'; json.StationID = stationId; json.StationName = stationName; //发送时间和请求参数页面渲染 time('.send-time'); $(".send-request").text(JSON.stringify(json, null, 2)); if (websocket) { websocket.close(); websocket = ''; } isWs(JSON.stringify(json)) })