swoole

// pages/chat/chat.js

Page({

/**

  • 页面的初始数据

  • /

    data: {

    data:[],

    records:[]

    },

    sendTime(time = 3000, status = true) {

    if (status == true) {

    let timer = setInterval(function () {

    console.log('心跳已连接');
    
    wx.sendSocketMessage({
    
      data: JSON.stringify({
    
        type: 'type'
    
      }),
    
      fail: () => {
    
        console.log('心跳连接失败');
    
        wx.closeSocket();
    
        wx.showToast({
    
          title: '关闭心跳连接',
    
          icon: 'none',
    
          duration: 1500,
    
        });
    
        clearInterval(timer)
    
      }
    
    });

    }, time)

    }

    },

    /**

  • 生命周期函数–监听页面加载

  • /

    onLoad: function (options) {

    this.sendTime();

    let that=this

    // 建立链接

    wx.connectSocket({

    url: ‘ws://124.222.228.174:9506’,

    success:(res)=>{

    // 监听连接数
    
    wx.onSocketOpen((result) => {
    
      let msg={
    
        'to':'miao',
    
        'my':'yuchen',
    
        'type':'open',
    
      }
    
      // 像服务器发送信息转为json
    
      that.send(JSON.stringify(msg));
    
      // 接收服务器返回值
    
      that.serverMsg();
    
    })

    }

    })

    },

//发送信息

send:function(res){

//调用向服务器发送信息函数

wx.sendSocketMessage({

  data : res

})

},

//获取输入框信息并发送

sendMyMsg:function(option){

let myMsg = option.detail.value.text;

console.log(myMsg);

let msg = {

  'to' : 'miao',    //发送给谁

  'my' : 'yuchen',    //我是谁

  'type' : 'send',    //当前状态

  'data' : myMsg    //发送信息

}

this.send(JSON.stringify(msg)); //调用发送方法,向服务器发送数据

let data = this.data.records; //将页面data内的数组records进行调用

//将用户名称及发送信息追加到数组中

data.push({

  user : msg.my,

  msg : myMsg

})

//进行数据传递

this.setData({

  records : data

})

},

//接收服务器返回消息

serverMsg:function(){

let that = this;

//调用接收函数,接收服务器返回数据

wx.onSocketMessage((result)=>{

  //因为在数据库中返回的数据为json数据格式,因此我们需要在前台进行转义

  let parseMsg = JSON.parse(result.data);

  console.log(parseMsg);

  let data = that.data.records;    //调用页面data内的数组records

  console.log(data);

  //接收到的数据追加到数组中

  data.push({

      user : parseMsg.user,

      msg : parseMsg.msg

  })

  //数据传递

  that.setData({

      records : data

  })

})

}

})

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6
自由与温暖是遥不可及的梦想

总感觉你的MD文档写的有问题

2年前 评论
坐忘 2年前
自由与温暖是遥不可及的梦想 (作者) 2年前
坐忘 2年前

回头自己都看不懂,你说他发这个干啥

2年前 评论

放弃吧少年,写swoole其实可以看看webman和go

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!