Browse Source

首页添加 SSE, 但暂时不启用

reghao 2 năm trước cách đây
mục cha
commit
b6cd81dbcc
2 tập tin đã thay đổi với 27 bổ sung7 xóa
  1. 1 1
      src/api/map.js
  2. 26 6
      src/views/home/Index.vue

+ 1 - 1
src/api/map.js

@@ -16,7 +16,7 @@ export function getMarkerInfo(id) {
 }
 
 export function getMapMarkers(type) {
-  return get(mapAPI.markers + "?type=" + type)
+  return get(mapAPI.markers + '?type=' + type)
 }
 
 export function sendClickedLocation(loc) {

+ 26 - 6
src/views/home/Index.vue

@@ -74,6 +74,8 @@ export default {
   },
   created() {
     this.videoRecommendWrapper(this.nextId)
+    this.getHotVideoWrapper()
+    // this.initServerSendEvent()
   },
   mounted() {
     // 当窗口宽度改变时获取屏幕宽度
@@ -83,12 +85,6 @@ export default {
         this.screenWidth = window.screenWidth
       }
     }
-
-    getHotVideo().then(resp => {
-      if (resp.code === 0) {
-        this.carouselList = resp.data
-      }
-    })
   },
   methods: {
     videoRecommendWrapper(nextId) {
@@ -133,6 +129,30 @@ export default {
       setTimeout(() => {
         this.videoRecommendWrapper(this.nextId)
       }, 1000)
+    },
+    getHotVideoWrapper() {
+      getHotVideo().then(resp => {
+        if (resp.code === 0) {
+          this.carouselList = resp.data
+        }
+      })
+    },
+    initServerSendEvent() {
+      var list = this.carouselList
+      if (typeof (EventSource) !== 'undefined') {
+        const sseUrl = 'https://api.reghao.cn/api/data/video/hot'
+        const source = new EventSource(sseUrl)
+        source.addEventListener('test', function(e) {
+          console.log(e)
+        })
+        source.onmessage = function(event) {
+          console.log(event)
+          console.log(list)
+          // list.push(event.data)
+        }
+      } else {
+        console.log('抱歉,你的浏览器不支持 SSE...')
+      }
     }
   }
 }