/base/CCConsole.cpp, 3.0之后新增谷异,用于遠程調試
Console 是一個讓開發(fā)者通過 TCP 連接控制游戲的助手(helper)類. Console將產(chǎn)生一個監(jiān)聽特定 TCP 端口的新線程. Console有一個基本的命令解析器(token parser).每一條命令都會調用std::function<void(int)>. 如果std::function<>需要使用 Cocos2d API, 它需要調用
scheduler->performFunctionInCocosThread( ... );
開啟Console監(jiān)聽功能
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", //說明
//回調函數(shù)螟够,fd為socket句柄,args為傳遞的字符串參數(shù)
[](int fd, const std::string& args) {
const char msg[] = "how are you?\nArguments passed: ";
send(fd, msg, sizeof(msg),0); //發(fā)生數(shù)據(jù)給client
send(fd, args.c_str(), args.length(),0);
send(fd, "\n",1,0)
}
},
};
Director::getInstance()->getConsole()->addCommand(commands[0]);
重新運行可以看到自定義添加的指令
2.png