博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx使用lua和protobuf
阅读量:4069 次
发布时间:2019-05-25

本文共 2107 字,大约阅读时间需要 7 分钟。

为了使游戏开发更加方便快捷,我继续了protobuf在lua下的尝试。

socket使用的是cocos2dx集成的websocket。

先说下环境:cocos2d-x-2.2.1 + protobuf 2.5.0 + protoc-gen-lua + Python 2.7.5

1.在protobuf目录下依次执行如下命令

[plain] 
  1. python setup.py build  
  2. python setup.py install  

2.在protoc-gen-lua目录下的plugin目录中新建protoc-gen-lua.bat文件,并将如下内容粘贴到里面

[plain] 
  1. @python <你的目录>\protoc-gen-lua\plugin\protoc-gen-lua  

3.生成protobuf对应的lua文件,执行如下命令:

[plain] 
  1. <你的路径>/protoc.exe --lua_out=./ --plugin=protoc-gen-lua="<你的路径>\protoc-gen-lua\plugin\protoc-gen-lua.bat" test.proto  
执行完后就会生成test_pb.lua文件。

4.使用cocos2dx的create_project.py创建lua工程;

5.将protoc-gen-lua/protobuf目录下的pb.c文件复制到lua工程的Classes目录下,并加入到C++工程中;

6.将protoc-gen-lua/protobuf目录下的所有lua文件复制到lua工程的Resources目录下;

7.编辑AppDelegate.cpp文件,添加如下代码:

[cpp] 
  1. extern "C"{    
  2. #include <lua.h>    
  3. #include <lualib.h>    
  4. #include <lauxlib.h>    
  5.     int luaopen_pb (lua_State *L);    
  6. }  

8.在AppDelegate::applicationDidFinishLaunching()方法中加入初始化方法:

[cpp] 
  1. luaopen_pb(tolua_s);  

9.此时对lua工程进行编译,如果出错,请检查并修正;编译通过,并且可以正常运行后继续下面的步骤;

10.cocos2dx默认产生的lua工程包含2个文件hello.lua与hello2.lua,打开hello2.lua,将如下内容添加到文件末尾(因为我使用的是websocket,各位可根据自己的实际情况进行修改):

[javascript] 
  1. local wsProtobuf=nil  
  2. function testProtobuf()  
  3.     wsProtobuf = WebSocket:create("ws://localhost:8080/web")  
  4.     local function onOpen(strData)  
  5.         print("socket open ...")  
  6.         require "test_pb"  
  7.         local msg=test_pb.Message()  
  8.         msg.id=101        
  9.         local person =test_pb.Person()  
  10.         person.id=111  
  11.         person.name="user1"  
  12.         person.email="a1@a.a"  
  13.         msg.data=person:SerializeToString()  
  14.         local pb_data = msg:SerializeToString()  
  15.         local t={string.byte(pb_data,1,-1)}  
  16.         wsProtobuf:sendBinaryMsg(t,table.getn(t))  
  17.     end  
  18.     local function onMessage(strData)  
  19.         print("socket message ...")  
  20.     end  
  21.     local function onClose(strData)  
  22.         print("socket close ...")  
  23.     end  
  24.     local function onError(strData)  
  25.         print("socket error")  
  26.     end  
  27.       
  28.     if nil ~= wsProtobuf then  
  29.         wsProtobuf:registerScriptHandler(onOpen,kWebSocketScriptHandlerOpen)  
  30.         wsProtobuf:registerScriptHandler(onMessage,kWebSocketScriptHandlerMessage)  
  31.         wsProtobuf:registerScriptHandler(onClose,kWebSocketScriptHandlerClose)  
  32.         wsProtobuf:registerScriptHandler(onError,kWebSocketScriptHandlerError)  
  33.     end   
  34. end  

11.然后在hello.lua中调用testProtobuf()函数即可。

测试运行,你可以在服务器端查看收到的消息。

转载地址:http://lsoji.baihongyu.com/

你可能感兴趣的文章
read humor_campus
查看>>
my read_soft
查看>>
my pdfs
查看>>
framework Schedule Quartz
查看>>
IBM WebSphere Commerce Analyzer
查看>>
Unix + OS IBM Aix System Director
查看>>
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
framework apache commons
查看>>
my read work
查看>>
blancerServer IBM WebSphere Edge Server 6.1
查看>>
db db2 base / instance database tablespace container
查看>>
my read _job
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>