initSocket: function () {
var that = this;
SocketTask.onOpen(res => {
socketOpen = true;
console.log('监听 WebSocket 连接打开事件。', res)
})
SocketTask.onClose(onClose => {
console.log('监听 WebSocket 连接关闭事件。', onClose)
socketOpen = false;
})
SocketTask.onError(onError => {
console.log('监听 WebSocket 错误。错误信息', onError)
socketOpen = false
})
SocketTask.onMessage(onMessage => {
console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', JSON.parse(onMessage.data))
var onMessage_data = JSON.parse(onMessage.data)
that.data.chatList.push(onMessage_data);
that.setData({
chatList: that.data.chatList
})
that.bottom()
})
},
webSocket: function () {
let that = this
var url = `wss://ws.dusun.com.cn/websocket/${this.data.shopId}|${this.data.mid}/${this.data.mid}`;
SocketTask = wx.connectSocket({
url: url,
header: {
'content-type': 'application/json'
},
method: 'post',
success: function (res) {
console.log('WebSocket连接创建', res,url)
},
fail: function (err) {
wx.showToast({
title: '网络异常!',
})
console.log(err)
},
})
that.initSocket()
},
已经解决了,url参数"/"也算中文字符。。。。。。。加了encodeURL就可以了