├── .cocos-project.json ├── .gitignore ├── CMakeLists.txt ├── Classes ├── AppDelegate.cpp ├── AppDelegate.h ├── ChatRoom.cpp ├── ChatRoom.h ├── HelloWorldScene.cpp ├── HelloWorldScene.h ├── PomeloClient.cpp ├── PomeloClient.h └── lib │ ├── include │ ├── pomelo.h │ └── pomelo_trans.h │ ├── libpomelo2.lib │ ├── libuv.lib │ └── openssl.lib ├── LICENSE ├── README.md ├── Resources ├── CloseNormal.png ├── CloseSelected.png ├── HelloWorld.png ├── animationbuttonnormal.png ├── animationbuttonpressed.png ├── fonts │ ├── Marker Felt.ttf │ └── arial.ttf ├── green_edit.png ├── orange_edit.png └── res │ └── .gitkeep └── 常见问题.md /.cocos-project.json: -------------------------------------------------------------------------------- 1 | { 2 | "engine_version": "cocos2d-x-3.10", 3 | "project_type": "cpp" 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | 24 | # Visual Studio 2015 cache/options directory 25 | .vs/ 26 | # Uncomment if you have tasks that create the project's static files in wwwroot 27 | #wwwroot/ 28 | 29 | # MSTest test Results 30 | [Tt]est[Rr]esult*/ 31 | [Bb]uild[Ll]og.* 32 | 33 | # NUNIT 34 | *.VisualState.xml 35 | TestResult.xml 36 | 37 | # Build Results of an ATL Project 38 | [Dd]ebugPS/ 39 | [Rr]eleasePS/ 40 | dlldata.c 41 | 42 | # DNX 43 | project.lock.json 44 | artifacts/ 45 | 46 | *_i.c 47 | *_p.c 48 | *_i.h 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.tmp_proj 63 | *.log 64 | *.vspscc 65 | *.vssscc 66 | .builds 67 | *.pidb 68 | *.svclog 69 | *.scc 70 | 71 | # Chutzpah Test files 72 | _Chutzpah* 73 | 74 | # Visual C++ cache files 75 | ipch/ 76 | *.aps 77 | *.ncb 78 | *.opendb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | # NuGet v3's project.json files produces more ignoreable files 154 | *.nuget.props 155 | *.nuget.targets 156 | 157 | # Microsoft Azure Build Output 158 | csx/ 159 | *.build.csdef 160 | 161 | # Microsoft Azure Emulator 162 | ecf/ 163 | rcf/ 164 | 165 | # Microsoft Azure ApplicationInsights config file 166 | ApplicationInsights.config 167 | 168 | # Windows Store app package directory 169 | AppPackages/ 170 | BundleArtifacts/ 171 | 172 | # Visual Studio cache files 173 | # files ending in .cache can be ignored 174 | *.[Cc]ache 175 | # but keep track of directories ending in .cache 176 | !*.[Cc]ache/ 177 | 178 | # Others 179 | ClientBin/ 180 | ~$* 181 | *~ 182 | *.dbmdl 183 | *.dbproj.schemaview 184 | *.pfx 185 | *.publishsettings 186 | node_modules/ 187 | orleans.codegen.cs 188 | 189 | # RIA/Silverlight projects 190 | Generated_Code/ 191 | 192 | # Backup & report files from converting an old project file 193 | # to a newer Visual Studio version. Backup files are not needed, 194 | # because we have git ;-) 195 | _UpgradeReport_Files/ 196 | Backup*/ 197 | UpgradeLog*.XML 198 | UpgradeLog*.htm 199 | 200 | # SQL Server files 201 | *.mdf 202 | *.ldf 203 | 204 | # Business Intelligence projects 205 | *.rdl.data 206 | *.bim.layout 207 | *.bim_*.settings 208 | 209 | # Microsoft Fakes 210 | FakesAssemblies/ 211 | 212 | # GhostDoc plugin setting file 213 | *.GhostDoc.xml 214 | 215 | # Node.js Tools for Visual Studio 216 | .ntvs_analysis.dat 217 | 218 | # Visual Studio 6 build log 219 | *.plg 220 | 221 | # Visual Studio 6 workspace options file 222 | *.opt 223 | 224 | # Visual Studio LightSwitch build output 225 | **/*.HTMLClient/GeneratedArtifacts 226 | **/*.DesktopClient/GeneratedArtifacts 227 | **/*.DesktopClient/ModelManifest.xml 228 | **/*.Server/GeneratedArtifacts 229 | **/*.Server/ModelManifest.xml 230 | _Pvt_Extensions 231 | 232 | # Paket dependency manager 233 | .paket/paket.exe 234 | 235 | # FAKE - F# Make 236 | .fake/ 237 | 238 | # platform proj and cocos2d 239 | cocos2d 240 | proj.android 241 | proj.android-studio 242 | proj.ios_mac 243 | proj.linux 244 | proj.win8.1-universal 245 | proj.win10 246 | proj.win32 247 | Debug.win32 -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #/**************************************************************************** 2 | # Copyright (c) 2013-2014 cocos2d-x.org 3 | # 4 | # http://www.cocos2d-x.org 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | # THE SOFTWARE. 23 | # ****************************************************************************/ 24 | cmake_policy(SET CMP0017 NEW) 25 | 26 | cmake_minimum_required(VERSION 2.8) 27 | 28 | set(APP_NAME MyGame) 29 | project (${APP_NAME}) 30 | 31 | set(COCOS2D_ROOT ${CMAKE_SOURCE_DIR}/cocos2d) 32 | 33 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${COCOS2D_ROOT}/cmake/Modules/") 34 | include(CocosBuildHelpers) 35 | 36 | # libcocos2d 37 | set(BUILD_CPP_TESTS OFF CACHE BOOL "turn off build cpp-tests") 38 | set(BUILD_LUA_LIBS OFF CACHE BOOL "turn off build lua related targets") 39 | set(BUILD_JS_LIBS OFF CACHE BOOL "turn off build js related targets") 40 | add_subdirectory(${COCOS2D_ROOT}) 41 | 42 | # Some macro definitions 43 | if(WINDOWS) 44 | if(BUILD_SHARED_LIBS) 45 | ADD_DEFINITIONS (-D_USRDLL -D_EXPORT_DLL_ -D_USEGUIDLL -D_USREXDLL -D_USRSTUDIODLL) 46 | else() 47 | ADD_DEFINITIONS (-DCC_STATIC) 48 | endif() 49 | 50 | ADD_DEFINITIONS (-DCOCOS2DXWIN32_EXPORTS -D_WINDOWS -DWIN32 -D_WIN32) 51 | set(PLATFORM_FOLDER win32) 52 | elseif(MACOSX OR APPLE) 53 | ADD_DEFINITIONS (-DCC_TARGET_OS_MAC) 54 | ADD_DEFINITIONS (-DUSE_FILE32API) 55 | set(PLATFORM_FOLDER mac) 56 | elseif(LINUX) 57 | ADD_DEFINITIONS(-DLINUX) 58 | set(PLATFORM_FOLDER linux) 59 | elseif(ANDROID) 60 | ADD_DEFINITIONS (-DUSE_FILE32API) 61 | set(PLATFORM_FOLDER android) 62 | else() 63 | message( FATAL_ERROR "Unsupported platform, CMake will exit" ) 64 | endif() 65 | 66 | 67 | # Compiler options 68 | if(MSVC) 69 | ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS 70 | -wd4251 -wd4244 -wd4334 -wd4005 -wd4820 -wd4710 71 | -wd4514 -wd4056 -wd4996 -wd4099) 72 | else() 73 | set(CMAKE_C_FLAGS_DEBUG "-g -Wall -DCOCOS2D_DEBUG=1") 74 | set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG}) 75 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") 76 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-deprecated-declarations -Wno-reorder") 77 | if(CLANG) 78 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 79 | endif() 80 | endif(MSVC) 81 | 82 | 83 | 84 | set(PLATFORM_SPECIFIC_SRC) 85 | set(PLATFORM_SPECIFIC_HEADERS) 86 | if(MACOSX OR APPLE) 87 | set(PLATFORM_SPECIFIC_SRC 88 | proj.ios_mac/ios/main.m 89 | proj.ios_mac/ios/RootViewController.mm 90 | proj.ios_mac/ios/AppController.mm 91 | ) 92 | set(PLATFORM_SPECIFIC_HEADERS 93 | proj.ios_mac/ios/RootViewController.h 94 | proj.ios_mac/ios/AppController.h 95 | ) 96 | elseif(LINUX) #assume linux 97 | set(PLATFORM_SPECIFIC_SRC 98 | proj.linux/main.cpp 99 | ) 100 | elseif ( WIN32 ) 101 | set(PLATFORM_SPECIFIC_SRC 102 | proj.win32/main.cpp 103 | ) 104 | set(PLATFORM_SPECIFIC_HEADERS 105 | proj.win32/main.h 106 | proj.win32/resource.h 107 | ) 108 | endif() 109 | 110 | include_directories( 111 | /usr/local/include/GLFW 112 | /usr/include/GLFW 113 | ${COCOS2D_ROOT}/cocos 114 | ${COCOS2D_ROOT}/cocos/platform 115 | ${COCOS2D_ROOT}/cocos/audio/include/ 116 | Classes 117 | ) 118 | if ( WIN32 ) 119 | include_directories( 120 | ${COCOS2D_ROOT}/external/glfw3/include/win32 121 | ${COCOS2D_ROOT}/external/win32-specific/gles/include/OGLES 122 | ) 123 | endif( WIN32 ) 124 | 125 | set(GAME_SRC 126 | Classes/AppDelegate.cpp 127 | Classes/HelloWorldScene.cpp 128 | ${PLATFORM_SPECIFIC_SRC} 129 | ) 130 | 131 | set(GAME_HEADERS 132 | Classes/AppDelegate.h 133 | Classes/HelloWorldScene.h 134 | ${PLATFORM_SPECIFIC_HEADERS} 135 | ) 136 | 137 | if(GAME_HEADERS) 138 | if ( WIN32 ) 139 | add_executable(${APP_NAME} WIN32 ${GAME_SRC} ${GAME_HEADERS}) 140 | else() 141 | add_executable(${APP_NAME} ${GAME_SRC} ${GAME_HEADERS}) 142 | endif ( WIN32 ) 143 | else() 144 | if ( WIN32 ) 145 | add_executable(${APP_NAME} WIN32 ${GAME_SRC}) 146 | else() 147 | add_executable(${APP_NAME} ${GAME_SRC}) 148 | endif ( WIN32 ) 149 | endif() 150 | 151 | target_link_libraries(${APP_NAME} cocos2d) 152 | 153 | set(APP_BIN_DIR "${CMAKE_BINARY_DIR}/bin") 154 | 155 | set_target_properties(${APP_NAME} PROPERTIES 156 | RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}") 157 | 158 | if ( WIN32 ) 159 | #also copying dlls to binary directory for the executable to run 160 | pre_build(${APP_NAME} 161 | COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources 162 | COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources 163 | COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/gles/prebuilt/glew32.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE} 164 | COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/zlib/prebuilt/zlib1.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE} 165 | ) 166 | else() 167 | pre_build(${APP_NAME} 168 | COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources 169 | COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources 170 | ) 171 | 172 | endif() 173 | -------------------------------------------------------------------------------- /Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(320, 480); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(320, 480); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(768, 1024); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(1536, 2048); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("pomeloclientcpp", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("pomeloclientcpp"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | // turn on display FPS 51 | director->setDisplayStats(true); 52 | 53 | // set FPS. the default value is 1.0/60 if you don't call this 54 | director->setAnimationInterval(1.0 / 60); 55 | 56 | // Set the design resolution 57 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 58 | Size frameSize = glview->getFrameSize(); 59 | // if the frame's height is larger than the height of medium size. 60 | if (frameSize.height > mediumResolutionSize.height) 61 | { 62 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 63 | } 64 | // if the frame's height is larger than the height of small size. 65 | else if (frameSize.height > smallResolutionSize.height) 66 | { 67 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 68 | } 69 | // if the frame's height is smaller than the height of medium size. 70 | else 71 | { 72 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 73 | } 74 | 75 | register_all_packages(); 76 | 77 | // create a scene. it's an autorelease object 78 | auto scene = HelloWorld::createScene(); 79 | 80 | // run 81 | director->runWithScene(scene); 82 | 83 | return true; 84 | } 85 | 86 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 87 | void AppDelegate::applicationDidEnterBackground() { 88 | Director::getInstance()->stopAnimation(); 89 | 90 | // if you use SimpleAudioEngine, it must be pause 91 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 92 | } 93 | 94 | // this function will be called when the app is active again 95 | void AppDelegate::applicationWillEnterForeground() { 96 | Director::getInstance()->startAnimation(); 97 | 98 | // if you use SimpleAudioEngine, it must resume here 99 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 100 | } 101 | -------------------------------------------------------------------------------- /Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /Classes/ChatRoom.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Classes/ChatRoom.cpp -------------------------------------------------------------------------------- /Classes/ChatRoom.h: -------------------------------------------------------------------------------- 1 | #ifndef __CHATROOM_SCENE_H__ 2 | #define __CHATROOM_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | #include "pomelo.h" 6 | #include "ui/UIListView.h" 7 | #include "ui/UIText.h" 8 | #include 9 | #include 10 | struct EventCb{ 11 | pc_client_t* client; 12 | int ev_type; 13 | void* ex_data; 14 | std::string arg1; 15 | std::string arg2; 16 | }; 17 | class ChatRoom : public cocos2d::Layer 18 | { 19 | public: 20 | static cocos2d::Scene* createScene(int port,std::string name, std::string roomId); 21 | 22 | virtual bool init(); 23 | 24 | // a selector callback 25 | void menuCloseCallback(cocos2d::Ref* pSender); 26 | 27 | // implement the "static create()" method manually 28 | CREATE_FUNC(ChatRoom); 29 | // pomelo 30 | pc_client_t* client; 31 | int handler_id; 32 | std::string roleName; 33 | std::string roomId; 34 | static void request_cb(const pc_request_t* req, int rc, const char* resp); 35 | static void event_cb(pc_client_t* client, int ev_type, void* ex_data, const char* arg1, const char* arg2); 36 | void connectToServer(int port); 37 | void addMsg(std::string router, std::string msg); 38 | cocos2d::ui::ListView * listView; 39 | std::vector eventCbArray; 40 | std::mutex eventMutex; 41 | void update(float delta); 42 | 43 | }; 44 | 45 | #endif // __ChatRoom_SCENE_H__ 46 | -------------------------------------------------------------------------------- /Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "ui/UIEditBox/UIEditBox.h" 3 | #include "ui/UIButton.h" 4 | #include "json\document.h" 5 | #include "ChatRoom.h" 6 | 7 | USING_NS_CC; 8 | using namespace cocos2d::ui; 9 | 10 | #define flag ((void *)0x11) 11 | 12 | 13 | Scene* HelloWorld::createScene() 14 | { 15 | // 'scene' is an autorelease object 16 | auto scene = Scene::create(); 17 | 18 | // 'layer' is an autorelease object 19 | auto layer = HelloWorld::create(); 20 | layer->scheduleUpdate(); 21 | 22 | // add layer as a child to scene 23 | scene->addChild(layer); 24 | 25 | // return the scene 26 | return scene; 27 | } 28 | 29 | // on "init" you need to initialize your instance 30 | bool HelloWorld::init() 31 | { 32 | ////////////////////////////// 33 | // 1. super init first 34 | if ( !Layer::init() ) 35 | { 36 | return false; 37 | } 38 | 39 | Size visibleSize = Director::getInstance()->getVisibleSize(); 40 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 41 | 42 | ///////////////////////////// 43 | // 2. add a menu item with "X" image, which is clicked to quit the program 44 | // you may modify it. 45 | 46 | // add a "close" icon to exit the progress. it's an autorelease object 47 | auto closeItem = MenuItemImage::create( 48 | "CloseNormal.png", 49 | "CloseSelected.png", 50 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 51 | 52 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 53 | origin.y + closeItem->getContentSize().height/2)); 54 | 55 | // create menu, it's an autorelease object 56 | auto menu = Menu::create(closeItem, NULL); 57 | menu->setPosition(Vec2::ZERO); 58 | this->addChild(menu, 1); 59 | 60 | ///////////////////////////// 61 | // 3. add your codes below... 62 | 63 | // add a label shows "Hello World" 64 | // create and initialize a label 65 | 66 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 67 | 68 | // position the label on the center of the screen 69 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 70 | origin.y + visibleSize.height - label->getContentSize().height)); 71 | 72 | // add the label as a child to this layer 73 | this->addChild(label, 1); 74 | 75 | // add "HelloWorld" splash screen" 76 | auto sprite = Sprite::create("HelloWorld.png"); 77 | 78 | // position the sprite on the center of the screen 79 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 80 | 81 | // add the sprite as a child to this layer 82 | this->addChild(sprite, 0); 83 | 84 | auto name = Label::createWithTTF("name:", "fonts/Marker Felt.ttf", 16.0f); 85 | name->setPosition(visibleSize.width / 2 - 100, 300); 86 | this->addChild(name,10); 87 | auto room = Label::createWithTTF("roomId:", "fonts/Marker Felt.ttf", 16.0f); 88 | room->setPosition(visibleSize.width / 2 - 100 , 200); 89 | this->addChild(room,10); 90 | auto editBoxSize = Size(200, 30); 91 | auto nameEditBox = ui::EditBox::create(editBoxSize, "orange_edit.png"); 92 | nameEditBox->setPosition(Vec2(visibleSize.width / 2 + 50, 300)); 93 | this->addChild(nameEditBox); 94 | auto roomEditBox = ui::EditBox::create(editBoxSize, "orange_edit.png"); 95 | roomEditBox->setPosition(Vec2(visibleSize.width / 2 + 50, 200)); 96 | this->addChild(roomEditBox); 97 | 98 | auto loginBtn = ui::Button::create("animationbuttonnormal.png", 99 | "animationbuttonpressed.png"); 100 | loginBtn->setTitleText("entry"); 101 | loginBtn->setAnchorPoint(Vec2(0.5, 0.5)); 102 | loginBtn->setPosition(Vec2(visibleSize.width / 2 , 100)); 103 | loginBtn->addClickEventListener([=](Ref*){ 104 | auto name = nameEditBox->getText(); 105 | auto roomId = roomEditBox->getText(); 106 | CCLOG("name = %s, room = %s", name, roomId); 107 | auto scene = ChatRoom::createScene(port, name, roomId); 108 | Director::getInstance()->replaceScene(scene); 109 | pomeloClient->destroy(); 110 | }); 111 | this->addChild(loginBtn, 11); 112 | // pomelo init 113 | PomeloClient::initLib(NULL, NULL, NULL, NULL); 114 | pomeloClient = PomeloClient::createClient(); 115 | pomeloClient->registerEventCb(PC_EV_CONNECTED_NAME, [=](const char *msg){ 116 | pomeloClient->requestWithTimeout("gate.gateHandler.queryEntry", "{\"username\": \"test\",\"uid\":\"1\"}", (void *)0x14, 2, [=](const char *msg){ 117 | CCLOG("resp %s = :", msg); 118 | using rapidjson::Document; 119 | Document doc; 120 | doc.Parse<0>(msg); 121 | port = doc["port"].GetInt(); 122 | }); 123 | }); 124 | pomeloClient->connect("192.168.0.14", 3014, NULL); 125 | 126 | return true; 127 | } 128 | 129 | 130 | void HelloWorld::menuCloseCallback(Ref* pSender) 131 | { 132 | Director::getInstance()->end(); 133 | 134 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 135 | exit(0); 136 | #endif 137 | } 138 | 139 | void HelloWorld::update(float delta){ 140 | pomeloClient->update(delta); 141 | } -------------------------------------------------------------------------------- /Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | #include "pomelo.h" 6 | #include "PomeloClient.h" 7 | 8 | class HelloWorld : public cocos2d::Layer 9 | { 10 | public: 11 | static cocos2d::Scene* createScene(); 12 | 13 | virtual bool init(); 14 | 15 | // a selector callback 16 | void menuCloseCallback(cocos2d::Ref* pSender); 17 | 18 | // implement the "static create()" method manually 19 | CREATE_FUNC(HelloWorld); 20 | // pomelo 21 | PomeloClient *pomeloClient; 22 | int port; 23 | void update(float delta); 24 | 25 | }; 26 | 27 | #endif // __HELLOWORLD_SCENE_H__ 28 | -------------------------------------------------------------------------------- /Classes/PomeloClient.cpp: -------------------------------------------------------------------------------- 1 | #include "PomeloClient.h" 2 | using namespace std; 3 | mutex PomeloClient::eventLock; 4 | 5 | PomeloClient * PomeloClient::createClient(){ 6 | auto client = new PomeloClient(); 7 | client->init(); 8 | return client; 9 | } 10 | 11 | void PomeloClient::initLib(void (*pc_log)(int level, const char* msg, ...), 12 | void* (*pc_alloc)(size_t len), void (*pc_free)(void* d), const char* platform){ 13 | pc_lib_init(pc_log, pc_alloc, pc_free, platform); 14 | } 15 | void PomeloClient::cleanupLib(){ 16 | pc_lib_cleanup(); 17 | } 18 | 19 | void PomeloClient::eventCb(pc_client_t *client, int ev_type, void* ex_data, 20 | const char* arg1, const char* arg2){ 21 | auto pomeloClient = (PomeloClient *)pc_client_ex_data(client); 22 | if (arg1 == NULL){ 23 | arg1 = ""; 24 | } 25 | if (arg2 == NULL){ 26 | arg2 = ""; 27 | } 28 | eventLock.lock(); 29 | EventCbData t = { ev_type, ex_data, arg1, arg2 }; 30 | pomeloClient->eventCbQueue.push_back(t); 31 | eventLock.unlock(); 32 | } 33 | 34 | void PomeloClient::requestCb(const pc_request_t * req, int rc, const char * resp){ 35 | auto pomeloClient = (PomeloClient *)pc_client_ex_data(pc_request_client(req)); 36 | auto route = pc_request_route(req); 37 | eventLock.lock(); 38 | pomeloClient->requestCbQueue.push_back(RequestCbData{ rc, resp, route }); 39 | eventLock.unlock(); 40 | } 41 | 42 | void PomeloClient::notifyCb(const pc_notify_t * req, int rc){ 43 | auto pomeloClient = (PomeloClient *)pc_client_ex_data(pc_notify_client(req)); 44 | auto route = pc_notify_route(req); 45 | eventLock.lock(); 46 | pomeloClient->notifyCbQueue.push_back(NotifyCbData{ rc, route }); 47 | eventLock.unlock(); 48 | } 49 | 50 | void PomeloClient::init(){ 51 | pc_client_config_t config = PC_CLIENT_CONFIG_DEFAULT; 52 | client = (pc_client_t *)malloc(pc_client_size()); 53 | pc_client_init(client, (void*)this, &config); 54 | eventHandler = pc_client_add_ev_handler(client, PomeloClient::eventCb, (void *)this, NULL); 55 | } 56 | void PomeloClient::update(float delta){ 57 | if(eventCbQueue.size() <= 0 && requestCbQueue.size() <= 0 && notifyCbQueue.size() <= 0){ 58 | return ; 59 | } 60 | eventLock.lock(); 61 | for(int i=0;i cb){ 109 | functionMap.erase(route); 110 | functionMap[route] = cb; 111 | return pc_request_with_timeout(client, route, msg, ex_data, timeout, PomeloClient::requestCb); 112 | } 113 | int PomeloClient::notifyWithTimeout(const char* route,const char* msg, void* ex_data, 114 | int timeout, std::function cb){ 115 | functionMap.erase(route); 116 | functionMap[route] = cb; 117 | return pc_notify_with_timeout(client, route, msg, ex_data, timeout, PomeloClient::notifyCb); 118 | } 119 | int PomeloClient::registerEventCb(const char * route, std::function cb){ 120 | functionMap.erase(route); 121 | functionMap[route] = cb; 122 | return 0; 123 | } 124 | 125 | void PomeloClient::destroy(){ 126 | pc_client_disconnect(client); 127 | pc_client_rm_ev_handler(client, eventHandler); 128 | pc_client_cleanup(client); 129 | free(client); 130 | delete this; 131 | } -------------------------------------------------------------------------------- /Classes/PomeloClient.h: -------------------------------------------------------------------------------- 1 | #ifndef __POMELO_CLIENT___H__ 2 | #define __POMELO_CLIENT___H__ 3 | #include "pomelo.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | struct EventCbData{ 10 | int ev_type; 11 | void * ex_data; 12 | std::string arg1; 13 | std::string arg2; 14 | }; 15 | struct RequestCbData{ 16 | int rc; 17 | std::string resp; 18 | std::string route; 19 | }; 20 | struct NotifyCbData{ 21 | int rc; 22 | std::string route; 23 | }; 24 | #define PC_EV_CONNECTED_NAME "onConnected" 25 | class PomeloClient{ 26 | public: 27 | static PomeloClient * createClient(); 28 | static void initLib(void (*pc_log)(int level, const char* msg, ...), 29 | void* (*pc_alloc)(size_t len), void (*pc_free)(void* d), const char* platform); 30 | static void cleanupLib(); 31 | private: 32 | static void eventCb(pc_client_t *client, int ev_type, void* ex_data, 33 | const char* arg1, const char* arg2); 34 | static void requestCb(const pc_request_t * req, int rc, const char * resp); 35 | static void notifyCb(const pc_notify_t * req, int rc); 36 | public: 37 | void init(); 38 | void update(float delta); 39 | // client 40 | int connect(const char* host, int port, const char* handshake_opts); 41 | int disconnect(); 42 | void * getExData(); 43 | int getState(); 44 | int getConnQuality(); 45 | void * getTransData(); 46 | void destroy(); 47 | // callback 48 | int requestWithTimeout(const char* route,const char* msg, void* ex_data, 49 | int timeout, std::function cb); 50 | int notifyWithTimeout(const char* route,const char* msg, void* ex_data, 51 | int timeout, std::function cb); 52 | int registerEventCb(const char * route, std::function cb); 53 | private: 54 | pc_client_t * client; 55 | int eventHandler; 56 | static std::mutex eventLock; 57 | std::vector eventCbQueue; 58 | std::vector requestCbQueue; 59 | std::vector notifyCbQueue; 60 | std::map> functionMap; 61 | }; 62 | #endif -------------------------------------------------------------------------------- /Classes/lib/include/pomelo.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 NetEase, Inc. and other Pomelo contributors 3 | * MIT Licensed. 4 | */ 5 | 6 | #ifndef PC_POMELO_H 7 | #define PC_POMELO_H 8 | 9 | #include 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #ifdef _WIN32 16 | # if defined(BUILDING_PC_SHARED) 17 | # define PC_EXPORT __declspec(dllexport) 18 | # else 19 | # define PC_EXPORT 20 | # endif 21 | #else 22 | # define PC_EXPORT 23 | #endif 24 | 25 | #define PC_MAJOR_VERSION 0 26 | #define PC_MINOR_VERSION 3 27 | #define PC_REVISION 5 28 | #define PC_VERSION_SUFFIX "release" 29 | 30 | #define PC_T(x) PC__T(x) 31 | #define PC__T(x) #x 32 | 33 | #define PC_VERSION_NUM (PC_MAJOR_VERSION * 10000 + PC_MINOR_VERSION * 100 + PC_REVISION) 34 | #define PC_VERSION_STR ( PC_T(PC_MAJOR_VERSION) "." PC_T(PC_MINOR_VERSION) \ 35 | "." PC_T(PC_REVISION) "-" PC_VERSION_SUFFIX ) 36 | 37 | /** 38 | * error code 39 | */ 40 | #define PC_RC_OK 0 41 | #define PC_RC_ERROR -1 42 | #define PC_RC_TIMEOUT -2 43 | #define PC_RC_INVALID_JSON -3 44 | #define PC_RC_INVALID_ARG -4 45 | #define PC_RC_NO_TRANS -5 46 | #define PC_RC_INVALID_THREAD -6 47 | #define PC_RC_TRANS_ERROR -7 48 | #define PC_RC_INVALID_ROUTE -8 49 | #define PC_RC_INVALID_STATE -9 50 | #define PC_RC_NOT_FOUND -10 51 | #define PC_RC_RESET -11 52 | #define PC_RC_MIN -12 53 | 54 | typedef struct pc_client_s pc_client_t; 55 | typedef struct pc_request_s pc_request_t; 56 | typedef struct pc_notify_s pc_notify_t; 57 | 58 | /** 59 | * client state 60 | */ 61 | #define PC_ST_NOT_INITED 0 62 | #define PC_ST_INITED 1 63 | #define PC_ST_CONNECTING 2 64 | #define PC_ST_CONNECTED 3 65 | #define PC_ST_DISCONNECTING 4 66 | #define PC_ST_UNKNOWN 5 67 | #define PC_ST_COUNT 6 68 | 69 | 70 | /** 71 | * log level 72 | */ 73 | #define PC_LOG_DEBUG 0 74 | #define PC_LOG_INFO 1 75 | #define PC_LOG_WARN 2 76 | #define PC_LOG_ERROR 3 77 | #define PC_LOG_DISABLE 4 78 | 79 | /** 80 | * some tunable arguments 81 | */ 82 | #define PC_TRANSPORT_PLUGIN_SLOT_COUNT 8 83 | #define PC_PRE_ALLOC_REQUEST_SLOT_COUNT 4 84 | #define PC_PRE_ALLOC_NOTIFY_SLOT_COUNT 4 85 | #define PC_TIMEOUT_CHECK_INTERVAL 2 86 | #define PC_HEARTBEAT_TIMEOUT_FACTOR 2 87 | #define PC_TCP_READ_BUFFER_SIZE (1 << 16) 88 | 89 | /** 90 | * builtin transport name 91 | */ 92 | #define PC_TR_NAME_UV_TCP 0 93 | #define PC_TR_NAME_UV_TLS 1 94 | #define PC_TR_NAME_DUMMY 7 95 | 96 | /** 97 | * reconnect max retry 98 | */ 99 | #define PC_ALWAYS_RETRY -1 100 | 101 | /** 102 | * disable timeout 103 | */ 104 | #define PC_WITHOUT_TIMEOUT -1 105 | 106 | typedef enum { 107 | PC_LOCAL_STORAGE_OP_READ = 0, 108 | PC_LOCAL_STORAGE_OP_WRITE = 1, 109 | } pc_local_storage_op_t; 110 | typedef int (*pc_local_storage_cb_t)(pc_local_storage_op_t op, 111 | char* data, size_t* len, void* ex_data); 112 | 113 | typedef struct { 114 | int conn_timeout; 115 | 116 | int enable_reconn; 117 | int reconn_max_retry; 118 | int reconn_delay; 119 | int reconn_delay_max; 120 | int reconn_exp_backoff; 121 | 122 | int enable_polling; 123 | 124 | pc_local_storage_cb_t local_storage_cb; 125 | void* ls_ex_data; 126 | 127 | int transport_name; 128 | } pc_client_config_t; 129 | 130 | #define PC_CLIENT_CONFIG_DEFAULT \ 131 | { \ 132 | 30, /* conn_timeout */ \ 133 | 1, /* enable_reconn */ \ 134 | PC_ALWAYS_RETRY, /* reconn_max_retry */ \ 135 | 2, /* reconn_delay */ \ 136 | 30, /* reconn_delay_max */ \ 137 | 1, /* reconn_exp_backoff */ \ 138 | 0, /* enable_polling */ \ 139 | NULL, /* local_storage_cb */ \ 140 | NULL, /* ls_ex_data */ \ 141 | PC_TR_NAME_UV_TCP /* transport_name */ \ 142 | } 143 | 144 | PC_EXPORT int pc_lib_version(); 145 | PC_EXPORT const char* pc_lib_version_str(); 146 | 147 | /** 148 | * If you do use default log callback, 149 | * this function will change the level of log out. 150 | * 151 | * Otherwise, this function does nothing. 152 | */ 153 | PC_EXPORT void pc_lib_set_default_log_level(int level); 154 | 155 | /** 156 | * pc_lib_init and pc_lib_cleanup both should be invoked only once. 157 | */ 158 | PC_EXPORT void pc_lib_init(void (*pc_log)(int level, const char* msg, ...), 159 | void* (*pc_alloc)(size_t len), void (*pc_free)(void* d), const char* platform); 160 | PC_EXPORT void pc_lib_cleanup(); 161 | 162 | PC_EXPORT size_t pc_client_size(); 163 | PC_EXPORT int pc_client_init(pc_client_t* client, void* ex_data, const pc_client_config_t* config); 164 | PC_EXPORT int pc_client_connect(pc_client_t* client, const char* host, int port, const char* handshake_opts); 165 | PC_EXPORT int pc_client_disconnect(pc_client_t* client); 166 | PC_EXPORT int pc_client_cleanup(pc_client_t* client); 167 | PC_EXPORT int pc_client_poll(pc_client_t* client); 168 | 169 | /** 170 | * pc_client_t getters 171 | */ 172 | PC_EXPORT void* pc_client_ex_data(const pc_client_t* client); 173 | PC_EXPORT const pc_client_config_t* pc_client_config(const pc_client_t* client); 174 | PC_EXPORT int pc_client_state(pc_client_t* client); 175 | PC_EXPORT int pc_client_conn_quality(pc_client_t* client); 176 | PC_EXPORT void* pc_client_trans_data(pc_client_t* client); 177 | 178 | /** 179 | * Event 180 | */ 181 | 182 | /** 183 | * event handler callback and event types 184 | * 185 | * arg1 and arg2 are significant for the following events: 186 | * PC_EV_USER_DEFINED_PUSH - arg1 as push route, arg2 as push msg 187 | * PC_EV_CONNECT_ERROR - arg1 as short error description 188 | * PC_EV_CONNECT_FAILED - arg1 as short reason description 189 | * PC_EV_UNEXPECTED_DISCONNECT - arg1 as short reason description 190 | * PC_EV_PROTO_ERROR - arg1 as short reason description 191 | * 192 | * For other events, arg1 and arg2 will be set to NULL. 193 | */ 194 | typedef void (*pc_event_cb_t)(pc_client_t *client, int ev_type, void* ex_data, 195 | const char* arg1, const char* arg2); 196 | #define PC_EV_USER_DEFINED_PUSH 0 197 | #define PC_EV_CONNECTED 1 198 | #define PC_EV_CONNECT_ERROR 2 199 | #define PC_EV_CONNECT_FAILED 3 200 | #define PC_EV_DISCONNECT 4 201 | #define PC_EV_KICKED_BY_SERVER 5 202 | #define PC_EV_UNEXPECTED_DISCONNECT 6 203 | #define PC_EV_PROTO_ERROR 7 204 | #define PC_EV_COUNT 8 205 | 206 | #define PC_EV_INVALID_HANDLER_ID -1 207 | 208 | PC_EXPORT int pc_client_add_ev_handler(pc_client_t* client, pc_event_cb_t cb, 209 | void* ex_data, void (*destructor)(void* ex_data)); 210 | PC_EXPORT int pc_client_rm_ev_handler(pc_client_t* client, int id); 211 | 212 | /** 213 | * Request 214 | */ 215 | 216 | typedef void (*pc_request_cb_t)(const pc_request_t* req, int rc, const char* resp); 217 | 218 | /** 219 | * pc_request_t getters. 220 | * 221 | * All the getters should be called in pc_request_cb_t to access read-only 222 | * properties of the current pc_request_t. 223 | * 224 | * User should not hold any references to pc_request_t. 225 | */ 226 | PC_EXPORT pc_client_t* pc_request_client(const pc_request_t* req); 227 | PC_EXPORT const char* pc_request_route(const pc_request_t* req); 228 | PC_EXPORT const char* pc_request_msg(const pc_request_t* req); 229 | PC_EXPORT int pc_request_timeout(const pc_request_t* req); 230 | PC_EXPORT void* pc_request_ex_data(const pc_request_t* req); 231 | 232 | /** 233 | * Initiate a request. 234 | */ 235 | PC_EXPORT int pc_request_with_timeout(pc_client_t* client, const char* route, 236 | const char* msg, void* ex_data, int timeout, pc_request_cb_t cb); 237 | 238 | /** 239 | * Notify 240 | */ 241 | 242 | typedef void (*pc_notify_cb_t)(const pc_notify_t* req, int rc); 243 | 244 | /** 245 | * pc_notify_t getters. 246 | * 247 | * All the getters should be called in pc_notify_cb_t to access read-only 248 | * properties of the current pc_notify_t. 249 | * 250 | * User should not hold any references to pc_notify_t. 251 | */ 252 | PC_EXPORT pc_client_t* pc_notify_client(const pc_notify_t* notify); 253 | PC_EXPORT const char* pc_notify_route(const pc_notify_t* notify); 254 | PC_EXPORT const char* pc_notify_msg(const pc_notify_t* notify); 255 | PC_EXPORT int pc_notify_timeout(const pc_notify_t* notify); 256 | PC_EXPORT void* pc_notify_ex_data(const pc_notify_t* notify); 257 | 258 | /** 259 | * Initiate a notify. 260 | */ 261 | PC_EXPORT int pc_notify_with_timeout(pc_client_t* client, const char* route, 262 | const char* msg, void* ex_data, int timeout, pc_notify_cb_t cb); 263 | 264 | /** 265 | * Utilities 266 | */ 267 | PC_EXPORT const char* pc_client_state_str(int state); 268 | PC_EXPORT const char* pc_client_ev_str(int ev_type); 269 | PC_EXPORT const char* pc_client_rc_str(int rc); 270 | 271 | /** 272 | * set ca file for tls transports 273 | */ 274 | 275 | #if !defined(PC_NO_UV_TCP_TRANS) && !defined(PC_NO_UV_TLS_TRANS) 276 | 277 | void tr_uv_tls_set_ca_file(const char* ca_file, const char* ca_path); 278 | 279 | #endif /* uv_tls */ 280 | 281 | /** 282 | * Macro implementation 283 | */ 284 | #define pc_lib_version() PC_VERSION_NUM 285 | #define pc_lib_version_str() PC_VERSION_STR 286 | 287 | #ifdef __cplusplus 288 | } 289 | #endif 290 | 291 | #endif /* PC_POMELO_H */ 292 | -------------------------------------------------------------------------------- /Classes/lib/include/pomelo_trans.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 NetEase, Inc. and other Pomelo contributors 3 | * MIT Licensed. 4 | */ 5 | 6 | #ifndef PC_POMELO_TRANS_H 7 | #define PC_POMELO_TRANS_H 8 | 9 | #include 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | typedef struct pc_transport_s pc_transport_t; 16 | typedef struct pc_transport_plugin_s pc_transport_plugin_t; 17 | 18 | /** 19 | * special request id 20 | */ 21 | #define PC_NOTIFY_PUSH_REQ_ID ((unsigned int)0) 22 | #define PC_INVALID_REQ_ID ((unsigned int)-1) 23 | 24 | struct pc_transport_s { 25 | int (*init)(pc_transport_t* trans, pc_client_t* client); 26 | int (*connect)(pc_transport_t* trans, const char* host, int port, const char* handshake_opt); 27 | 28 | /** 29 | * req_id == PC_NOTIFY_PUSH_REQ_ID indicates that it is a notify message, 30 | * otherwise it is a request message 31 | */ 32 | int (*send)(pc_transport_t* trans, const char* route, unsigned int seq_num, 33 | const char* msg, unsigned int req_id, int timeout); 34 | 35 | int (*disconnect)(pc_transport_t* trans); 36 | int (*cleanup)(pc_transport_t* trans); 37 | 38 | void* (*internal_data)(pc_transport_t* trans); /* optional */ 39 | int (*quality)(pc_transport_t* trans); /* optional */ 40 | pc_transport_plugin_t* (*plugin)(pc_transport_t* trans); 41 | }; 42 | 43 | struct pc_transport_plugin_s { 44 | pc_transport_t* (*transport_create)(pc_transport_plugin_t* plugin); 45 | void (*transport_release)(pc_transport_plugin_t* plugin, pc_transport_t* trans); 46 | 47 | void (*on_register)(pc_transport_plugin_t* plugin); /* optional */ 48 | void (*on_deregister)(pc_transport_plugin_t* plugin); /* optional */ 49 | 50 | int transport_name; 51 | }; 52 | 53 | PC_EXPORT int pc_transport_plugin_register(pc_transport_plugin_t* plugin); 54 | PC_EXPORT int pc_transport_plugin_deregister(int trans_name); 55 | 56 | /** 57 | * when net work event occures, transport impl should invoke this function. 58 | */ 59 | PC_EXPORT void pc_trans_fire_event(pc_client_t* client, int ev_type, const char* arg1, const char* arg2); 60 | 61 | /** 62 | * when a notify is sent or timeout, transport impl should invoke this function. 63 | */ 64 | PC_EXPORT void pc_trans_sent(pc_client_t* client, unsigned int seq_num, int rc); 65 | 66 | /** 67 | * when a request gets a resp or timeout, transport impl should invoke this function. 68 | */ 69 | PC_EXPORT void pc_trans_resp(pc_client_t* client, unsigned int req_id, int rc, const char* resp); 70 | 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif /* PC_POMELO_TRANS_H */ 77 | -------------------------------------------------------------------------------- /Classes/lib/libpomelo2.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Classes/lib/libpomelo2.lib -------------------------------------------------------------------------------- /Classes/lib/libuv.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Classes/lib/libuv.lib -------------------------------------------------------------------------------- /Classes/lib/openssl.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Classes/lib/openssl.lib -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 焱 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pomeloclientcpp 2 | pomelo的cocos2dx客户端的C++版本,使用新版的libpomelo2 3 | cocos2dx 版本为v3.10 4 | 客户端平台: win7 5 | 服务端系统: ubuntu 14.01 (在win7配置麻烦) 6 | 7 | # 运行方式 8 | ## 服务端 9 | 采用的是[官方教程](https://github.com/NetEase/pomelo/wiki/chat%E6%BA%90%E7%A0%81%E4%B8%8B%E8%BD%BD%E4%B8%8E%E5%AE%89%E8%A3%85)里的例子。 10 | 11 | $ git clone https://github.com/NetEase/chatofpomelo-websocket.git 12 | $ git checkout tutorial-starter 13 | 14 | ## 客户端 15 | 1. 根据[libpomelo2](https://github.com/NetEase/libpomelo2)生成```libpomelo2.lib```,```libuv.lib```,```openssl.lib```这三个静态库,将其导入到 16 | cocos2dx工程,这三个文件放在了```Classes/lib```,头文件在```Classes/lib/include```。静态库如何导入,详情百度,或根据自己需要导入工程。 17 | 18 | 2. 新建cocos2dx C++工程,替换 Classes和Resources这两个文件夹 19 | 20 | 3. 修改客户端代码中写死的IP地址,将```192.168.0.14```替换成服务端实际所在的IP 21 | 22 | 然后可以编译运行。 23 | -------------------------------------------------------------------------------- /Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/CloseNormal.png -------------------------------------------------------------------------------- /Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/CloseSelected.png -------------------------------------------------------------------------------- /Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/HelloWorld.png -------------------------------------------------------------------------------- /Resources/animationbuttonnormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/animationbuttonnormal.png -------------------------------------------------------------------------------- /Resources/animationbuttonpressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/animationbuttonpressed.png -------------------------------------------------------------------------------- /Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /Resources/fonts/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/fonts/arial.ttf -------------------------------------------------------------------------------- /Resources/green_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/green_edit.png -------------------------------------------------------------------------------- /Resources/orange_edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/orange_edit.png -------------------------------------------------------------------------------- /Resources/res/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/isghost/pomeloclientcpp/2aa6d89c210f45d499599fa1c9e6c1e9126d357a/Resources/res/.gitkeep -------------------------------------------------------------------------------- /常见问题.md: -------------------------------------------------------------------------------- 1 | 关于vs开发cocos2dx3.0使用pomelo的一些注意事项 2 | 3 | ## libpomelo2 4 | 5 | 网易的libpomelo 更新成了 libpomelo2 ,所以下载新的libpomelo2 6 | (注意不要用旧的,会出现问题,libpomelo2 相比libpomelo 有了很大的改变) 7 | 下载网址 8 | https://github.com/NetEase/libpomelo2 9 | 10 | 讨论区 11 | http://nodejs.netease.com/topic/540702e0898634292cd0a588 12 | 13 | ![](http://i.imgur.com/fYKeHlu.png) 14 | 15 | ## vs2015 编译libpomelo2 16 | 17 | * 建议使用vs2015免费的社区版,用vs2013编译的时候出现v14.0错误,还有各种未解析的符号错误等 18 | 19 | * gyp下载 20 | 有人提示去下载(下面的网址根本是没有东西的) 21 | https://github.com/martine/gyp 22 | 23 | 24 | 当然libpomelo2给的文档如下(是需要翻墙的) 25 | 26 | ``` 27 | git clone https://chromium.googlesource.com/external/gyp 28 | cd gyp 29 | python setup.py install 30 | ``` 31 | 32 | 我是翻墙下载的,如下 33 | ![](http://i.imgur.com/Rex91Ct.jpg) 34 | 35 | 可以将文件拷贝到libpomelo2目录下 36 | 37 | ![](http://i.imgur.com/BBNIGpx.png) 38 | 39 | 40 | ## 编译生成.lib文件 41 | 42 | 还是使用vs2015 43 | 44 | 使用的命令 45 | ``` 46 | gyp.bat --depth=. ../pomelo.gyp -Dlibrary=static_library -DTO=pc​​ 47 | ``` 48 | 或改进如下 49 | ``` 50 | gyp.bat --depth=. ../pomelo.gyp -Duse_sys_openssl=false -Dlibrary=static_library -DTO=pc 51 | ``` 52 | 会出现 sln 53 | ![](http://i.imgur.com/6n9c2I2.png) 54 | 55 | 56 | 随后vs2015打开对象目进行重新生成,会生成如下的三个lib(有错误,自己google) 57 | ![](http://i.imgur.com/K8YL0fP.png) 58 | 59 | 60 | 参考如下网址 61 | http://blog.sina.com.cn/s/blog_537283200102w0ca.html 62 | 63 | 64 | # cocos2dx 客户端项目 65 | 66 | 67 | #### cocos2dx环境 68 | 69 | * ccocos2dx v3.10 70 | * vs2015社区版 71 | 72 | #### 73 | 74 | * fork 如下地址的项目 75 | https://github.com/isghost/pomeloclientcpp 76 | 77 | * 自己新建立的项目,引入库文件,头文件(vs如何引入,自己google) 78 | ![](http://i.imgur.com/qoEhRxV.png) 79 | 80 | * copy原项目的Classes 和 Resources 到自己的项目中,编译会可能会出现如下的问题 81 | ![](http://i.imgur.com/ZbQqYyK.png) 82 | 83 | 84 | 解决办法是在PomeloClient.h中添加如下几个 #paragma comment() 85 | 86 | ```` 87 | #ifndef __POMELO_CLIENT___H__ 88 | #define __POMELO_CLIENT___H__ 89 | #include "pomelo.h" 90 | #include 91 | #include 92 | #include 93 | #include 94 | #include 95 | 96 | #pragma comment(lib, "ws2_32.lib") 97 | #pragma comment(lib, "libuv.lib") 98 | #pragma comment(lib, "IPHLPAPI.lib") 99 | #pragma comment(lib, "Psapi.lib") 100 | ```` 101 | 102 | 这里参考如下 103 | http://nodejs.netease.com/topic/52ba9e68b5bc7de2050b3455 104 | 105 | 106 | 上面的步骤都没问题的话,客户端应该可以正常运行, 107 | 在clone 官方的chatofpomelo-websocket 例子 108 | https://github.com/NetEase/chatofpomelo-websocket.git 109 | 110 | 运行服务端,注意node版本不能太低 111 | 我的服务端环境是 112 | * ubuntu14.04 32bit 113 | * node5.8.0 114 | 115 | # 运行截图 116 | ![](http://i.imgur.com/0AkG4D0.png) 117 | 118 | ![](http://i.imgur.com/JxgwQsr.png) 119 | 120 | ![](http://i.imgur.com/WLK0MkV.png) 121 | 122 | --- --------------------------------------------------------------------------------