index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // 站点工况(websocket实时推送)
  2. (function() {
  3. var flag = false;
  4. var aa = false;
  5. var arrData = [];
  6. var wsUri = "wss://iot.usky.cn:55120";
  7. var websocket;
  8. function initWebSocket() {
  9. try {
  10. if (typeof MozWebSocket == 'function')
  11. WebSocket = MozWebSocket;
  12. websocket = new WebSocket(wsUri);
  13. websocket.onopen = function(evt) {
  14. var json = {};
  15. json.agentid = 'admin';
  16. console.log("Connected.");
  17. (function() {})()
  18. websocket.send(JSON.stringify(json));
  19. };
  20. websocket.onclose = function(evt) {
  21. console.log("DisConnected.");
  22. (function() {})()
  23. initWebSocket();
  24. };
  25. websocket.onmessage = function(evt) {
  26. var siteData = eval('(' + evt.data + ')');
  27. console.log('siteData')
  28. console.log(siteData)
  29. // console.log('arrData')
  30. // console.log(arrData)
  31. //返回值渲染
  32. if (siteData.time && aa) {
  33. $('.receive-time').text('[' + siteData.time + ' 收到]')
  34. $(".receive-request").text(JSON.stringify(siteData, null, 2));
  35. }
  36. };
  37. websocket.onerror = function(evt) {
  38. console.log("Error:", evt.data);
  39. (function() {})()
  40. };
  41. } catch (exception) {
  42. console.log("Exception:", exception);
  43. (function() {})()
  44. }
  45. }
  46. //打开websocket
  47. $('.openWebsocket').on('click', function() {
  48. initWebSocket();
  49. flag = true;
  50. })
  51. //站点工况点击
  52. $('.siteCondition').on('click', function() {
  53. aa = true;
  54. if (!flag) {
  55. alert('请先打开连接')
  56. return
  57. }
  58. var stationId = $('#stationId').val()
  59. var stationName = $('#stationName').val()
  60. var json = {};
  61. json.CMD = 'getStationInfo';
  62. json.StationID = stationId;
  63. json.StationName = stationName;
  64. //发送时间和请求参数页面渲染
  65. time('.send-time');
  66. $(".send-request").text(JSON.stringify(json, null, 2));
  67. websocket.send(JSON.stringify(json));
  68. })
  69. })()