试了一下小程序的websocket,提供个简单的代码吧
[代码]var[代码] [代码]util = require([代码][代码]'../../../utils/util.js'[代码][代码]);[代码] [代码]var[代码] [代码]socketOpen = [代码][代码]false[代码][代码];[代码] [代码]var[代码] [代码]frameBuffer_Data, session, SocketTask;[代码] [代码]var[代码] [代码]url = [代码][代码]'wss://wss......./Chat'[代码][代码];[代码] [代码]Page({[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 页面的初始数据[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]data: {[代码] [代码] [代码][代码]toView: [代码][代码]'green'[代码][代码],[代码] [代码] [代码][代码]windowH: [代码][代码]"1000"[代码][代码],[代码] [代码] [代码][代码]user_input_text: [代码][代码]''[代码][代码], [代码][代码]//用户输入文字[代码] [代码] [代码][代码]inputValue: [代码][代码]''[代码][代码],[代码] [代码] [代码][代码]returnValue: [代码][代码]''[代码][代码],[代码] [代码] [代码][代码]addImg: [代码][代码]false[代码][代码],[代码] [代码] [代码][代码]//格式示例数据,可为空[代码] [代码] [代码][代码]allContentList: [],[代码] [代码] [代码][代码]num: 0,[代码] [代码] [代码][代码]wo: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]ta: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]youImg: [代码][代码]""[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]scrollToTop() {[代码] [代码] [代码][代码]const that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]this[代码][代码].setAction({[代码] [代码] [代码][代码]scrollTop: that.windowH[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]bindKeyInput: [代码][代码]function[代码][代码](e) {[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]inputValue: e.detail.value[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]// 提交文字[代码] [代码] [代码][代码]submitTo: [代码][代码]function[代码][代码](e) {[代码] [代码] [代码][代码]let that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]var[代码] [代码]content = that.data.inputValue;[代码] [代码] [代码][代码]var[代码] [代码]data = {[代码] [代码] [代码][代码]// body: that.data.inputValue,[代码] [代码] [代码][代码]"name"[代码][代码]: that.data.wo,[代码] [代码] [代码][代码]"content"[代码][代码]: content,[代码] [代码] [代码][代码]"type"[代码][代码]: 5,[代码] [代码] [代码][代码]"toName"[代码][代码]: that.data.ta[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]//静态显示[代码] [代码] [代码][代码]this[代码][代码].data.allContentList.push({[代码] [代码] [代码][代码]"id"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"hx_id"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"wo"[代码][代码]: that.data.wo,[代码] [代码] [代码][代码]"ta"[代码][代码]: that.data.ta,[代码] [代码] [代码][代码]"content"[代码][代码]: content,[代码] [代码] [代码][代码]"voice_url"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"fileurl"[代码][代码]: [代码][代码]null[代码][代码],[代码] [代码] [代码][代码]"create_date"[代码][代码]: util.formatTime[代码] [代码] [代码][代码]});[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]allContentList: [代码][代码]this[代码][代码].data.allContentList,[代码] [代码] [代码][代码]inputValue: [代码][代码]''[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]//至底[代码] [代码] [代码][代码]that.bottom();[代码] [代码] [代码][代码]//socket[代码] [代码] [代码][代码]if[代码] [代码](socketOpen) {[代码] [代码] [代码][代码]// 如果打开了socket就发送数据给服务器[代码] [代码] [代码][代码]that.sendSocketMessage(data);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]//通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。[代码] [代码] [代码][代码]sendSocketMessage: [代码][代码]function[代码][代码](msg) {[代码] [代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]console.log([代码][代码]'通过 WebSocket 连接发送数据'[代码][代码], JSON.stringify(msg))[代码] [代码] [代码][代码]// debugger[代码] [代码] [代码][代码]SocketTask.send([代码] [代码] [代码][代码]{[代码] [代码] [代码][代码]data: JSON.stringify(msg)[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]function[代码] [代码](res){[代码] [代码] [代码][代码]console.log([代码][代码]'已发送'[代码][代码], res)[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码])[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]addImg: [代码][代码]function[代码][代码](e) {[代码] [代码] [代码][代码]var[代码] [代码]a = [代码][代码]this[代码][代码].data.addImg;[代码] [代码] [代码][代码]if[代码] [代码](a == [代码][代码]true[代码][代码]) {[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]addImg: [代码][代码]false[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]addImg: [代码][代码]true[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]tap() {[代码] [代码] [代码][代码]for[代码] [代码](let i = 0; i < order.length; ++i) {[代码] [代码] [代码][代码]if[代码] [代码](order[i] === [代码][代码]this[代码][代码].data.toView) {[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]toView: order[i + 1],[代码] [代码] [代码][代码]scrollTop: (i + 1) * 200[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]break[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]tapMove() {[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]scrollTop: [代码][代码]this[代码][代码].data.scrollTop + 10[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 生命周期函数--监听页面加载[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onLoad: [代码][代码]function[代码][代码](options) {[代码] [代码] [代码][代码]const that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]that.setData({[代码] [代码] [代码][代码]ta: options.tel,[代码] [代码] [代码][代码]youImg: options.youImg,[代码] [代码] [代码][代码]wo: util.phone[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]// 获取设备高度[代码] [代码] [代码][代码]wx.getSystemInfo({[代码] [代码] [代码][代码]success: [代码][代码]function[代码][代码](res) {[代码] [代码] [代码][代码]var[代码] [代码]clientHeight = res.windowHeight,[代码] [代码] [代码][代码]clientWidth = res.windowWidth,[代码] [代码] [代码][代码]rpxR = 750 / clientWidth;[代码] [代码] [代码][代码]var[代码] [代码]helfH = clientHeight * 1 * rpxR;[代码] [代码] [代码][代码]//var textH = helfH - 100;[代码] [代码] [代码][代码]that.setData({[代码] [代码] [代码][代码]windowH: helfH[代码] [代码] [代码][代码]});[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]});[代码] [代码] [代码][代码]//加载历史数据[代码] [代码] [代码][代码]// util.apiHtml +http://localhost:9193[代码] [代码] [代码] [代码] [代码][代码]//修改已读[代码] [代码] [代码] [代码] [代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 生命周期函数--监听页面初次渲染完成[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onReady: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码] [代码] [代码] [代码] [代码][代码]SocketTask.onOpen(res => {[代码] [代码] [代码][代码]socketOpen = [代码][代码]true[代码][代码];[代码] [代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 连接打开事件。'[代码][代码], res)[代码] [代码] [代码][代码]//发送登陆信息[代码] [代码] [代码][代码]var[代码] [代码]data = {[代码] [代码] [代码][代码]// body: that.data.inputValue,[代码] [代码] [代码][代码]"Name"[代码][代码]: that.data.wo,[代码] [代码] [代码][代码]"content"[代码][代码]: [代码][代码]"login"[代码][代码],[代码] [代码] [代码][代码]"type"[代码][代码]: 4[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]that.sendSocketMessage(data);[代码] [代码] [代码][代码]//循环发送心跳[代码] [代码] [代码][代码]setInterval([代码] [代码] [代码][代码]function[代码] [代码]() {[代码] [代码] [代码][代码]var[代码] [代码]ping = { [代码][代码]"type"[代码][代码]: [代码][代码]"ping"[代码] [代码]};[代码] [代码] [代码][代码]that.sendSocketMessage(ping);[代码] [代码] [代码][代码]}, 20000[代码] [代码] [代码][代码]); [代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]SocketTask.onClose(onClose => {[代码] [代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 连接关闭事件。'[代码][代码], onClose)[代码] [代码] [代码][代码]socketOpen = [代码][代码]false[代码][代码];[代码] [代码] [代码][代码]this[代码][代码].webSocket()[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]SocketTask.onError(onError => {[代码] [代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 错误。错误信息'[代码][代码], onError)[代码] [代码] [代码][代码]socketOpen = [代码][代码]false[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]SocketTask.onMessage(onMessage => {[代码] [代码] [代码][代码]console.log([代码][代码]"onMessage:::::"[代码][代码]+onMessage.data);[代码] [代码] [代码][代码]if[代码] [代码](onMessage.data.indexOf([代码][代码]"上线"[代码][代码]) != -1 || onMessage.data.indexOf([代码][代码]"下线"[代码][代码]) != -1) {[代码] [代码] [代码][代码]return[代码][代码];[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]console.log([代码][代码]'监听WebSocket接受到服务器的消息事件。服务器返回的消息'[代码][代码], JSON.parse(onMessage.data))[代码] [代码] [代码][代码]var[代码] [代码]onMessage_data = JSON.parse(onMessage.data)[代码] [代码] [代码][代码]if[代码] [代码](onMessage_data.toName == that.data.wo && onMessage_data.name == that.data.ta) {[代码] [代码] [代码][代码]// addmsglist1(msg1.name, msg1.content)[代码] [代码] [代码][代码]that.data.allContentList.push({[代码] [代码] [代码][代码]"id"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"hx_id"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"wo"[代码][代码]: that.data.ta,[代码] [代码] [代码][代码]"ta"[代码][代码]: that.data.wo,[代码] [代码] [代码][代码]"content"[代码][代码]: onMessage_data.content,[代码] [代码] [代码][代码]"voice_url"[代码][代码]: [代码][代码]""[代码][代码],[代码] [代码] [代码][代码]"fileurl"[代码][代码]: [代码][代码]null[代码][代码],[代码] [代码] [代码][代码]"create_date"[代码][代码]: util.formatTime[代码] [代码] [代码][代码]});[代码] [代码] [代码][代码]that.setData({[代码] [代码] [代码][代码]allContentList: that.data.allContentList[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]that.bottom();[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]// 获取hei的id节点然后屏幕焦点调转到这个节点 [代码] [代码] [代码][代码]bottom: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]this[代码][代码].setData({[代码] [代码] [代码][代码]scrollTop: 1000000[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 生命周期函数--监听页面显示[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onShow: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]if[代码] [代码](!socketOpen) {[代码] [代码] [代码][代码]this[代码][代码].webSocket()[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]webSocket: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]const that = [代码][代码]this[代码][代码];[代码] [代码] [代码][代码]// 创建Socket[代码] [代码] [代码][代码]SocketTask = wx.connectSocket({[代码] [代码] [代码][代码]url: url,[代码] [代码] [代码][代码]data: [代码][代码]'data'[代码][代码],[代码] [代码] [代码][代码]header: {[代码] [代码] [代码][代码]'content-type'[代码][代码]: [代码][代码]'application/json'[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]method: [代码][代码]'post'[代码][代码],[代码] [代码] [代码][代码]success: [代码][代码]function[代码][代码](res) {[代码] [代码] [代码][代码]console.log([代码][代码]'WebSocket连接创建'[代码][代码], res)[代码] [代码] [代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]fail: [代码][代码]function[代码][代码](err) {[代码] [代码] [代码][代码]wx.showToast({[代码] [代码] [代码][代码]title: [代码][代码]'网络异常!'[代码][代码],[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]console.log(err)[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]})[代码] [代码] [代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 生命周期函数--监听页面隐藏[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onHide: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]SocketTask.close([代码][代码]function[代码] [代码](res) {[代码] [代码] [代码][代码]console.log(res);[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 生命周期函数--监听页面卸载[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onUnload: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]SocketTask.close([代码][代码]function[代码][代码](res){[代码] [代码] [代码][代码]console.log(res);[代码] [代码] [代码][代码]})[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 页面相关事件处理函数--监听用户下拉动作[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onPullDownRefresh: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 页面上拉触底事件的处理函数[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onReachBottom: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码] [代码] [代码][代码]* 用户点击右上角分享[代码] [代码] [代码][代码]*/[代码] [代码] [代码][代码]onShareAppMessage: [代码][代码]function[代码][代码]() {[代码] [代码] [代码][代码]}[代码] [代码]})[代码]