首先,微信小程序如果要使用WebSokect需要调用官方的API函数
demo:
- var app = getApp();
- const that = this;
- wx.connectSocket({
- url: 'wss://地址',
- header: { 'content-type': 'application/json' },
- success(res) {
- app.connecting = true;
- },
- })
- wx.onSocketOpen(function (res) {
- wx.sendSocketMessage({
- data: 入参
- })
- console.log('webSocket已链接')
- })
- wx.onSocketMessage(function (res) {
- console.log(res.data, '收到服务器内容')
- that.setData({ matchDesc: that.data.matchDesc + res.data })
- })
- console.log(this.data.matchDesc, 'matchDesc')