logging.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!DOCTYPE>
  2. <!--解决idea thymeleaf 表达式模板报红波浪线-->
  3. <!--suppress ALL -->
  4. <html xmlns:th="http://www.thymeleaf.org">
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>实时日志</title>
  8. <!-- 引入公用部分 -->
  9. <script th:replace="common/head::static"></script>
  10. </head>
  11. <body>
  12. <!-- 标题 -->
  13. <h1 style="text-align: center;">实时日志</h1>
  14. <h6 style="text-align: center;">1秒刷新一次</h6>
  15. <!-- 显示区 -->
  16. <div id="loggingText" contenteditable="true"
  17. style="width:100%;height: 500px;background-color: ghostwhite; overflow: auto;"></div>
  18. <!-- 操作栏 -->
  19. <div style="text-align: center;">
  20. <button onclick="$('#loggingText').text('')" style="color: green; height: 35px;">清屏</button>
  21. <button onclick="$('#loggingText').animate({scrollTop:$('#loggingText')[0].scrollHeight});"
  22. style="color: green; height: 35px;">滚动至底部
  23. </button>
  24. <button onclick="if(window.loggingAutoBottom){$(this).text('开启自动滚动');}else{$(this).text('关闭自动滚动');};window.loggingAutoBottom = !window.loggingAutoBottom"
  25. style="color: green; height: 35px; ">开启自动滚动
  26. </button>
  27. </div>
  28. </body>
  29. <script th:inline="javascript">
  30. let port = [[${port}]];//端口
  31. //websocket对象
  32. let websocket = null;
  33. //判断当前浏览器是否支持WebSocket
  34. if ('WebSocket' in window) {
  35. //动态获取域名或ip
  36. let hostname = window.location.hostname;
  37. port = window.location.port;
  38. websocket = new WebSocket("wss://"+hostname+":" + port + ctx + "/websocket/logging");
  39. console.log("wss://"+hostname+":" + port + ctx + "/websocket/logging");
  40. } else {
  41. console.error("不支持WebSocket");
  42. }
  43. //连接发生错误的回调方法
  44. websocket.onerror = function (e) {
  45. console.error("WebSocket连接发生错误");
  46. };
  47. //连接成功建立的回调方法
  48. websocket.onopen = function () {
  49. console.log("WebSocket连接成功")
  50. };
  51. //接收到消息的回调方法
  52. websocket.onmessage = function (event) {
  53. //追加
  54. if (event.data) {
  55. //日志内容
  56. let $loggingText = $("#loggingText");
  57. $loggingText.append(event.data);
  58. //是否开启自动底部
  59. if (window.loggingAutoBottom) {
  60. //滚动条自动到最底部
  61. $loggingText.scrollTop($loggingText[0].scrollHeight);
  62. }
  63. }
  64. }
  65. //连接关闭的回调方法
  66. websocket.onclose = function () {
  67. console.log("WebSocket连接关闭")
  68. };
  69. </script>
  70. </html>