var wsApi = "wss://域名/wss";
Page({
data: {},
//本地服务器地址
onLoad: function (options) {
let self = this
let st = wx.connectSocket({
url: wsApi,
header:{
'content-type': 'application/json'
},
})
st.onOpen(function(openRes){
var uid = 'uid2';
st.send(uid);
})
this.ws = st // 其他地方可以访问到socket
st.onMessage(function(msg){
console.log('msg: ', msg)
let data = JSON.parse(msg.data).data
if (!!data) {
self.setData({
msgList: [...self.data.msgList, data]
})
}
})
st.onClose()
},
});