16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
functionscnet.send(msgid,data)
--encodeData此方法是根据需要发送的数据安装与服务器定义好的消息格式去write
local_ba=scnet.encodeData(msgid,data)
"scnet.send_ba"
,_ba:getLen())
if
not_bathen
"发送数据出错了......."
return
end
_ba:setPos(1)
localbyteList={}
localbyteCount=0
--把数据读出来,加密
for
i=1,#_ba._buf
do
localtmpBit=string.byte(_ba:readRawByte())
byteCount=byteCount+tmpBit
tmpBit=bit.band(tmpBit+80,255)
tmpBit=bit.band(bit.bnot(bit.band(tmpBit,255)),255)
byteList<i>=tmpBit
end
byteCount=byteCount%256
--最后再组成一个新的ByteArray
localresult=ByteArray.
(ByteArray.ENDIAN_BIG)
result:writeShort(_ba:getLen()+3)
result:writeByte(byteCount)
do
result:writeByte(byteList<i>)
end
--把数据发送给服务器
scnet.socket:send(result:getPack())
-- 根据messageid来确定数据格式
functionscnet.encodeData(msgid,monospace!important; font-size:1em!important; min-height:auto!important; color:black!important; background:none!important">msgidthen
localba=ByteArray.
(ByteArray.ENDIAN_BIG)
localfmt=InfoUtil:getMsgFmt(msgid)--此处为读取消息格式看下面的MessageType里面会有定义
ba:writeStringUShort(
"token"
)--此处为用户token,没有就为
""
,此处可以判断用户是否重新登陆啊等等.......
do
scnet.writeData(ba,fmt<i>,data)
localbaLength=ba:getLen()
localbt=ByteArray.
(ByteArray.ENDIAN_BIG)
bt:writeShort(baLength+4)--2为messagelength2为messagetype
bt:writeShort(msgid)
bt:writeBytes(ba)
return
bt
end
-- write 数据
28
functionscnet.writeData(ba,msg_type,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">
localkey=msg_type.key
"scnet.writeData"
"key"
keyanddata[key]then
local_type=msg_type[
"fmt"
]
type(_type)==
"string"
then
_type==
then
ba:writeStringUShort(data[key])
elseif_type==
"number"
then
ba:writeLuaNumber(data[key])
"int"
then
ba:writeInt(data[key])
"short"
then
ba:writeShort(data[key])
end
else
ba:writeShort(#data[key])
k,vinpairs(data[key])
do
do
end
end
end
else
"找不到对应的key"
end
-- 读取数据
-- 接收消息
12
functionscnet.receive(event)
(ByteArray.ENDIAN_BIG)
ba:writeBuf(event.data)
ba:setPos(1)
--有连包的情况,所以要读取数据
while
ba:getAvailable()<=ba:getLen()
scnet.decodeData(ba)
ba:getAvailable()==0then
break
end
end
-- 消息数据解析
24
functionscnet.decodeData(ba)
locallen=ba:readShort()--读数据总长度
localtotal=ba:readByte()--一个用于验证的数子
localbyteList={}
localtmpTotal=0
localtmpBit=ba:readByte()
localenByte=scnet.decodeByte(tmpBit)
tmpTotal=tmpTotal+enByte
byteList<i>=enByte
end
(ByteArray.ENDIAN_BIG)
do
result:writeRawByte(string.
char
(byteList<i>))
end
result:setPos(1)
(tmpTotal%256)==totalthen
scnet.decodeMsg(result)
else
"scnet.decodeDatatotalerror"
)
end
-- 根据格式解析数据
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
functionscnet.decodeMsg(byteArray)
localrData={}
locallen=byteArray:readShort()
localmsgid=byteArray:readShort()
localroleString=byteArray:readStringUShort()
localfmt=InfoUtil:getMsgFmt(msgid)
do
scnet.readData(byteArray,rData)
end
rData[
"result"
]~=0then
"resulthandlerishere"
return
else
NetManager:receiveMsg(msgid,rData)
end
--readData
functionscnet.readData(ba,data)
localkey=msg_type.key
keythen
data[key]=data[key]or{}
]
then
then
data[key]=ba:readStringUShort()
then
data[key]=ba:readLuaNumber()
then
data[key]=ba:readInt()
then
data[key]=ba:readShort()
end
key==
then--当结果不为零的时候,说明有错误
data[key]~=0then
return
end
end
else
local_len=ba:readShort()--读取数组长度
do
localtmp={}
j=1,153)!important; background:none!important">do
scnet.readData(ba,_type[j],tmp)
end
table.insert(data[key],tmp)
end
end
else
"找不到对应的keyscnet.readData"
end
-- 数据解密
5
functionscnet.decodeByte(byte)
localtmp=bit.band(bit.bnot(bit.band(byte,255)
tmp=bit.band((tmp+256-80),255)
tmp
消息格式
46
--发送
MsgFmt[
"1001"
]={
{
key=
"list"
fmt={
{
"id"
"int"
},
{
"name"
"string"
{
"level"
"int"
{
"sex"
"int"
}
}
{
"userid"
"int"
}
}
--返回
"5001"
]={
{
"int"
{
fmt={
{
"int"
{
"string"
{
"int"
{
"int"
}
}
}
}
|
网络管理NetManager
管理网络的发送与接收
58
59
--初始化
functionNetManager:init()
self._listenerList={}
scnet.init()
scnet.connect(host,port)
end
--注册之后接受到服务器消息之后进行广播,谁注册,谁相应
functionNetManager:registerMsg(msgid,callBack)
self._listenerList[msgid]=self._listenerList[msgid]or{}
localisExist=self:findCallBack(msgid,callBack)
notisExistthen
table.insert(self._listenerList[msgid],callBack)
functionNetManager:removeRegister(msgid,callBack)
self._listenerList[msgid]then
do
v==callBackthen
self._listenerList[msgid][k]=nil
end
end
end
--发送消息
--整理数据发送给服务器
functionNetManager:sendMsg(msgid,data)
scnet.send(msgid,data)
end
--接受消息
--派发事件(数据)
functionNetManager:receiveMsg(msgid,data)
self._listenerList[msgid]then
do
v(data)
end
end
end
functionNetManager:findCallBack(msgid,callBack)
do
v==callBackthen
return
true
end
end
false
test
19
--监听事件
functionMainScene:onEnter()
NetManager:registerMsg(MsgType.SC_LOGIN,handler(self,self.receiveHandler))
end
--移除坚挺
functionMainScene:onExit()
NetManager:removeRegister(MsgType.SC_LOGIN,self.receiveHandler))
end
--发送数据,根据MsgFmt构造数据
localdata={}
data.list={}
table.insert(data.list,{id=1001,name=
"小房"
"小田"
"2222"
"3333"
data.userid=10001
NetManager:sendMsg(MsgType.CS_LOGIN,data)
|
原文链接:https://www.f2er.com/cocos2dx/340220.html
| | | | | | |