微信小程序websoket
websocket(type){
if(type=='start'){
let url= __wxConfig.envVersion=='develop'?`ws://192.168.1.120:8080/webSocket/${this.data.orderInfo.num}`:`wss://${app.globalData.websocketUrl}/webSocket/${this.data.orderInfo.num}`
let socket = wx.connectSocket({
url:url,
success(res){
console.log(res)
},
fail(reg){
console.log(reg)
}
})
this.setData({
socket:socket
})
socket.onOpen(res=>{//打开链接
console.log(res)
socket.send({//发送消息
data:'打开链接',
success(resa){
console.log(resa)
},
fail(regs){
console.log(regs)
}
})
})
socket.onMessage(res=>{//接受消息
console.log(JSON.parse(res.data))
this.getOrder()
})
}else{
this.data.socket.close({
success(res){
console.log(res)
},
fail(reg){
console.log(reg)
}
})
}
},
销毁onUnload: function () {
this.websocket('end')
},