issue #2103 add webSocket lua test sample

This commit is contained in:
samuele3hu 2013-06-06 22:29:02 +08:00
parent 271d1fa55a
commit e4ec801363
11 changed files with 733 additions and 5 deletions

View File

@ -233,6 +233,10 @@ 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 ; }
};
/**

View File

@ -324,7 +324,7 @@ 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();
@ -656,4 +656,33 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
}
}
void WebSocket::registerScriptHandler(int nFunID,webSocketScriptHandlerType scriptHandlerType)
{
m_mapScriptHandler[scriptHandlerType] = nFunID;
}
void WebSocket::unregisterScriptHandler(webSocketScriptHandlerType scriptHandlerType)
{
std::map<int,int>::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<int,int>::iterator Iter = m_mapScriptHandler.find(scriptHandlerType);
if (m_mapScriptHandler.end() != Iter)
return Iter->second;
return -1;
}
NS_CC_EXT_END

View File

@ -120,6 +120,29 @@ public:
*/
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();
@ -147,6 +170,7 @@ private:
Delegate* _delegate;
int _SSLConnection;
libwebsocket_protocols* _wsProtocols;
std::map<int,int> m_mapScriptHandler;
};
NS_CC_EXT_END

View File

@ -4,6 +4,7 @@
#include "CCLuaEngine.h"
#include "SimpleAudioEngine.h"
#include "Lua_extensions_CCB.h"
#include "Lua_web_socket.h"
using namespace CocosDenshion;
@ -38,6 +39,10 @@ bool AppDelegate::applicationDidFinishLaunching()
lua_State *tolua_s = pStack->getLuaState();
tolua_extensions_ccb_open(tolua_s);
pStack = pEngine->getLuaStack();
tolua_s = pStack->getLuaState();
tolua_web_socket_open(tolua_s);
std::vector<std::string> searchPaths;
searchPaths.push_back("cocosbuilderRes");

View File

@ -1,4 +1,5 @@
require "luaScript/ExtensionTest/CocosBuilderTest"
require "luaScript/ExtensionTest/WebProxyTest"
local LINE_SPACE = 40
local kItemTagBasic = 1000
@ -20,7 +21,7 @@ local testsName =
"NotificationCenterTest",
"CCControlButtonTest",
"CocosBuilderTest",
"HttpClientTest",
"WebSocketTest",
"EditBoxTest",
"TableViewTest",
}
@ -1000,7 +1001,7 @@ local CreateExtensionsTestTable =
runNotificationCenterTest,
runCCControlTest,
runCocosBuilder,
runHttpClientTest,
runWebSocketTest,
runEditBoxTest,
runTableViewTest,
}
@ -1010,7 +1011,6 @@ local s = CCDirector:sharedDirector():getWinSize()
local function ExtensionsMainLayer()
local function CreateExtensionsTestScene(nPerformanceNo)
print(nPerformanceNo)
local pNewscene = CreateExtensionsTestTable[nPerformanceNo]()
return pNewscene
end

View File

@ -0,0 +1,218 @@
local function WebSocketTestLayer()
local layer = CCLayer:create()
local winSize = CCDirector:sharedDirector():getWinSize()
local MARGIN = 40
local SPACE = 35
local wsSendText = nil
local wsSendBinary = nil
local wsError = nil
local sendTextStatus = nil
local sendBinaryStatus = nil
local errorStatus = nil
local receiveTextTimes = 0
local receiveBinaryTimes = 0
local label = CCLabelTTF:create("WebSocket Test", "Arial", 28)
label:setPosition(ccp( winSize.width / 2, winSize.height - MARGIN))
layer:addChild(label, 0)
local menuRequest = CCMenu:create()
menuRequest:setPosition(ccp(0, 0))
layer:addChild(menuRequest)
--Send Text
local function onMenuSendTextClicked()
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.")
else
local warningStr = "send text websocket instance wasn't ready..."
print(warningStr)
sendTextStatus:setString(warningStr)
end
end
end
local labelSendText = CCLabelTTF:create("Send Text", "Arial", 22)
local itemSendText = CCMenuItemLabel:create(labelSendText)
itemSendText:registerScriptTapHandler(onMenuSendTextClicked)
itemSendText:setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - SPACE))
menuRequest:addChild(itemSendText)
--Send Binary
local function onMenuSendBinaryClicked()
if nil ~= wsSendBinary then
if kStateOpen == wsSendBinary:getReadyState() then
sendBinaryStatus:setString("Send Binary WS is waiting...")
local buf = "Hello WebSocket中文--,\0 I'm\0 a\0 binary\0 message\0."
local nLength = string.len(buf)
t = {string.byte(buf,1,-1)}
--[[print(t)
print(table.getn(t))
print(string.char(unpack(t)))]]--
wsSendBinary:sendBinaryMsg(t,table.getn(t))
else
local warningStr = "send binary websocket instance wasn't ready..."
sendBinaryStatus:setString(warningStr)
end
end
end
local labelSendBinary = CCLabelTTF:create("Send Binary", "Arial", 22)
local itemSendBinary = CCMenuItemLabel:create(labelSendBinary)
itemSendBinary:registerScriptTapHandler(onMenuSendBinaryClicked)
itemSendBinary:setPosition(ccp(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE))
menuRequest:addChild(itemSendBinary)
--Send Text Status Label
sendTextStatus = CCLabelTTF:create("Send Text WS is waiting...", "Arial", 14,CCSizeMake(160, 100),kCCVerticalTextAlignmentCenter,kCCVerticalTextAlignmentTop)
sendTextStatus:setAnchorPoint(ccp(0, 0))
sendTextStatus:setPosition(ccp(0, 25))
layer:addChild(sendTextStatus)
--Send Binary Status Label
sendBinaryStatus = CCLabelTTF:create("Send Binary WS is waiting...", "Arial", 14, CCSizeMake(160, 100), kCCVerticalTextAlignmentCenter, kCCVerticalTextAlignmentTop)
sendBinaryStatus:setAnchorPoint(ccp(0, 0))
sendBinaryStatus:setPosition(ccp(160, 25))
layer:addChild(sendBinaryStatus)
--Error Label
errorStatus = CCLabelTTF:create("Error WS is waiting...", "Arial", 14, CCSizeMake(160, 100), kCCVerticalTextAlignmentCenter, kCCVerticalTextAlignmentTop)
errorStatus:setAnchorPoint(ccp(0, 0))
errorStatus:setPosition(ccp(320, 25))
layer:addChild(errorStatus)
local toMainMenu = CCMenu:create()
CreateExtensionsBasicLayerMenu(toMainMenu)
toMainMenu:setPosition(ccp(0, 0))
layer:addChild(toMainMenu,10)
wsSendText = WebSocket:create("ws://echo.websocket.org")
wsSendBinary = WebSocket:create("ws://echo.websocket.org")
wsError = WebSocket:create("ws://invalid.url.com")
local function wsSendTextOpen(strData)
sendTextStatus:setString("Send Text WS was opened.")
end
local function wsSendTextMessage(strData)
receiveTextTimes= receiveTextTimes + 1
local strInfo= "response text msg: "..strData..", "..receiveTextTimes
sendTextStatus:setString(strInfo)
end
local function wsSendTextClose(strData)
print("_wsiSendText websocket instance closed.")
sendTextStatus = nil
end
local function wsSendTextError(strData)
print("sendText Error was fired")
end
local function wsSendBinaryOpen(strData)
sendBinaryStatus:setString("Send Binary WS was opened.")
end
local function wsSendBinaryMessage(paramTable,strEventName)
local length = table.getn(paramTable)
local i = 1
local strInfo = "response bin msg: "
for i = 1,length do
if 0 == paramTable[i] then
strInfo = strInfo.."\'\\0\'"
else
strInfo = strInfo..string.char(paramTable[i])
end
end
receiveBinaryTimes = receiveBinaryTimes + 1
strInfo = strInfo..receiveBinaryTimes
sendBinaryStatus:setString(strInfo)
end
local function wsSendBinaryClose(strData)
print("_wsiSendBinary websocket instance closed.")
sendBinaryStatus = nil
end
local function wsSendBinaryError(strData)
print("sendBinary Error was fired")
end
local function wsErrorOpen(strData)
end
local function wsErrorMessage(strData)
end
local function wsErrorError(strData)
print("Error was fired")
errorStatus:setString("an error was fired")
end
local function wsErrorClose(strData)
print("_wsiError websocket instance closed.")
errorStatus= nill
end
if nil ~= wsSendText then
print("come in")
wsSendText:registerScriptHandler(wsSendTextOpen,kwebSocketScriptHandlerOpen)
wsSendText:registerScriptHandler(wsSendTextMessage,kwebSocketScriptHandlerMessage)
wsSendText:registerScriptHandler(wsSendTextClose,kwebSocketScriptHandlerClose)
wsSendText:registerScriptHandler(wsSendTextError,kwebSocketScriptHandlerError)
end
if nil ~= wsSendBinary then
wsSendBinary:registerScriptHandler(wsSendBinaryOpen,kwebSocketScriptHandlerOpen)
wsSendBinary:registerScriptHandler(wsSendBinaryMessage,kwebSocketScriptHandlerMessage)
wsSendBinary:registerScriptHandler(wsSendBinaryClose,kwebSocketScriptHandlerClose)
wsSendBinary:registerScriptHandler(wsSendBinaryError,kwebSocketScriptHandlerError)
end
if nil ~= wsError then
wsError:registerScriptHandler(wsErrorOpen,kwebSocketScriptHandlerOpen)
wsError:registerScriptHandler(wsErrorMessage,kwebSocketScriptHandlerMessage)
wsError:registerScriptHandler(wsErrorClose,kwebSocketScriptHandlerClose)
wsError:registerScriptHandler(wsErrorError,kwebSocketScriptHandlerError)
end
local function OnExit(strEventName)
if "exit" == strEventName then
if nil ~= wsSendText then
wsSendText:close()
wsSendText:unregisterScriptHandler(kwebSocketScriptHandlerOpen)
wsSendText:unregisterScriptHandler(kwebSocketScriptHandlerMessage)
wsSendText:unregisterScriptHandler(kwebSocketScriptHandlerClose)
wsSendText:unregisterScriptHandler(kwebSocketScriptHandlerError)
end
if nil ~= wsSendBinary then
wsSendBinary:close()
wsSendBinary:unregisterScriptHandler(kwebSocketScriptHandlerOpen)
wsSendBinary:unregisterScriptHandler(kwebSocketScriptHandlerMessage)
wsSendBinary:unregisterScriptHandler(kwebSocketScriptHandlerClose)
wsSendBinary:unregisterScriptHandler(kwebSocketScriptHandlerError)
end
if nil ~= wsError then
wsError:close()
wsError:unregisterScriptHandler(kwebSocketScriptHandlerOpen)
wsError:unregisterScriptHandler(kwebSocketScriptHandlerMessage)
wsError:unregisterScriptHandler(kwebSocketScriptHandlerClose)
wsError:unregisterScriptHandler(kwebSocketScriptHandlerError)
end
end
end
layer:registerScriptHandler(OnExit)
return layer
end
function runWebSocketTest()
local scene = CCScene:create()
scene:addChild(WebSocketTestLayer())
return scene
end

View File

@ -1 +1 @@
8e637a47f18c655e82a0b57f93dc0ba49fb2e9b2
77b20270dfc029d68b9129b05b9451b8d47c10b1

View File

@ -331,4 +331,27 @@ 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

View File

@ -117,6 +117,8 @@ 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)

View File

@ -0,0 +1,409 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua_fix.h"
#ifdef __cplusplus
}
#endif
#include <map>
#include <string>
#include "Lua_web_socket.h"
#include "cocos2d.h"
#include "WebSocket.h"
using namespace cocos2d;
using namespace cocos2d::extension;
class LuaWebSocketDelegate : public WebSocket::Delegate
{
public:
virtual void onOpen(WebSocket* ws)
{
if (NULL != ws) {
int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerOpen);
if (-1 != nHandler) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
}
}
}
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, "");
}
}
else{
int nHandler = ws->getScriptHandler(WebSocket::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);
if (-1 != nHandler) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
}
}
}
virtual void onError(WebSocket* ws, const WebSocket::ErrorCode& error)
{
if (NULL != ws) {
int nHandler = ws->getScriptHandler(WebSocket::kwebSocketScriptHandlerError);
if (-1 != nHandler) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
}
}
}
};
#ifdef __cplusplus
static int tolua_collect_WebSocket (lua_State* tolua_S)
{
WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0);
Mtolua_delete(self);
return 0;
}
#endif
/* function to release collected object via destructor */
static void tolua_reg_Web_Socket_type(lua_State* tolua_S)
{
tolua_usertype(tolua_S, "WebSocket");
}
/* method: create of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_create00
static int tolua_Cocos2d_WebSocket_create00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* urlName = ((const char*) tolua_tostring(tolua_S,2,0));
WebSocket *wSocket = new WebSocket();
LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate();
wSocket->init(*delegate, urlName);
tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket");
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: createByAProtocol of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_createByAProtocol00
static int tolua_Cocos2d_WebSocket_createByAProtocol00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
!tolua_isstring(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char *urlName = ((const char*) tolua_tostring(tolua_S,2,0));
const char *protocol = ((const char*) tolua_tostring(tolua_S,3,0));
std::vector<std::string> protocols;
protocols.push_back(protocol);
WebSocket *wSocket = new WebSocket();
LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate();
wSocket->init(*delegate, urlName,&protocols);
tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket");
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'createByAProtocol'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: createByAProtocol of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_createByProtocolArray00
static int tolua_Cocos2d_WebSocket_createByProtocolArray00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
!tolua_isusertable(tolua_S,3,"CCArray",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char *urlName = ((const char*) tolua_tostring(tolua_S,2,0));
CCArray* protocolArray = ((CCArray*) tolua_tousertype(tolua_S,3,0));
std::vector<std::string> protocols;
if (NULL != protocolArray) {
CCObject* pObj = NULL;
CCARRAY_FOREACH(protocolArray, pObj)
{
CCString* pStr = (CCString*)pObj;
if (NULL != pStr) {
protocols.push_back(pStr->getCString());
}
}
}
WebSocket *wSocket = new WebSocket();
LuaWebSocketDelegate* delegate = new LuaWebSocketDelegate();
wSocket->init(*delegate, urlName,&protocols);
tolua_pushusertype(tolua_S,(void*)wSocket,"WebSocket");
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'createByProtocolArray'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: getReadyState of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_getReadyState00
static int tolua_Cocos2d_WebSocket_getReadyState00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0);
int tolua_ret = -1;
if (NULL != self) {
tolua_ret = self->getReadyState();
}
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'getReadyState'.",&tolua_err);
return 0;
#endif
}
#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)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) 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;
self->send(strData);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'sendStrMsg'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: close of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_close00
static int tolua_Cocos2d_WebSocket_close00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0);
if (NULL != self ) {
self->close();
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'getReadyState'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: addHandlerOfDelegateEvent of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_registerScriptHandler00
static int tolua_Cocos2d_WebSocket_registerScriptHandler00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) 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));
self->registerScriptHandler(nFunID, handlerType);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: removeHandlerOfDelegateEvent of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_unregisterScriptHandler00
static int tolua_Cocos2d_WebSocket_unregisterScriptHandler00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0);
if (NULL != self ) {
WebSocket::webSocketScriptHandlerType handlerType = ((WebSocket::webSocketScriptHandlerType) (int) tolua_tonumber(tolua_S,2,0));
self->unregisterScriptHandler(handlerType);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: sendBinaryMsg of class WebSocket */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_sendBinaryMsg00
static int tolua_Cocos2d_WebSocket_sendBinaryMsg00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
!tolua_istable(tolua_S, 2, 0, &tolua_err) ||
!tolua_isnumber(tolua_S,3, 0,&tolua_err) ||
!tolua_isnoobj(tolua_S, 4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
WebSocket* self = (WebSocket*) tolua_tousertype(tolua_S,1,0);
int nLength = lua_tonumber(tolua_S, 3);
if (NULL != self && nLength > 0) {
unsigned char* binaryArray = new unsigned char[nLength];
for (int i = 0; i < nLength; i++) {
binaryArray[i] = (unsigned char)tolua_tofieldnumber(tolua_S, 2, i+1, 0);
}
self->send(binaryArray, nLength);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'sendBinaryMsg'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){
tolua_open(tolua_S);
tolua_reg_Web_Socket_type(tolua_S);
tolua_module(tolua_S,NULL,0);
tolua_beginmodule(tolua_S,NULL);
tolua_constant(tolua_S,"kStateConnecting",WebSocket::kStateConnecting);
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);
#ifdef __cplusplus
tolua_cclass(tolua_S,"WebSocket","WebSocket","",tolua_collect_WebSocket);
#else
tolua_cclass(tolua_S,"WebSocket","WebSocket","",NULL);
#endif
tolua_beginmodule(tolua_S,"WebSocket");
tolua_function(tolua_S, "create", tolua_Cocos2d_WebSocket_create00);
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, "close", tolua_Cocos2d_WebSocket_close00);
tolua_function(tolua_S, "registerScriptHandler", tolua_Cocos2d_WebSocket_registerScriptHandler00);
tolua_function(tolua_S, "unregisterScriptHandler", tolua_Cocos2d_WebSocket_unregisterScriptHandler00);
tolua_function(tolua_S, "sendBinaryMsg", tolua_Cocos2d_WebSocket_sendBinaryMsg00);
tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;
}

View File

@ -0,0 +1,14 @@
#ifndef __LUA_WEB_SOCKET_H__
#define __LUA_WEB_SOCKET_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif
TOLUA_API int tolua_web_socket_open(lua_State* tolua_S);
#endif //__LUA_WEB_SOCKET_H__