网络抖动,Ifame改造说明


Ifame

  1. 通过接口获取进入应用地址,赋值给iframe的src,并通过接口获取到taskId的值,用于后续重连,代码示例如下:
 
var config = {
    server: "http://192.168.0.55:8181/", // server
    webclient: "http://192.168.0.55:8181/webclient", // client
    testAppId: "925773094113509376",
   // testAppUrl: "http://192.168.0.55:8181/appli/start/?appliId=925773094113509376"
    taskId: ""
}
$("#enter").on("click", function (e) {
    if (!config.server) {
        alert("请设置 config.server");
        return;
    }
   enterApp(config.testAppId, null); 
})
function enterApp(appliId,taskId) {
    if(taskId != null){
        //重连分支
        $("#iframe").attr("src",  config.webclient + "?appliId=" + appliId + "&taskId=" + taskId)
    }else {
        //重新请求分支
        $.get(config.server + "getEnterAppliInfo?appliId=" + appliId, function (res) {
            console.log("enter appli res:", res, joinParam(res.result));
            if (res && res.code == 1000) {
                config.taskId = res.result.taskId
                $("#iframe").attr("src", config.webclient + "?" + joinParam(res.result));
            }
        })
    }
}
  1. 当出现异常时,捕获异常。并在设定的“延迟时间”内进行URL重连,例如设置延时时间为20s,若由于网络原因服务挂掉等其他特殊原因在20秒内依旧无法重连。那么超过"延迟时间"后,需要重启Task。代码示例如下:
 
// 与代理服务器连接关闭
//LK_PROXY_SERVER_CLOSE                            : 202,
// 与代理服务器连接出错
//LK_PROXY_SERVER_ERROR                            : 203,
// 与渲染服务器连接关闭
//LK_RENDER_SERVER_CLOSE                           : 102,
    var timerId, timeoutTimer;
        function onMessage(e) {
            if(e.data.type == lark.EventTypes.LK_PROXY_SERVER_CLOSE || 
            e.data.type == lark.EventTypes.LK_RENDER_SERVER_CLOSE){
            //关闭错误提示
            poster.setAlertEnable("false");
            //开启重连
                startReload()
            }
            else if(e.data.type == lark.EventTypes.LK_VIDEO_LOADED ){
                clearInterval(timerId);
                clearTimeout(timeoutTimer)
            }
        }
        // 开始计时执行重启
        function startReload() {
            doReload()
            // 设置时间间隔为5s
             timerId = setInterval(doReload, 5000)
            // 20秒后停止计时器
            timeoutTimer = setTimeout(() => {
                stopClock();
            }, 20000);
        }

        function doReload() {
            enterApp(config.testAppId, config.taskId)
        }

// 停止计时
        function stopClock() {
            clearInterval(timerId);
            clearTimeout(timeoutTimer)
            enterApp(config.testAppId, null)
        }

admin 2025年11月14日 14:25 收藏文档

开发者交流群

QQ群号:1011308692