//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() {}
});
},
睡眠与失眠 (作者)
小程序本身的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