socket onSocketOpen 不触发回调

发布于 6 年前作者 min0810422 次浏览最后编辑 6 年前来自 ask

socket在开发者工具正常使用 在真机上无法触发 wx.onSocketOpen

服务器地址是wss的

wx.connectSocket({
    url: url,
    method: 'GET',
    success: function (res) {
        console.log('连接成功' + res)
        console.log(JSON.stringify(res))
    },
    fail: function (res) {
        console.log('连接失败' + res)
  }
})
   
wx.onSocketOpen(function (res) {
    console.log('WebSocket连接已打开!')
})
wx.onSocketError(function (res) {
    console.log('WebSocket连接打开失败,请检查!')
})
   
wx.onSocketMessage(function (res) {
    console.log('收到服务器内容:' + res.data)
}
9 回复
bwei
bwei1 楼6 年前

IOS才有问题,android没有问题

yaoxia
yaoxia2 楼6 年前

Error 的时候,会有 errMsg ,可以打出来看看。

另外麻烦确认下,下面这个截图是在什么版本中看到的。

guiying12
guiying123 楼6 年前

解决问题了,升级到TLS1.2可以了,
但是问题又来了,低版本的android链接又链接不上了。

andoird7没有问题,7一下好像都链接不上

cuilei
cuilei4 楼6 年前

和我的情况一样啊,andoird运行没有问题

ios,websocket有问题版本和我一样

kongmin
kongmin5 楼6 年前

wx.connectSocket({

     url: app.globalData.net_wss,

     data: {},

     method: ‘GET’,

     success: function (res) {

       console.log(“connectSocket 成功”);

     },

     fail: function (res) {

       console.log(“connectSocket 失败”)

     }

   });

 

   wx.onSocketOpen(function (res) {

     console.log(“socket打开”);

     wx.sendSocketMessage({

       success: function (res) {

       },

       fail: function (res) {

       }

     });

     wx.onSocketMessage(function (data) {

       console.log(data);

 

     }),

       wx.onSocketClose(function (data) {

         console.log(‘已经退出成功’);

       })

   })  

我这段代码android链接成功,运行没有任何问题

ios不成功,  console.log(“connectSocket 成功”)可以,但wx.onSocketOpen执行不了。

wwen
wwen6 楼6 年前

@Tiger 可以监听 onError 事件看看错误信息

heguiying
heguiying7 楼6 年前

connectSocket 的回调成功不代表连接成功,仅表示连接请求已经发出。

bzhou
bzhou8 楼6 年前

就连接成功之后就不触发 onSocketOpen 1分钟后就超时了微信版本 6.6.1

leiyan
leiyan9 楼4 年前

这是什么情况…