最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

sockjs前端WebSocket二次封裝示例詳解

 更新時間:2023年08月24日 11:17:44   作者:點墨  
這篇文章主要為大家介紹了sockjs前端WebSocket二次封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

業(yè)務需求

因業(yè)務需要,與后端進行websocket長連接通信,經(jīng)過研究,決定使用sockjs-client和stompjs庫,并進行了二次封裝。

package.json版本:

"sockjs-client": "^1.5.1",
"stompjs": "^2.3.3",

socketManager.js

import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
import lodash from 'lodash';
function subscribeCallBack(data, subscribes) {
    if (data) {
        let topic = data.headers.destination;
        let funces = subscribes.get(topic);
        funces.forEach((func) => {
            func(data);
        });
    }
}
let clientManager = {
    client: null,
    connecting: false,//是否正在連接
    subscribes: new Map(),//訂閱列表
    subscribe(topic, onMessage) {
        if (this.client != null && this.client.connected == true) {
            //已連接狀態(tài)
            console.log('增加訂閱 已連接狀態(tài)');
            if (!this.subscribes.has(topic)) {
                this.client.subscribe(topic, (data) => subscribeCallBack(data, this.subscribes));
                this.subscribes.set(topic, [onMessage]);
            } else {
                let funces = this.subscribes.get(topic);
                funces.push(onMessage);
            }
        } else {
            //未連接狀態(tài)
            console.log('增加訂閱 未連接狀態(tài)');
            if (!this.subscribes.has(topic)) {
                this.subscribes.set(topic, [onMessage]);
            } else {
                let funces = this.subscribes.get(topic);
                funces.push(onMessage);
            }
        }
    },
    subscribesAll() {
        console.log('訂閱全部');
        if (lodash.isEmpty(this.client) || this.client.connected != true) {
            return;
        }
        let subscribes = this.subscribes;
        for (let topic of subscribes.keys()) {
            this.client.subscribe(topic, (data) => subscribeCallBack(data, subscribes));
        }
    },
    disconnect() {
        console.log('斷開連接');
        if (lodash.isEmpty(this.client) || this.client.connected != true) {
            return;
        }
        this.client.disconnect();
        this.subscribes = new Map();
    },
    connect(onSuccess, onDisconnect) {
        try {
            if (this.connecting == true) {
                console.log('正在連接中');
                return;
            }
            this.connecting = true;
            if (lodash.isEmpty(this.client) || this.client.connected != true) {//未連接狀態(tài)
                let socket = new SockJS('/bond/notification', null, { timeout: 6000 });
                let stompClient = Stomp.over(socket);
                stompClient.debug = null;
                console.log('開始連接');
                stompClient.connect
                    ({},
                        () => {
                            this.client = stompClient;
                            console.log('連接成功');
                            this.subscribesAll();//連接成功后開始訂閱所有內(nèi)容
                            if (onSuccess != null && onSuccess != undefined) {
                                onSuccess();
                            };
                        },
                        (error) => this.errorCallBack(error, onSuccess, onDisconnect)
                    );
            } else if (this.client != null && this.client.connected == true) {//已連接狀態(tài)直接調(diào)用回調(diào)
                onSuccess();
            }
        }
        catch (err) {
            console.log('連接異常', err);
        }
        finally {
            this.connecting = false;
        }
    },
    errorCallBack(error, onSuccess, onDisconnect) {
        console.log('連接失敗');
        if (onDisconnect != null && onDisconnect != undefined) {
            onDisconnect();
        }
        setTimeout(() => {//自動重連
            console.log('重新連接中');
            this.connect(onSuccess, onDisconnect);
        }, 10000);
    },
};
export default clientManager;

連接方式

useEffect(()=>{
    socketmanager.connect();
    return () => {
         socketmanager.disconnect();
    };
})

訂閱方式

useEffect(() => {
        let topic = `/topic/notification`;
        socketmanager.subscribe(topic, (data) => {
            if (data) {
                //do something
            }
        })
    }, [])

如果有發(fā)現(xiàn)程序啟動的時候報這個錯誤:

可能是后端返回時contentType不對導致stream流寫入異常,修改后端后問題就可以解決。

以上就是sockjs前端WebSocket二次封裝示例詳解的詳細內(nèi)容,更多關于sockjs前端WebSocket二次封裝的資料請關注腳本之家其它相關文章!

相關文章

  • 微信小程序 實戰(zhàn)實例開發(fā)流程詳細介紹

    微信小程序 實戰(zhàn)實例開發(fā)流程詳細介紹

    這篇文章主要介紹了微信小程序 實戰(zhàn)實例開發(fā)流程詳細介紹的相關資料,這里主要介紹微信小程序的開發(fā)流程和簡單實例,需要的朋友可以參考下
    2017-01-01
  • TypeScript中extends的正確打開方式詳解

    TypeScript中extends的正確打開方式詳解

    這篇文章主要為大家介紹了TypeScript中extends的正確打開方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 精確到按鈕級別前端權限管理實現(xiàn)方案

    精確到按鈕級別前端權限管理實現(xiàn)方案

    這篇文章主要為大家介紹了精確到按鈕級別前端權限管理實現(xiàn)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 微信小程序教程之本地圖片上傳(leancloud)實例詳解

    微信小程序教程之本地圖片上傳(leancloud)實例詳解

    這篇文章主要介紹了微信小程序教程之本地圖片上傳(leancloud)實例詳解的相關資料,這里舉例說明該如何實現(xiàn)和實例代碼,文章一一表述,需要的朋友可以參考下
    2016-11-11
  • TypeScript枚舉類型

    TypeScript枚舉類型

    這篇文章主要介紹了TypeScript枚舉類型,所謂的枚舉類型就是為一組數(shù)值賦予名字,下面我們來看看文章是怎么介紹的吧,需要的小伙伴也可以參考一下,希望對你有所幫助
    2021-12-12
  • 微信小程序 Video API實例詳解

    微信小程序 Video API實例詳解

    這篇文章主要介紹了 微信小程序 Video API實例詳解,需要的朋友可以參考下
    2016-10-10
  • JavaScript設計模式之原型模式和適配器模式示例詳解

    JavaScript設計模式之原型模式和適配器模式示例詳解

    這篇文章主要為大家介紹了JavaScript 原型模式和適配器模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Project?Reference優(yōu)化TypeScript編譯性能示例

    Project?Reference優(yōu)化TypeScript編譯性能示例

    這篇文章主要為大家介紹了Project?Reference優(yōu)化TypeScript編譯性能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 微信小程序 教程之WXSS

    微信小程序 教程之WXSS

    這篇文章主要介紹了微信小程序 WXSS的相關資料,并附簡單實例代碼,需要的朋友可以參考下
    2016-10-10
  • 微信小程序返回多級頁面的實現(xiàn)方法

    微信小程序返回多級頁面的實現(xiàn)方法

    這篇文章主要介紹了微信小程序返回多級頁面的實現(xiàn)方法的相關資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10

最新評論

巢湖市| 阿鲁科尔沁旗| 镇远县| 房产| 吴川市| 安宁市| 康乐县| 靖远县| 板桥市| 六枝特区| 子长县| 张掖市| 开封县| 黄浦区| 宁南县| 绥阳县| 增城市| 鄱阳县| 沽源县| 堆龙德庆县| 旌德县| 丰宁| 宾阳县| 云浮市| 吴江市| 米泉市| 钦州市| 泰顺县| 改则县| 平和县| 伊吾县| 庐江县| 江山市| 扬州市| 东安县| 黔南| 齐河县| 盐津县| 洞头县| 海兴县| 长汀县|