收藏
回答

WebSocket在同一个页面初始化两次的问题

问题描述:通过开发工具扫码进入,demo代码

Page({
    data:{
 
    },
    onLoad:function(options){
        console.info('ok');
        var _ = options._;
        console.info(_);
        wx.onSocketOpen(function (res) {
            wx.sendSocketMessage({
                data:JSON.stringify({
                    data:1
                })
            })
            console.log('打开:' + res);
        });
        wx.onSocketMessage(function (res) {
            console.log('收到服务器内容:' + res.data);
        });
        wx.onSocketClose(function () {
            console.log('WebSocket 已关闭!')
        });
        wx.connectSocket({
            url: `wss://seeknow.org/wss`
        })
 
        _ && wx.sendSocketMessage({
            data:JSON.stringify({
                data:2222
            })
        })
    },
    onReady:function(){
 
    },
    onShow:function(){
 
    },
    onHide:function(){
 
    },
    onUnload:function(){
 
    },
    onPullDownRefresh:function(){
 
    },
    onReachBottom:function(){
 
    },
    onShareAppMessage:function(){
        return {
            title: `邀请您一起参与专注挑战`,
            desc: '让我们一起远离手机 专注当下',
            path: `/pages/test/test?_=${Date.now()}`
        }
    }
})

进入后成功的接收到了服务的反馈,但是通过分享链接再次进入,socket没有断,服务器也能接收到客户端的消息,但是客户端没能接收到信息。

结论:onSocketMessage 方法有bug

回答关注问题邀请回答
收藏
登录 后发表内容