l***@sina.com
l***@sina.com
  • 发布:2025-03-15 16:52
  • 更新:2025-11-19 17:00
  • 阅读:345

客户端调用uniCloud.connectWebSocket,云对象未调用_onWebsocketConnection方法

分类:uniCloud
const exampleWebSocket = await uniCloud.connectWebSocket({  
                    name: "wsfunc",  
                    query: {  
                        key: "val"  
                    }  
                })  

                // 连接成功时触发  
                exampleWebSocket.onOpen(event => {  
                    console.log("WebSocket:open", event);  
                });

WebSocket:open打印了 但是云对象没有回调响应链接成功的方法

_onWebsocketConnection: async function(event) {  
        console.log("onWebsocketConnection", event);  
    }

_onWebsocketConnection未被调用

2025-03-15 16:52 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

客户端调用有报错吗?触发onopen回调了吗?

  • l***@sina.com (作者)

    客户端onopen回调了,但是云对象没有触发_onWebsocketConnection回调

    2025-04-07 10:18

  • DCloud_uniCloud_CRL

    回复 l***@sina.com: 现在还能复现吗?

    2025-04-07 14:47

SeanDavis

SeanDavis

云对象中, 使用 async 标记 _onWebsocketConnection 函数后就不会触发
去掉 async 后就正常出发了,
好迷惑的 bug,是因为支付宝云前置的 socket 网关么

l***@sina.com

l***@sina.com (作者)

uni-app-x代码:

methods: {  
            async onInitWS() {  
                const exampleWebSocket = await uniCloud.connectWebSocket({  
                    name: "wsfunc",  
                    query: {  
                        key: "123"  
                    }  
                })  
                // 连接成功时触发  
                exampleWebSocket.onOpen(event => {  
                    console.log("WebSocket:open", event)  
                })  

                // 收到数据时触发  
                exampleWebSocket.onMessage(event => {  
                    console.log("WebSocket:message", event.data)  
                })  

                // 连接被关闭时触发  
                exampleWebSocket.onClose(event => {  
                    console.log("WebSocket:close", event)  
                })  

                // 连接因错误而关闭时触发  
                exampleWebSocket.onError(event => {  
                    console.log("WebSocket:error", event)  
                })  
            }  
        }

云对象代码:

module.exports = {  
    _before: function () { // 通用预处理器  

    },  
    _onWebsocketConnection: function(event) {  
        console.log("onWebsocketConnection", event)  
    },  

    _onWebsocketMessage: function(event) {  
        console.log("onWebsocketMessage", event)  
    },  
    _onWebsocketDisConnection: function(event) {  
        console.log("onWebsocketDisConnection", event)  
    },  
    _onWebsocketError: function(event) {  
        console.log("onWebsocketError", event)  
    }  
}

输出结果:
16:52:38.426 WebSocket:open { ⁠...⁠ } at pages\index\index.uvue:33
16:52:38.426 WebSocket:close {code: 4000, reason: "call function failed"} at pages\index\index.uvue:43

  • DCloud_uniCloud_CRL

    在uniCloud控制台看下云函数【wsfunc】的日志。有没有详细的错误信息

    2025-11-20 11:38

  • l***@sina.com (作者)

    回复 DCloud_uniCloud_CRL: 莫名其妙的就好了,切换后台也不掉线(华为云切到后台2分钟就掉线),不过要配合云数据库存放连接的用户id(不知道有没有其它方案获取其他人连接后的响应回调),不过对应目前项目是够用了,真香!!!

    2025-11-24 16:58

要回复问题请先登录注册