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
"发送数据出错了......."
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,msgid)
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)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#byteList
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,没有就为
""
,此处可以判断用户是否重新登陆啊等等.......
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#fmt
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"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"key"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#_type
do
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_type<i>,v)
end
end
end
else
"找不到对应的key"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,msg_type.key,data)
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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,len-3
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"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,rData[key])
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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,data[key])
return
end
end
else
local_len=ba:readShort()--读取数组长度
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_len
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"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,data)
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"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
fmt={
{
"id"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,fmt=
"int"
},
{
"name"
"string"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"level"
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"sex"
"int"
}
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"userid"
"int"
}
}
--返回
"5001"
]={
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
fmt={
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"string"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,vinpairs(self._listenerList)
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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,vinpairs(self._listenerList[msgid])
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=
"小房"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=1,sex=1})
"小田"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=11,sex=2})
"2222"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=21,sex=1})
"3333"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=31,sex=2})
data.userid=10001
NetManager:sendMsg(MsgType.CS_LOGIN,data)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。
相关推荐
如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2 1 4
本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991. 软件准备 下载地址:http://cn.cocos2d-x.org/download 2. 简介2.1 引用C
Cocos2D-x-3.0 编译(Win7)
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
quick-cocos2d-x实例之挑战记忆极限设计文档
1. 来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
Cocos2d-x 3 X CMake MinGW版本编译运行
Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
vs 2013 编译cocos2d-x-3.9
下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
Cocos2d-x CCControlPotentiometer之圆形音量button及特效
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
Cocos2d-x入门教程二简单的静态显示对象
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
最新文章
• 如何使用CCRenderTexture创建动态纹理 …
• Cocos-code-ide使用入门学习
• Cocos2D-x-3.0 编译(Win7)
• Cocos2d-x 2 0 在Windows平台下的使用
• quick-cocos2d-x实例之挑战记忆极限设计…
• Cocos2d-x 3 X CMake MinGW版本编译运行
• vs 2013 编译cocos2d-x-3.9
• cocos2d-x游戏开发系列教程-超级玛丽01…
• Cocos2d-x CCControlPotentiometer之圆…
• Cocos2d-x入门教程二简单的静态显示对象
• cocos2d-x中CCScale9Sprite的另一种实现
• Cocos2d-x v2.2.2版本+Win7+VS2010环境…
• Ubuntu14.04+eclipse下cocos2d-x3.0正式…
• 分别基于WIN32 API界面编程和Cocos2d-x…
• Cocos2d-x 开发小记二:控件
• Cocos2d-x 开发小记一:基本动作
• 买Cocos2d-x视频课程送纸质图书
• Cocos2d-x 学习笔记(11.10) Spawn