login.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!-- <template>
  2. <div class="login">
  3. <h1></h1>
  4. </div>
  5. </template>
  6. <script setup>
  7. import { onMounted, onUnmounted } from 'vue';
  8. // import { EventSource } from 'eventsource'
  9. import EventSource from 'eventsource';
  10. // 创建 EventSource 实例,订阅事件流
  11. // const eventSource = new EventSource('http://192.168.126.42/gap/nodes');
  12. // const EventSource = require('eventsource');
  13. // var EventSource = {};
  14. // var myCustomVariable = {};
  15. const eventSource = new EventSource('http://192.168.1.201:9501/collector/zhipu');
  16. const express = require('express');
  17. const app = express();
  18. app.get('/events', (req, res) => {
  19. res.setHeader('Content-Type', 'text/event-stream');
  20. // 这里可以编写逻辑来持续推送事件数据,例如
  21. const interval = setInterval(() => {
  22. res.write('data: Some data here\n\n');
  23. }, 1000);
  24. req.on('close', () => {
  25. clearInterval(interval);
  26. res.end();
  27. });
  28. });
  29. app.listen(3000, () => {
  30. console.log('Server started on port 3000');
  31. });
  32. onUnmounted(() => {
  33. eventSource.close();
  34. });
  35. // 页面加载时
  36. onMounted(() => {
  37. // getTableData();
  38. // 创建 EventSource 实例,订阅事件流
  39. eventSource.onmessage = (event) => {
  40. console.log(event);
  41. // 处理收到的数据
  42. console.log(JSON.parse(event.data));
  43. };
  44. eventSource.onerror = (event) => {
  45. // 处理错误
  46. };
  47. });
  48. </script> -->
  49. <!--
  50. <template>
  51. <div>
  52. <div v-for="(message, index) in messages" :key="index">{{ message }}</div>
  53. </div>
  54. </template>
  55. <script>
  56. import { ref } from 'vue';
  57. import axios from 'axios';
  58. export default {
  59. setup() {
  60. const messages = ref([]);
  61. const eventSource = axios.create({
  62. baseURL: 'http://192.168.1.201:9501',
  63. headers: { 'Content-Type': 'text/event-stream' }
  64. });
  65. const setupEventSource = () => {
  66. const source = new EventSource(eventSource.getUri({ path: '/collector/zhipu' }));
  67. source.onmessage = (event) => {
  68. const data = JSON.parse(event.data);
  69. messages.value.push(data.message);
  70. };
  71. source.onerror = (error) => {
  72. console.error(error);
  73. };
  74. };
  75. setupEventSource();
  76. return { messages };
  77. }
  78. };
  79. </script> -->
  80. <!-- <template>
  81. <div>
  82. <button @click="startEventSource">开始接收事件</button>
  83. <button @click="stopEventSource">停止接收事件</button>
  84. <div v-for="event in events" :key="event.id">
  85. {{ event.data }}
  86. </div>
  87. </div>
  88. </template>
  89. <script>
  90. export default {
  91. name: 'EventSourceExample',
  92. data() {
  93. return {
  94. events: [],
  95. eventSource: null
  96. };
  97. },
  98. methods: {
  99. startEventSource() {
  100. if (!this.eventSource) {
  101. this.eventSource = new EventSource('http://192.168.1.201:9501/collector/zhipu');
  102. this.eventSource.onmessage = this.handleMessage;
  103. this.eventSource.onerror = this.handleError;
  104. }
  105. },
  106. stopEventSource() {
  107. if (this.eventSource) {
  108. this.eventSource.close();
  109. this.eventSource = null;
  110. }
  111. },
  112. handleMessage(event) {
  113. // 处理接收到的消息
  114. console.log('收到消息:', event.data);
  115. this.events.push({ id: this.events.length, data: event.data });
  116. },
  117. handleError(event) {
  118. // 处理错误
  119. console.error('EventSource 错误:', event);
  120. this.stopEventSource();
  121. }
  122. },
  123. beforeDestroy() {
  124. // 组件销毁前停止 EventSource
  125. this.stopEventSource();
  126. }
  127. };
  128. </script> -->
  129. <template>
  130. <div>
  131. <h2>事件流数据</h2>
  132. <ul>
  133. <li v-for="(item, index) in dataStream" :key="index">{{ item }}</li>
  134. </ul>
  135. <button @click="startStream">启动事件流</button>
  136. <button @click="stopStream">停止事件流</button>
  137. </div>
  138. </template>
  139. <script>
  140. export default {
  141. data() {
  142. return {
  143. dataStream: [], // 用于存储从事件流接收的数据
  144. eventSource: null, // 保存 EventSource 实例
  145. };
  146. },
  147. methods: {
  148. // 启动事件流
  149. startStream() {
  150. if (this.eventSource) {
  151. console.log("事件流已经启动!");
  152. return;
  153. }
  154. // 连接到 PHP SSE 后端接口
  155. // const apiUrl = "http://192.168.1.201:8989/";
  156. const apiUrl = "http://192.168.1.201:9501/stream";
  157. this.eventSource = new EventSource(apiUrl);
  158. // 监听普通消息
  159. this.eventSource.onmessage = (event) => {
  160. console.log(event);
  161. console.log("收到事件流数据:", event.data);
  162. this.dataStream.push(JSON.parse(event.data)); // 将数据存储到 dataStream 中
  163. };
  164. // 监听自定义事件(如关闭事件)
  165. this.eventSource.addEventListener("close", (event) => {
  166. console.log("事件流关闭:", event.data);
  167. this.stopStream(); // 停止事件流
  168. });
  169. // 监听错误事件
  170. this.eventSource.onerror = (error) => {
  171. console.error("事件流错误:", error);
  172. this.stopStream(); // 遇到错误时停止事件流
  173. };
  174. },
  175. // 停止事件流
  176. stopStream() {
  177. if (this.eventSource) {
  178. this.eventSource.close(); // 关闭事件流
  179. this.eventSource = null;
  180. console.log("事件流已停止");
  181. }
  182. },
  183. },
  184. beforeDestroy() {
  185. // 在组件销毁前关闭事件流
  186. this.stopStream();
  187. },
  188. };
  189. </script>
  190. <style scoped>
  191. h2 {
  192. color: #333;
  193. }
  194. button {
  195. margin: 5px;
  196. padding: 10px;
  197. background-color: #007bff;
  198. color: white;
  199. border: none;
  200. border-radius: 5px;
  201. cursor: pointer;
  202. }
  203. button:hover {
  204. background-color: #0056b3;
  205. }
  206. </style>