Quick-cocos2d-x v3.3 SocketTCP链接

前端之家收集整理的这篇文章主要介绍了Quick-cocos2d-x v3.3 SocketTCP链接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Quick-Cocos2d-x v3.3里面提供了两种长连接WebSockets、SocketTCP,这里说一下SocketTCP的用法


1
2
3
localnet=require( "framework.cc.net.init" )
localByteArray=require( "framework.cc.utils.ByteArray" )
require( "framework.cc.utils.bit" )


-- 网络初始化,添加侦听函数

3
4
5
6
7
8
9
10
11
12
13
14
15
16
functionscnet.init()
local time =net.SocketTCP.getTime()
print( "sockettime:" .. )
localsocket=net.SocketTCP. new ()
socket:setName( "HeroGameTcp" )
socket:setTickTime(1)
socket:setReconnTime(6)
socket:setConnFailTime(4)
socket:addEventListener(net.SocketTCP.EVENT_DATA,scnet.receive)
socket:addEventListener(net.SocketTCP.EVENT_CLOSE,scnet.tcpClose)
socket:addEventListener(net.SocketTCP.EVENT_CLOSED,scnet.tcpClosed)
socket:addEventListener(net.SocketTCP.EVENT_CONNECTED,scnet.tcpConnected)
socket:addEventListener(net.SocketTCP.EVENT_CONNECT_FAILURE,scnet.error)
scnet.socket=socket
end

--发送数据给服务器

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
do --去除前两个长度
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

猜你在找的Cocos2d-x相关文章