springboot服務(wù)端怎么推送SSE

springboot服務(wù)端怎么推送SSE,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

站在用戶的角度思考問題,與客戶深入溝通,找到郊區(qū)網(wǎng)站設(shè)計(jì)與郊區(qū)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋郊區(qū)地區(qū)。

package com.demo.action;

import com.demo.serviceI.DemoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class DemoAction {

    @Autowired
    private DemoService demoService;

    /**
     * 健康檢查json串模擬
     * @return
     */
    @RequestMapping(value = "health.json")
    public String healt(){

        return "{\"status\":\"UP\",\"diskSpace\":{\"status\":\"UP\",\"total\":249769230336,\"free\":71914618880,\"threshold\":10485760},\"db\":{\"status\":\"UP\",\"database\":\"MySQL\",\"hello\":1}}";
    }

    /**
     * 條件注解使用
     * @return
     */
    @RequestMapping(value = "user/info")
    public String info(){

        return demoService.info();
    }

    /**
     * 異步調(diào)用方法
     */
    @RequestMapping(value = "print")
    public void print(){
        for (int i = 0; i < 100; i++) {
            demoService.print(i);
        }
    }

    /**
     * 服務(wù)端推送技術(shù)
     */
    @RequestMapping(value = "serverPush",produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public String serverPush(){

        Map<String,String> demo = new HashMap<>(1);
        demo.put("name","張三"+new Random().nextInt());
        ObjectMapper objectMapper = new ObjectMapper();
        String s = "data:";
        try {
            s += objectMapper.writeValueAsString(demo)+"\n\n";
            System.out.println(s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }


}

頁面代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="application/javascript">
        if (!!window.EventSource) {
            var event = new EventSource("/serverPush");

            event.addEventListener('message', function(t) {
                var data = t.data;
                document.write(data+"<br/>")
            });

            event.addEventListener("open",function (t) {
                console.log("開啟");
            },false);

            event.addEventListener("error",function (t) {
                if (t.readyState  == EventSource.CLOSED) {
                    console.log("關(guān)閉");
                }
            },false);



        }else{
            console.error("瀏覽器不支持");
        }


    </script>
</head>
<body>

</body>
</html>

注意使用過程中容易遇到的問題:

1.由于返回類型使用了text/event-stream,所以在服務(wù)端響應(yīng)數(shù)據(jù)必須使用String或其他文本類型

2. 在返回?cái)?shù)據(jù)時(shí),必須用data:和\n\n分別開頭和結(jié)尾;如:String.format("data:%s\n\n",data),這里將我坑慘,網(wǎng)上和書上資料只是給了個(gè)例子沒有具體說明,找了半天沒找到原因和前端鏈接成功就是觸發(fā)不了message事件

springboot服務(wù)端怎么推送SSE

關(guān)于springboot服務(wù)端怎么推送SSE問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)頁名稱:springboot服務(wù)端怎么推送SSE
標(biāo)題路徑:http://bm7419.com/article30/gejdso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、全網(wǎng)營銷推廣關(guān)鍵詞優(yōu)化、云服務(wù)器、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)