1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
-
- 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() {})()
-
- console.log(1111111)
- };
- websocket.onmessage = function(evt) {
- var siteData = eval('(' + evt.data + ')');
- console.log('siteData')
- console.log(siteData)
-
-
-
- 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()
- }
- })
- }
-
-
-
-
-
- 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))
- })
|