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 = {
"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();
if (socketOpen) {
that.sendSocketMessage(data);
}
},
sendSocketMessage: function(msg) {
var that = this;
console.log('通过 WebSocket 连接发送数据', JSON.stringify(msg))
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;
that.setData({
windowH: helfH
});
}
});
},
onReady: function() {
var that = this;
SocketTask.onOpen(res => {
socketOpen = true;
console.log('监听 WebSocket 连接打开事件。', res)
var data = {
"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) {
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();
}
})
},
bottom: function() {
var that = this;
this.setData({
scrollTop: 1000000
})
},
onShow: function() {
if (!socketOpen) {
this.webSocket()
}
},
webSocket: function() {
const that = this;
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() {
}
})
|
老哥,能不能注意一下格式
这样行不
略屌。。。服务器端是怎么处理的?
服务器是自己用C#开发的windows服务,复制了一段代码,然后部署在windows上的
老衲也是搞C#的,加个好友吧。QQ:489520785
感谢分享
虽然乱,但还是可以用
老哥,SocketTask.onOpen() 这个方法偶尔才触发怎么解决
强烈建议使用SocketTask,使用wx.sendSocketMessage有几率报taskID not exist的问题
这是最初版本,后面自己封装到APPJS里了,全局用,目前还没出现问题
恩恩
有点乱,还可以
好的,,,我看怎么改
好乱