uni-app websokert连接鸿蒙虚拟机一直断连,一直循环报错,怎么处理

发布于 1周前 作者 sinazl 来自 Uni-App

uni-app websokert连接鸿蒙虚拟机一直断连,一直循环报错,怎么处理

websocket_exec.cpp 614] lws callback reason is 8  
08-29 16:54:29.726   4402-4402    C03f00/ArkCompiler              [ecmascript] Trigger old gc here may cost long time, trigger full concurrent mark instead  
08-29 16:54:29.766   4402-6193    C015b0/NETSTACK               [websocket_exec.cpp 614] lws callback reason is 10  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               [gc] Has checked a long time gc  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                [gc]  [ HPP OldGC ] 109.617 (150) -> 99.3629 (142.25) MB, 610.361(+0.612)ms, Memory reach limit  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              [gc] IsInBackground: 0; SensitiveStatus: 0; OnStartupEvent: 0; BundleName: com.example.test.template;  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               [gc] /* GC Duration statistic: */  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              [gc] TotalGC:                 610.361 ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                Initialize:              0       ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            Mark:                    0.217   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                MarkRoots:               0.002   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              ConcurrentMark pause:    0.612   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               WaitConcurrentMarkFinish:527.905 ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            ProcessMarkStack:        0       ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               ReMark:                  0.216   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                ProcessSharedGCRSetWorkList:0       ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               Sweep:                   0.352   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                 ClearNativeObject:       30.503  ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                Evacuate:                36.061  ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                UpdateReference:         26.547  ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                 UpdateWeekRef:           26.183  ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              UpdateRoot:              0.219   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               ProceeWorkload:          0       ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               EvacuateSpace:           9.467   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            EvacuateRegion:          9.132   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              WaitFinish:              0.132   ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                Finish:                  14.947  ms  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               [gc] /* GC Memory statistic: */  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                [gc] AllSpaces        used:  101748KB     committed:  145664KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                 EdenSpace        used:       0KB     committed:       0KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                ActiveSemiSpace  used: 4381.93KB     committed:    5888KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              OldSpace         used:   73714KB     committed:   82944KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                HugeObjectSpace  used:   22528KB     committed:   22528KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler               NonMovableSpace  used: 1123.68KB     committed:   28928KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                MachineCodeSpace used:       0KB     committed:       0KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler              HugeMachineCodeSpace used:       0KB     committed:       0KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            SnapshotSpace    used:       0KB     committed:       0KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler                AppSpawnSpace    used: 4885.19KB     committed:    5120KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            [gc] Anno memory usage size:  151.5   MB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler             Native memory usage size:17.1623 MB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler             NativeBindingSize:       101.395 KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler        ArrayBufferNativeSize:   97.5938 KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            RegExpByteCodeNativeSize:3.58398 KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            ChunkNativeSize:         72344.8 KB  
08-29 16:54:30.482   4402-4402    C03f00/ArkCompiler            [gc] Heap alive rate:         websocket run service end  
08-29 16:54:34.767   4402-4402    A03d00/JSAPP                WebSocket连接打开失败! [object store/index.js:220  

更多关于uni-app websokert连接鸿蒙虚拟机一直断连,一直循环报错,怎么处理的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app websokert连接鸿蒙虚拟机一直断连,一直循环报错,怎么处理的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对uni-app中WebSocket连接鸿蒙虚拟机时出现的断连和循环报错问题,这里提供一个可能的解决方案,主要通过代码示例来展示如何处理WebSocket连接的状态管理和重连逻辑。

首先,确保你的uni-app项目中已经正确引入了WebSocket的相关库,并配置好了WebSocket的URL。接下来,通过代码来管理WebSocket的连接状态和处理重连逻辑。

// 在页面的data中定义WebSocket相关状态
data() {
    return {
        socket: null,
        reconnectInterval: 5000, // 重连间隔,单位毫秒
        reconnectAttempts: 0,   // 重连尝试次数
        maxReconnectAttempts: 10 // 最大重连尝试次数
    };
},

// 在页面的methods中定义WebSocket连接和重连的方法
methods: {
    // 初始化WebSocket连接
    initWebSocket() {
        this.socket = new WebSocket('ws://your-websocket-url');

        // WebSocket打开事件
        this.socket.onopen = () => {
            console.log('WebSocket连接成功');
            this.reconnectAttempts = 0; // 重置重连尝试次数
        };

        // WebSocket消息接收事件
        this.socket.onmessage = (event) => {
            console.log('收到消息:', event.data);
            // 处理接收到的消息
        };

        // WebSocket关闭事件
        this.socket.onclose = (event) => {
            console.log('WebSocket连接关闭:', event);
            this.reconnectWebSocket();
        };

        // WebSocket错误事件
        this.socket.onerror = (error) => {
            console.error('WebSocket错误:', error);
            this.reconnectWebSocket();
        };
    },

    // WebSocket重连方法
    reconnectWebSocket() {
        if (this.reconnectAttempts < this.maxReconnectAttempts) {
            setTimeout(() => {
                this.initWebSocket();
                this.reconnectAttempts++;
                console.log(`尝试第${this.reconnectAttempts}次重连`);
            }, this.reconnectInterval);
        } else {
            console.error('已达到最大重连尝试次数');
        }
    }
},

// 在页面的生命周期钩子中初始化WebSocket连接
onReady() {
    this.initWebSocket();
}

上述代码展示了如何在uni-app中管理WebSocket的连接状态,包括打开、接收消息、关闭和错误处理。同时,通过reconnectWebSocket方法实现了自动重连的逻辑,当WebSocket连接关闭或发生错误时,会尝试重新连接,直到达到最大重连尝试次数。

请注意,这里的WebSocket URL 'ws://your-websocket-url'需要替换为你实际的WebSocket服务器地址。此外,如果问题依然存在,建议检查鸿蒙虚拟机的网络配置,以及WebSocket服务器是否支持你的连接请求。

回到顶部