国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

Uniapp斷網(wǎng)重連

oop86868j8j

問(wèn)題描述

大佬們能分享個(gè)穩(wěn)定的Uniapp斷網(wǎng)重連的代碼嗎?

2050 2 1
2個(gè)回答

augushong

這是我之前寫(xiě)的聊天寫(xiě)過(guò)的。
其實(shí)很簡(jiǎn)單,監(jiān)聽(tīng)一下事件就行。
其實(shí)麻煩的還是onMessage的業(yè)務(wù)監(jiān)聽(tīng)

import tools from "@/assets/js/tools.js";
import store from '@/util/store';
let socketResource = null;
let socketTimer = null;
let sound = null;
const socket = {
    currentChat: null,

    init(options) {

        if (socketResource != null) {
            return true;
        }

        console.log(options)
        if (typeof options == 'undefined') {
            options = {};
        }

        if (options.onSuccess == undefined) {
            options.onSuccess = function() {}
        }

        console.log(options)

        console.log('長(zhǎng)鏈接初始化')

        socketResource = uni.connectSocket({
            url: tools.wssHost, //僅為示例,并非真實(shí)接口地址。
            success: (res) => {
                socketTimer = setInterval(() => {
                    this.send({
                        type: "ping",
                    });
                }, 10000);
                console.log('調(diào)用onSuccess回調(diào)')

                setTimeout(() => {
                    options.onSuccess()
                }, 300)
            },
            fail(e) {
                console.log('建立連接失敗', e);
                setTimeout(() => {
                    socket.restartInit()
                }, 1200)
            }
        });

        socketResource.onMessage((revice) => {

        })

        socketResource.onError((e) => {
            console.log('長(zhǎng)鏈接錯(cuò)誤:', e)

            socketResource.close()
            socket.restartInit()

        })

        socketResource.onClose(() => {
            socket.restartInit()
        })
    },

    restartInit() {

        socketResource = null;
        clearInterval(socketTimer)
        socketTimer = null;
        setTimeout(() => {
            console.log('關(guān)閉長(zhǎng)鏈接并重新初始化')
            socket.init({
                onSuccess: () => {
                    console.log('執(zhí)行重啟回調(diào)')
                    socket.login()
                }
            });
        }, 300)
    },

    login() {
        this.send({
            type: "login",
        });
    },

    send(item) {

    },

}

export default socket;
chaz6chez

webman/push
workbunny/webman-push-server
這兩個(gè)插件中都有uniapp的樣例

  • 暫無(wú)評(píng)論
年代過(guò)于久遠(yuǎn),無(wú)法發(fā)表回答
??