Cocos Console模块的使用

/base/CCConsole.cpp, 3.0之后新增,用于远程调试

Console 是一个让开发者通过 TCP 连接控制游戏的助手(helper)类. Console将产生一个监听特定 TCP 端口的新线程. Console有一个基本的命令解析器(token parser).每一条命令都会调用std::function<void(int)>. 如果std::function<>需要使用 Cocos2d API, 它需要调用
scheduler->performFunctionInCocosThread( ... );

开启Console监听功能

Director::getInstance()->getConsole->listenOnTCP(5678)

PC端接入app的Console

$nc localhost 5678

连接成功后,通过help指令查看使用方法

\>help

...

1.png

除了默认的指令外,还可以自定义指令


  #include <sys/socket.h>

  //add custom console command

  static struct Console::Command commands[] = {
    {
        "hello", //命令
        "This is a custom command",    //说明
        //回调函数,fd为socket句柄,args为传递的字符串参数
        [](int fd, const std::string& args) {    
            const char msg[] = "how are you?\nArguments passed: ";
            send(fd, msg, sizeof(msg),0);    //发生数据给client
            send(fd, args.c_str(), args.length(),0);
            send(fd, "\n",1,0)
        }
    },
   };

    Director::getInstance()->getConsole()->addCommand(commands[0]);

重新运行可以看到自定义添加的指令

2.png

下载源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容