最近一直在研究微信小程序。
到今天刚刚把客户端完成了,这个客户端应当可以满足对数据要求不是特别多的tcpsocket请求和处理。
朋友们如果需要使用TCPSOCKET与服务器交换数据的,可以把下面代码直接拷贝使用。只要你的数据量不是很大,都可以满足。
只有小程序客户端的代码,服务器是用JAVA写的,就不上了(以前写的,比较庞大,上百万行代码)
其中的arraybufferTOString和stringTOArrayBuffer,参考了其他朋友的代码。
至于其他页面发送的服务器数据请求使用的代码,很简单,就是一个JSON数据结构,服务器返回的数据也是JSON结构,处理起来比较简单。
getApp().globalData.coma.push(
{ id:50,
name:"手机号码验证",
value:this.data.phone.trim(),
thisEvent:this
},
);
上代码
// app.js
App({
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
//this.globalData.registerStatus=wx.getStorageSync('registerStatus')
if (!this.globalData.registerStatus){
wx.removeStorageSync('appcode')
wx.removeStorageSync('userid')
wx.removeStorageSync('phone')
wx.removeStorageSync('firstlogintime')
wx.removeStorageSync('registerStatus')
}
this.globalData.appcode = wx.getStorageSync('appcode')//用来对数据进行加密
this.globalData.firstlogintime = wx.getStorageSync('firstlogintime')//用来对数据进行加密
this.globalData.userid = wx.getStorageSync('userid')
this.globalData.phone = wx.getStorageSync('phone')
this.startMsgDel()
this.initapp();
},
globalData: {
registerStatus:false,
userInfo: null,
phone:'',
isConnected:false,
socketTask:null,
serverHostname:'192.168.1.5',
serverPort:8189,
appcode:'',
firstlogintime:0,
commtype:0,
componentsMap:new Map(),//存储所有组件的信息
msga:new Array(),//从服务器接收到的数据
coma:new Array(), //客户端发出的命令
resulta:new Array(), //处理以后的命令等待队列
sendmessage:new Array(),//需要发送到服务器的命令以及数据
waiting_result_command:new Map(),//the command is sent to the sever and waiting result
userid:0,//用户 id on the server
phonecheck:false,
result:null,
heartbeatInterval:null,
iswaiting:false,
},
initapp:function(){
wx.login({
success: (res) => {
if (res.code){
this.globalData.appcode = res.code;
this.globalData.coma.push(
{ id:this.globalData.registerStatus ? 2 : 1,
name:this.globalData.registerStatus ? '检查注册信息' : '获取Openid',
value:res.code,
thisEvent:this.globalData,
},
);
}
},
})
},
connectToServer:function(){
if (this.globalData.socketTask == n

最低0.47元/天 解锁文章
5391

被折叠的 条评论
为什么被折叠?



