收藏
回答

微信小程序建立WebSocket后不触发onSocketOpen

建立连接后成功,进入成功回调,但是onSocketOpen和onSocketMessage都不打印,求解,代码如下:

onLoad: function (options) {

    wx.connectSocket({

        url: 'ws://xxx.xx.xxx:80xx/xxx/websocket/',

        data: {},

        header: {

            'content-type': 'application/json'

        },

        method: 'GET',

        success: function (socketRes) {

            console.log('connect success: ', socketRes);

        },

        fail: function (err) {

            console.log('connect error: ', err)

        }

    });

    

    wx.onSocketOpen(function (res) {

        console.log('WebSocket连接已打开!')

    })

    

    wx.onSocketMessage(function (res) {

        console.log('收到服务器内容:' + res.data)

    })

}


最后一次编辑于  2018-02-28  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏关注问题回答

5 个回答

  • Link
    Link
    2018-02-28

    请检查是否连接建立失败了,可以用 wx.onSocketError 监听错误事件

    2018-02-28
    赞同
    回复 1
    • 咖啡
      咖啡
      2018-06-12

      我也碰到这个问题,但是,我用wx.onSocketError监听却没有返回值,然后还是连接不上,怎么解决???

      2018-06-12
      赞同
      回复
  • 谢舜海
    谢舜海
    2018-03-03

    com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: Certificate expired at Sun Feb 18 07:59:59 GMT+08:00 2018 (compared to Sat Mar 03 09:49:20 GMT+08:00 2018)

    我也是出现这种情况

    2018-03-03
    赞同
    回复
  • 谢舜海
    谢舜海
    2018-03-03

    监听出来就是这个错误:

    com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: Certificate expired at Sun Feb 18 07:59:59 GMT+08:00 2018 (compared to Sat Mar 03 09:49:20 GMT+08:00 2018)

    2018-03-03
    赞同
    回复
  • 张孝银
    张孝银
    2018-03-04

    Socket连接失败,报错如下: java.security.cert.CertPathValidatorException:Trust anchor for certification path not found

    2018-03-04
    赞同
    回复