warning.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //使用 WebSocket 的網址向 Server 開啟連結
  2. //let ws = new WebSocket('ws://180.218.150.185:3030')
  3. let ws = new WebSocket('ws://125.227.151.69:3030')
  4. var client_id;
  5. //開啟後執行的動作,指定一個 function 會在連結 WebSocket 後執行
  6. ws.onopen = () => {
  7. console.log('open connection')
  8. }
  9. //關閉後執行的動作,指定一個 function 會在連結中斷後執行
  10. ws.onclose = () => {
  11. console.log('close connection')
  12. }
  13. //接收 Server 發送的訊息
  14. ws.onmessage = event => {
  15. var table;
  16. var x;
  17. if (event.data.includes("client")) {
  18. client_id = event.data.replace("client:","");
  19. console.log("this client id: " + client_id);
  20. } else {
  21. const user = JSON.parse(event.data);
  22. if(user != ""){
  23. console.log("user[0]:"+user[0]);
  24. if (user[0].hasOwnProperty('aid_id')) {
  25. console.log("warning: " + event.data);
  26. document.getElementById("warning-count").innerHTML = user.length;
  27. document.getElementById("warning-count2").innerHTML = user.length;
  28. document.getElementById("warning-none").innerHTML = "";
  29. document.getElementById("warning-style").style.display = "block";
  30. //$("#warning").click();
  31. if(document.URL == 'http://125.227.151.69:8800/C3Octopus/octopus/warning.php') {
  32. table = document.getElementById("warningTable");
  33. x = table.rows.length;
  34. for (var i = 0; i < x - 1; i++) {
  35. table.deleteRow(1);
  36. }
  37. for (var i = 0; i < user.length; i++) {
  38. var test = user[i].aid_id;
  39. var test2 = user[i].beacon_id;
  40. var test3 = user[i].created_at;
  41. var row = table.insertRow(-1);
  42. var cell1 = row.insertCell(0);
  43. var cell2 = row.insertCell(1);
  44. var cell3 = row.insertCell(2);
  45. cell1.innerHTML = test;
  46. cell2.innerHTML = test2;
  47. cell3.innerHTML = test3;
  48. }
  49. $("#click-to-close-error").click();
  50. }
  51. } else if(user[0].hasOwnProperty('status')){
  52. $("#warning").click();
  53. }else{
  54. console.log("error");
  55. }
  56. }else{
  57. document.getElementById("warning-count").innerHTML = 0;
  58. document.getElementById("warning-count2").innerHTML = 0;
  59. document.getElementById("warning-none").innerHTML = "No warning";
  60. document.getElementById("warning-style").style.display = "none";
  61. //$("#clear").click();
  62. if(document.URL == 'http://125.227.151.69:8800/C3Octopus/octopus/warning.php') {
  63. table = document.getElementById("warningTable");
  64. x = table.rows.length;
  65. for (var i = 0; i < x - 1; i++) {
  66. table.deleteRow(1);
  67. }
  68. $(".click-2-close").click();
  69. $("#click-to-close-success").click();
  70. }
  71. }
  72. }
  73. }