睡眠与失眠
睡眠与失眠
  • 发布:2020-06-03 18:19
  • 更新:2022-06-13 07:58
  • 阅读:3612

【报Bug】uni.onSocketMessage (小程序端) 接收不到消息 但是消息是发送成功了的

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10 18363.836

HBuilderX类型: 正式

HBuilderX版本号: 2.7.9

第三方开发者工具版本号: 1.03.2005141

基础库版本号: 2.11.1

项目创建方式: HBuilderX

操作步骤:

//index页面
async fnConnectSocket() {
let userInfo = util.getUserInfo();
if (!userInfo) return;
let [error, res] = await uni.connectSocket({
url: 'wss://brandkefu.plutotech.cn/wss',
data() {
return {
type: 'bind',
fromid: userInfo.id
};
},
header: {
// 'content-type': 'application/json'
},
protocols: '',
method: 'POST'
});
let _this = this;
if (res.errMsg == 'connectSocket:ok') {
console.log(res);
uni.onSocketOpen(function(res) {
console.log('Socket已打开');
getApp().globalData.socketOpen = true;
uni.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data);
// var data = JSON.parse(res.data);
// uni.$emit('newMessage', { message: data });
});
uni.onSocketClose(function(res) {
console.log('WebSocket 已关闭!');
});
});
} else {
getApp().globalData.socketOpen = false;
this.showToast('连接失败');
}

        uni.onSocketError(function(res) {  
            console.log('WebSocket连接打开失败,请检查!');  
            _this.showToast('WebSocket连接打开失败,请检查!');  
            getApp().globalData.socketOpen = false;  
        });  
    }  

// 发送消息 聊天页面
fnSendMessage(msg_type, msg_cont) {
let userInfo = util.getUserInfo();
var time = util.timestampToTime(Date.parse(new Date()));
var data = {
type: 'information',
fromid: userInfo.id,
fromName: userInfo.userName,
fromHead: userInfo.avatar,
toid: this.toId,
isread: 0,
time: time,
content: {
msg_type: msg_type,
msg_cont: this.content
}
};
var _this = this;
uni.sendSocketMessage({
data: JSON.stringify(data),
success(res) {
console.log(res)
_this.fnSaveMsg(JSON.stringify(data));
},
fail() {}
});
},

预期结果:

接收消息

实际结果:

没有回调

bug描述:

2020-06-03 18:19 负责人:DCloud_UNI_GSQ 分享
已邀请:
DCloud_UNI_GSQ

DCloud_UNI_GSQ

不使用uni-app框架,新建一个小程序项目测试,看问题是否存在,如果存在,报到微信小程序社区。

  • 睡眠与失眠 (作者)

    小程序本身的socket是没有问题的

    2020-06-04 11:43

  • 睡眠与失眠 (作者)

    测试过了

    2020-06-04 11:44

  • 睡眠与失眠 (作者)

    我们这个项目就是做了一个小程序端(小程序原生写的)的客服,专门接受这个商城小程序(uni-app框架写的)发来的消息,商城小程序可以发送,客服小程序也可以接收,但是客服小程序发来的消息,商城小程序无法监听到

    2020-06-04 11:49

  • 7***@qq.com

    回复 睡眠与失眠: 兄弟,你解决了么,我也是类似的问题

    2020-11-16 23:16

  • DCloud_UNI_GSQ

    回复 睡眠与失眠: 直接使用 小程序的接口:wx.xxx 试试

    2020-11-17 14:27

1***@qq.com

1***@qq.com -

我也遇到这种问题了,有解决吗

该问题目前已经被锁定, 无法添加新回复