diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index 08a1ca7e10..099d30edad 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -233,10 +233,6 @@ public: * @return true if the assert was handled by the script engine, false otherwise. */ virtual bool handleAssert(const char *msg) = 0; - /** - function for commen event ,param have lua table - **/ - virtual int executeEventByTable(int nHandler,const unsigned char* pTable,int nLength,const char * pEventName,CCObject* pEventSource = NULL,const char* pEventSourceClassName = NULL){ return 0 ; } }; /** diff --git a/extensions/network/WebSocket.cpp b/extensions/network/WebSocket.cpp index 890036b9e8..a105703073 100644 --- a/extensions/network/WebSocket.cpp +++ b/extensions/network/WebSocket.cpp @@ -324,7 +324,6 @@ bool WebSocket::init(const Delegate& delegate, _wsProtocols[0].name = name; _wsProtocols[0].callback = WebSocketCallbackWrapper::onSocketCallback; } - m_mapScriptHandler.clear(); // WebSocket thread needs to be invoked at the end of this method. _wsHelper = new WsThreadHelper(); @@ -655,34 +654,4 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg) break; } } - - -void WebSocket::registerScriptHandler(int nFunID,webSocketScriptHandlerType scriptHandlerType) -{ - m_mapScriptHandler[scriptHandlerType] = nFunID; -} - -void WebSocket::unregisterScriptHandler(webSocketScriptHandlerType scriptHandlerType) -{ - std::map::iterator Iter = m_mapScriptHandler.find(scriptHandlerType); - - if (m_mapScriptHandler.end() != Iter) - { - m_mapScriptHandler.erase(Iter); - } -} - -/** - * @brief Get Handler By DelegateEvent Type - */ -int WebSocket::getScriptHandler(webSocketScriptHandlerType scriptHandlerType) -{ - std::map::iterator Iter = m_mapScriptHandler.find(scriptHandlerType); - - if (m_mapScriptHandler.end() != Iter) - return Iter->second; - - return -1; -} - NS_CC_EXT_END diff --git a/extensions/network/WebSocket.h b/extensions/network/WebSocket.h index e551a64c1f..0d58152bb9 100644 --- a/extensions/network/WebSocket.h +++ b/extensions/network/WebSocket.h @@ -119,30 +119,6 @@ public: * @brief Gets current state of connection. */ State getReadyState(); - - /* - * @brief delegate event enum,for lua register handler - */ - enum webSocketScriptHandlerType - { - kwebSocketScriptHandlerOpen = 0, - kwebSocketScriptHandlerMessage, - kwebSocketScriptHandlerClose, - kwebSocketScriptHandlerError, - }; - /** - * @brief Add Handler of DelegateEvent - */ - void registerScriptHandler(int nFunID,webSocketScriptHandlerType scriptHandlerType); - /** - * @brief Remove Handler of DelegateEvent - */ - void unregisterScriptHandler(webSocketScriptHandlerType scriptHandlerType); - /** - * @brief Get Handler By DelegateEvent Type - */ - int getScriptHandler(webSocketScriptHandlerType scriptHandlerType); - private: virtual void onSubThreadStarted(); virtual int onSubThreadLoop(); @@ -170,7 +146,6 @@ private: Delegate* _delegate; int _SSLConnection; libwebsocket_protocols* _wsProtocols; - std::map m_mapScriptHandler; }; NS_CC_EXT_END diff --git a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua index bd0042dfec..7e0c8fede0 100644 --- a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/ExtensionTest.lua @@ -9,7 +9,7 @@ local ExtensionTestEnum = TEST_NOTIFICATIONCENTER = 0, TEST_CCCONTROLBUTTON = 1, TEST_COCOSBUILDER = 2, - TEST_HTTPCLIENT = 3, + TEST_WEBSOCKET = 3, --TRAGET_PLATFORM TEST_EDITBOX = 4, TEST_TABLEVIEW = 5, @@ -1029,11 +1029,19 @@ local function ExtensionsMainLayer() menu:setPosition(CCPointMake(0, 0)) CCMenuItemFont:setFontName("Arial") CCMenuItemFont:setFontSize(24) + local targetPlatform = CCApplication:sharedApplication():getTargetPlatform() + local bSupportWebSocket = true + if (kTargetIphone ~= targetPlatform) and (kTargetIpad ~= targetPlatform) and (kTargetAndroid ~= targetPlatform) and (kTargetWindows ~= targetPlatform) then + bSupportWebSocket = false + end for i = 1, ExtensionTestEnum.TEST_MAX_COUNT do local item = CCMenuItemFont:create(testsName[i]) item:registerScriptTapHandler(menuCallback) item:setPosition(s.width / 2, s.height - i * LINE_SPACE) menu:addChild(item, kItemTagBasic + i) + if (i == ExtensionTestEnum.TEST_WEBSOCKET + 1) and false == bSupportWebSocket then + item:setEnabled(false) + end end layer:addChild(menu) diff --git a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/WebProxyTest.lua b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/WebProxyTest.lua index 9f0f96da0d..00e9a97e29 100644 --- a/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/WebProxyTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/ExtensionTest/WebProxyTest.lua @@ -28,7 +28,7 @@ if nil ~= wsSendText then if kStateOpen == wsSendText:getReadyState() then sendTextStatus:setString("Send Text WS is waiting...") - wsSendText:sendStrMsg("Hello WebSocket中文, I'm a text message.") + wsSendText:sendTextMsg("Hello WebSocket中文, I'm a text message.") else local warningStr = "send text websocket instance wasn't ready..." print(warningStr) @@ -116,7 +116,7 @@ sendBinaryStatus:setString("Send Binary WS was opened.") end - local function wsSendBinaryMessage(paramTable,strEventName) + local function wsSendBinaryMessage(paramTable) local length = table.getn(paramTable) local i = 1 local strInfo = "response bin msg: " diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp index 1dc6b41522..35220deb2a 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp @@ -331,27 +331,4 @@ int CCLuaEngine::reallocateScriptHandler(int nHandler) return nRet; } -int CCLuaEngine::executeEventByTable(int nHandler,const unsigned char* pTable,int nLength,const char * pEventName,CCObject* pEventSource,const char* pEventSourceClassName) -{ - if (NULL == pTable) { - return 0; - } - int nRet = 0; - CCLuaValueArray array; - for (int i = 0 ; i < nLength; i++) { - CCLuaValue value = CCLuaValue::intValue(pTable[i]); - array.push_back(value); - } - - m_stack->pushCCLuaValueArray(array); - m_stack->pushString(pEventName); - if (pEventSource) - { - m_stack->pushCCObject(pEventSource, pEventSourceClassName ? pEventSourceClassName : "CCObject"); - } - nRet = m_stack->executeFunctionByHandler(nHandler, pEventSource ? 3 : 2); - m_stack->clean(); - return nRet; -} - NS_CC_END diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.h b/scripting/lua/cocos2dx_support/CCLuaEngine.h index 1e24476c1e..e671c9fece 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.h +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.h @@ -117,8 +117,6 @@ public: virtual int executeEvent(int nHandler, const char* pEventName, CCObject* pEventSource = NULL, const char* pEventSourceClassName = NULL); virtual bool handleAssert(const char *msg); - /*d*/ - virtual int executeEventByTable(int nHandler,const unsigned char* pTable,int nLength,const char * pEventName,CCObject* pEventSource = NULL,const char* pEventSourceClassName = NULL); private: CCLuaEngine(void) diff --git a/scripting/lua/cocos2dx_support/Lua_web_socket.cpp b/scripting/lua/cocos2dx_support/Lua_web_socket.cpp index 7abb7fae3d..975b1c3225 100644 --- a/scripting/lua/cocos2dx_support/Lua_web_socket.cpp +++ b/scripting/lua/cocos2dx_support/Lua_web_socket.cpp @@ -11,18 +11,102 @@ extern "C" { #include "Lua_web_socket.h" #include "cocos2d.h" #include "WebSocket.h" +#include "CCLuaStack.h" +#include "CCLuaValue.h" +#include "CCLuaEngine.h" using namespace cocos2d; using namespace cocos2d::extension; -class LuaWebSocketDelegate : public WebSocket::Delegate + +static int SendBinaryMessageToLua(int nHandler,const unsigned char* pTable,int nLength) +{ + if (NULL == pTable || nHandler <= 0) { + return 0; + } + + if (NULL == CCScriptEngineManager::sharedManager()->getScriptEngine()) { + return 0; + } + + CCLuaStack *pStack = CCLuaEngine::defaultEngine()->getLuaStack(); + if (NULL == pStack) { + return 0; + } + + lua_State *tolua_s = pStack->getLuaState(); + if (NULL == tolua_s) { + return 0; + } + + int nRet = 0; + CCLuaValueArray array; + for (int i = 0 ; i < nLength; i++) { + CCLuaValue value = CCLuaValue::intValue(pTable[i]); + array.push_back(value); + } + + pStack->pushCCLuaValueArray(array); + nRet = pStack->executeFunctionByHandler(nHandler, 1); + pStack->clean(); + return nRet; +} + +class CLuaWebSocket: public WebSocket,public WebSocket::Delegate { public: + /* + * @brief delegate event enum,for lua register handler + */ + enum webSocketScriptHandlerType + { + kwebSocketScriptHandlerOpen = 0, + kwebSocketScriptHandlerMessage, + kwebSocketScriptHandlerClose, + kwebSocketScriptHandlerError, + }; + /** + * @brief Add Handler of DelegateEvent + */ + void registerScriptHandler(int nFunID,webSocketScriptHandlerType scriptHandlerType) + { + m_mapScriptHandler[scriptHandlerType] = nFunID; + } + /** + * @brief Remove Handler of DelegateEvent + */ + void unregisterScriptHandler(webSocketScriptHandlerType scriptHandlerType) + { + std::map::iterator Iter = m_mapScriptHandler.find(scriptHandlerType); + + if (m_mapScriptHandler.end() != Iter) + { + m_mapScriptHandler.erase(Iter); + } + } + /** + * @brief Get Handler By DelegateEvent Type + */ + int getScriptHandler(webSocketScriptHandlerType scriptHandlerType) + { + std::map::iterator Iter = m_mapScriptHandler.find(scriptHandlerType); + + if (m_mapScriptHandler.end() != Iter) + return Iter->second; + + return -1; + } + + void InitScriptHandleMap() + { + m_mapScriptHandler.clear(); + } virtual void onOpen(WebSocket* ws) { - if (NULL != ws) { - int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerOpen); + CLuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = luaWs->getScriptHandler(CLuaWebSocket::kwebSocketScriptHandlerOpen); if (-1 != nHandler) { CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,""); } @@ -31,25 +115,29 @@ public: virtual void onMessage(WebSocket* ws, const WebSocket::Data& data) { - if (data.isBinary) { - int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerMessage); - if (-1 != nHandler) { - CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEventByTable(nHandler, (const unsigned char*)data.bytes, data.len, ""); + CLuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + if (data.isBinary) { + int nHandler = luaWs->getScriptHandler(CLuaWebSocket::kwebSocketScriptHandlerMessage); + if (-1 != nHandler) { + SendBinaryMessageToLua(nHandler, (const unsigned char*)data.bytes, data.len); + } } - } - else{ - - int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerMessage); - if (-1 != nHandler) { - CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,data.bytes); + else{ + + int nHandler = luaWs->getScriptHandler(CLuaWebSocket::kwebSocketScriptHandlerMessage); + if (-1 != nHandler) { + CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,data.bytes); + } } } } virtual void onClose(WebSocket* ws) { - if (NULL != ws) { - int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerClose); + CLuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = luaWs->getScriptHandler(CLuaWebSocket::kwebSocketScriptHandlerClose); if (-1 != nHandler) { CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,""); } @@ -58,19 +146,24 @@ public: virtual void onError(WebSocket* ws, const WebSocket::ErrorCode& error) { - if (NULL != ws) { - int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerError); + CLuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = luaWs->getScriptHandler(CLuaWebSocket::kwebSocketScriptHandlerError); if (-1 != nHandler) { CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,""); } } } + + +private: + std::map m_mapScriptHandler; }; #ifdef __cplusplus static int tolua_collect_WebSocket (lua_State* tolua_S) { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); Mtolua_delete(self); return 0; } @@ -97,9 +190,8 @@ static int tolua_Cocos2d_WebSocket_create00(lua_State* tolua_S) #endif { const char* urlName = ((const char*) tolua_tostring(tolua_S,2,0)); - WebSocket *wSocket = new WebSocket(); - LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate(); - wSocket->init(*delegate, urlName); + CLuaWebSocket *wSocket = new CLuaWebSocket(); + wSocket->init(*wSocket, urlName); tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket"); } return 1; @@ -131,9 +223,8 @@ static int tolua_Cocos2d_WebSocket_createByAProtocol00(lua_State* tolua_S) const char *protocol = ((const char*) tolua_tostring(tolua_S,3,0)); std::vector protocols; protocols.push_back(protocol); - WebSocket *wSocket = new WebSocket(); - LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate(); - wSocket->init(*delegate, urlName,&protocols); + CLuaWebSocket *wSocket = new CLuaWebSocket(); + wSocket->init(*wSocket, urlName,&protocols); tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket"); } return 1; @@ -174,9 +265,8 @@ static int tolua_Cocos2d_WebSocket_createByProtocolArray00(lua_State* tolua_S) } } } - WebSocket *wSocket = new WebSocket(); - LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate(); - wSocket->init(*delegate, urlName,&protocols); + CLuaWebSocket *wSocket = new CLuaWebSocket(); + wSocket->init(*wSocket, urlName,&protocols); tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket"); } return 1; @@ -202,7 +292,7 @@ static int tolua_Cocos2d_WebSocket_getReadyState00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket *self = (CLuaWebSocket*)tolua_tousertype(tolua_S,1,0); int tolua_ret = -1; if (NULL != self) { tolua_ret = self->getReadyState(); @@ -218,9 +308,9 @@ tolua_lerror: } #endif //#ifndef TOLUA_DISABLE -/* method: sendStrMsg of class WebSocket */ -#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_sendStrMsg00 -static int tolua_Cocos2d_WebSocket_sendStrMsg00(lua_State* tolua_S) +/* method: sendTextMsg of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_sendTextMsg00 +static int tolua_Cocos2d_WebSocket_sendTextMsg00(lua_State* tolua_S) { #ifndef TOLUA_RELEASE tolua_Error tolua_err; @@ -233,7 +323,7 @@ static int tolua_Cocos2d_WebSocket_sendStrMsg00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); const char *pData = ((const char*) tolua_tostring(tolua_S,2,0)); if (NULL != self && NULL != pData && strlen(pData) > 0) { std::string strData = pData; @@ -263,7 +353,7 @@ static int tolua_Cocos2d_WebSocket_close00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); if (NULL != self ) { self->close(); } @@ -293,10 +383,10 @@ static int tolua_Cocos2d_WebSocket_registerScriptHandler00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); if (NULL != self ) { int nFunID = ( toluafix_ref_function(tolua_S,2,0)); - WebSocket::webSocketScriptHandlerType handlerType = ((WebSocket::webSocketScriptHandlerType) (int) tolua_tonumber(tolua_S,3,0)); + CLuaWebSocket::webSocketScriptHandlerType handlerType = ((CLuaWebSocket::webSocketScriptHandlerType) (int) tolua_tonumber(tolua_S,3,0)); self->registerScriptHandler(nFunID, handlerType); } } @@ -324,9 +414,9 @@ static int tolua_Cocos2d_WebSocket_unregisterScriptHandler00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); if (NULL != self ) { - WebSocket::webSocketScriptHandlerType handlerType = ((WebSocket::webSocketScriptHandlerType) (int) tolua_tonumber(tolua_S,2,0)); + CLuaWebSocket::webSocketScriptHandlerType handlerType = ((CLuaWebSocket::webSocketScriptHandlerType) (int) tolua_tonumber(tolua_S,2,0)); self->unregisterScriptHandler(handlerType); } } @@ -355,7 +445,7 @@ static int tolua_Cocos2d_WebSocket_sendBinaryMsg00(lua_State* tolua_S) else #endif { - WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0); + CLuaWebSocket* self = (CLuaWebSocket*) tolua_tousertype(tolua_S,1,0); int nLength = lua_tonumber(tolua_S, 3); if (NULL != self && nLength > 0) { @@ -384,10 +474,10 @@ TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){ tolua_constant(tolua_S,"kStateOpen",WebSocket::kStateOpen); tolua_constant(tolua_S,"kStateClosing",WebSocket::kStateClosing); tolua_constant(tolua_S,"kStateClosed",WebSocket::kStateClosed); - tolua_constant(tolua_S,"kwebSocketScriptHandlerOpen",WebSocket::kwebSocketScriptHandlerOpen); - tolua_constant(tolua_S,"kwebSocketScriptHandlerMessage",WebSocket::kwebSocketScriptHandlerMessage); - tolua_constant(tolua_S,"kwebSocketScriptHandlerClose",WebSocket::kwebSocketScriptHandlerClose); - tolua_constant(tolua_S,"kwebSocketScriptHandlerError",WebSocket::kwebSocketScriptHandlerError); + tolua_constant(tolua_S,"kwebSocketScriptHandlerOpen",CLuaWebSocket::kwebSocketScriptHandlerOpen); + tolua_constant(tolua_S,"kwebSocketScriptHandlerMessage",CLuaWebSocket::kwebSocketScriptHandlerMessage); + tolua_constant(tolua_S,"kwebSocketScriptHandlerClose",CLuaWebSocket::kwebSocketScriptHandlerClose); + tolua_constant(tolua_S,"kwebSocketScriptHandlerError",CLuaWebSocket::kwebSocketScriptHandlerError); #ifdef __cplusplus tolua_cclass(tolua_S,"WebSocket","WebSocket","",tolua_collect_WebSocket); #else @@ -398,7 +488,7 @@ TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){ tolua_function(tolua_S, "createByAProtocol", tolua_Cocos2d_WebSocket_createByAProtocol00); tolua_function(tolua_S, "createByProtocolArray", tolua_Cocos2d_WebSocket_createByProtocolArray00); tolua_function(tolua_S, "getReadyState", tolua_Cocos2d_WebSocket_getReadyState00); - tolua_function(tolua_S, "sendStrMsg", tolua_Cocos2d_WebSocket_sendStrMsg00); + tolua_function(tolua_S, "sendTextMsg", tolua_Cocos2d_WebSocket_sendTextMsg00); tolua_function(tolua_S, "close", tolua_Cocos2d_WebSocket_close00); tolua_function(tolua_S, "registerScriptHandler", tolua_Cocos2d_WebSocket_registerScriptHandler00); tolua_function(tolua_S, "unregisterScriptHandler", tolua_Cocos2d_WebSocket_unregisterScriptHandler00);