mirror of https://github.com/axmolengine/axmol.git
[WebSocket] Adds support for getting url and server selected protocol (#17381)
* [WebSocket] Adds support for getting url and server selected protocol * Adds const keyword for getter methods, and make LOGE also works in release mode.
This commit is contained in:
parent
f506cb9eb2
commit
619e012d9b
|
@ -656,6 +656,9 @@
|
|||
1AC35DF818CEE65B00F37B72 /* pew-pew-lei.wav in Resources */ = {isa = PBXBuildFile; fileRef = 1AC35CC418CED84500F37B72 /* pew-pew-lei.wav */; };
|
||||
1ACF6A521E4C52660033C137 /* cacert.pem in Resources */ = {isa = PBXBuildFile; fileRef = 1ACF6A511E4C52660033C137 /* cacert.pem */; };
|
||||
1ACF6A531E4C52660033C137 /* cacert.pem in Resources */ = {isa = PBXBuildFile; fileRef = 1ACF6A511E4C52660033C137 /* cacert.pem */; };
|
||||
1AF5ECFC1E56D6BC00484A9D /* cacert.pem in Resources */ = {isa = PBXBuildFile; fileRef = 1ACF6A511E4C52660033C137 /* cacert.pem */; };
|
||||
1AF5ECFD1E56D6BC00484A9D /* cocosvideo.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 3EA0FB5D191B92F100B170C8 /* cocosvideo.mp4 */; };
|
||||
1AF5ECFE1E56D6CD00484A9D /* cacert.pem in Resources */ = {isa = PBXBuildFile; fileRef = 1ACF6A511E4C52660033C137 /* cacert.pem */; };
|
||||
1F33634F18E37E840074764D /* RefPtrTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F33634D18E37E840074764D /* RefPtrTest.cpp */; };
|
||||
1F33635018E37E840074764D /* RefPtrTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F33634D18E37E840074764D /* RefPtrTest.cpp */; };
|
||||
27C5CE011C6E0469000CA4B3 /* SpriteFrameCacheTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C5CDFF1C6E0469000CA4B3 /* SpriteFrameCacheTest.cpp */; };
|
||||
|
@ -1843,7 +1846,6 @@
|
|||
/* Begin PBXFileReference section */
|
||||
150F918619DA409E00B89F57 /* lua_test_bindings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_test_bindings.cpp; sourceTree = "<group>"; };
|
||||
150F918719DA409E00B89F57 /* lua_test_bindings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_test_bindings.h; sourceTree = "<group>"; };
|
||||
15427B76198B750300DC375D /* lua_module_register.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_module_register.h; sourceTree = "<group>"; };
|
||||
15427B77198B843300DC375D /* lua_module_register.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_module_register.h; sourceTree = "<group>"; };
|
||||
15427B7B198B880100DC375D /* lua_cocos2dx_controller_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = lua_cocos2dx_controller_manual.cpp; path = "../../../../cocos/scripting/lua-bindings/manual/controller/lua_cocos2dx_controller_manual.cpp"; sourceTree = "<group>"; };
|
||||
15427B7C198B880100DC375D /* lua_cocos2dx_controller_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = lua_cocos2dx_controller_manual.hpp; path = "../../../../cocos/scripting/lua-bindings/manual/controller/lua_cocos2dx_controller_manual.hpp"; sourceTree = "<group>"; };
|
||||
|
@ -4569,7 +4571,6 @@
|
|||
children = (
|
||||
150F918619DA409E00B89F57 /* lua_test_bindings.cpp */,
|
||||
150F918719DA409E00B89F57 /* lua_test_bindings.h */,
|
||||
15427B76198B750300DC375D /* lua_module_register.h */,
|
||||
1AC35D7D18CEE5B100F37B72 /* AppDelegate.cpp */,
|
||||
1AC35D7E18CEE5B100F37B72 /* AppDelegate.h */,
|
||||
1AC35D7F18CEE5B100F37B72 /* lua_assetsmanager_test_sample.cpp */,
|
||||
|
@ -6086,6 +6087,8 @@
|
|||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
1AF5ECFC1E56D6BC00484A9D /* cacert.pem in Resources */,
|
||||
1AF5ECFD1E56D6BC00484A9D /* cocosvideo.mp4 in Resources */,
|
||||
1A78A5521D1A3C9B00B61B27 /* ccs-res in Resources */,
|
||||
150983E11B1C514B007F3818 /* NavMesh in Resources */,
|
||||
15B914491B15721400C6B95B /* Shaders3D in Resources */,
|
||||
|
@ -6136,6 +6139,7 @@
|
|||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
1AF5ECFE1E56D6CD00484A9D /* cacert.pem in Resources */,
|
||||
1A78A5541D1A3D0F00B61B27 /* ccs-res in Resources */,
|
||||
15BF83C71B1D9E8900D518FA /* NavMesh in Resources */,
|
||||
15B9144A1B15A18C00C6B95B /* Materials in Resources */,
|
||||
|
|
|
@ -119,17 +119,22 @@ static void wsLog(const char * format, ...)
|
|||
#define QUOTEME(x) QUOTEME_(x)
|
||||
|
||||
// Since CCLOG isn't thread safe, we uses LOGD for multi-thread logging.
|
||||
#if COCOS2D_DEBUG > 0
|
||||
#ifdef ANDROID
|
||||
#ifdef ANDROID
|
||||
#if COCOS2D_DEBUG > 0
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,__VA_ARGS__)
|
||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
|
||||
#else
|
||||
#define LOGD(fmt, ...) wsLog("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
||||
#define LOGE(fmt, ...) wsLog("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
||||
#define LOGD(...)
|
||||
#endif
|
||||
|
||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
|
||||
#else
|
||||
#define LOGD(...)
|
||||
#define LOGE(...)
|
||||
#if COCOS2D_DEBUG > 0
|
||||
#define LOGD(fmt, ...) wsLog("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOGD(fmt, ...)
|
||||
#endif
|
||||
|
||||
#define LOGE(fmt, ...) wsLog("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
static void printWebSocketLog(int level, const char *line)
|
||||
|
@ -509,12 +514,10 @@ void WebSocket::closeAllConnections()
|
|||
|
||||
WebSocket::WebSocket()
|
||||
: _readyState(State::CONNECTING)
|
||||
, _port(80)
|
||||
, _wsInstance(nullptr)
|
||||
, _protocols(nullptr)
|
||||
, _lwsProtocols(nullptr)
|
||||
, _isDestroyed(std::make_shared<std::atomic<bool>>(false))
|
||||
, _delegate(nullptr)
|
||||
, _SSLConnection(0)
|
||||
, _closeState(CloseState::NONE)
|
||||
{
|
||||
// reserve data buffer to avoid allocate memory frequently
|
||||
|
@ -575,58 +578,44 @@ bool WebSocket::init(const Delegate& delegate,
|
|||
const std::string& caFilePath/* = ""*/)
|
||||
{
|
||||
_delegate = const_cast<Delegate*>(&delegate);
|
||||
_url = url;
|
||||
_caFilePath = caFilePath;
|
||||
|
||||
const char* prot = nullptr;
|
||||
const char* address = nullptr;
|
||||
const char* path = nullptr;
|
||||
int port = -1;
|
||||
|
||||
if (lws_parse_uri((char*)url.c_str(), &prot, &address, &port, &path))
|
||||
{
|
||||
// make sure url start with 'ws://' or 'wss://'
|
||||
if (_url.size() < 6)
|
||||
return false;
|
||||
}
|
||||
|
||||
_host = address;
|
||||
_port = port;
|
||||
_path = path;
|
||||
|
||||
if (!strcmp(prot, "http") || !strcmp(prot, "ws"))
|
||||
_SSLConnection = 0;
|
||||
if (!strcmp(prot, "https") || !strcmp(prot, "wss"))
|
||||
_SSLConnection = LCCSCF_USE_SSL;
|
||||
CCASSERT(0 == strncmp(_url.c_str(), "ws://", 5) || 0 == strncmp(_url.c_str(), "wss://", 6), "Invalid URL");
|
||||
|
||||
if (protocols != nullptr && !protocols->empty())
|
||||
{
|
||||
size_t size = protocols->size();
|
||||
_protocols = (struct lws_protocols*)malloc((size + 1) * sizeof(struct lws_protocols));
|
||||
memset(_protocols, 0, (size + 1) * sizeof(struct lws_protocols));
|
||||
_lwsProtocols = (struct lws_protocols*)malloc((size + 1) * sizeof(struct lws_protocols));
|
||||
memset(_lwsProtocols, 0, (size + 1) * sizeof(struct lws_protocols));
|
||||
|
||||
static uint32_t __wsId = 0;
|
||||
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
_protocols[i].callback = WebSocketCallbackWrapper::onSocketCallback;
|
||||
_lwsProtocols[i].callback = WebSocketCallbackWrapper::onSocketCallback;
|
||||
size_t nameLen = protocols->at(i).length();
|
||||
char* name = (char*)malloc(nameLen + 1);
|
||||
name[nameLen] = '\0';
|
||||
strcpy(name, protocols->at(i).c_str());
|
||||
_protocols[i].name = name;
|
||||
_protocols[i].id = ++__wsId;
|
||||
_protocols[i].rx_buffer_size = WS_RX_BUFFER_SIZE;
|
||||
_protocols[i].per_session_data_size = 0;
|
||||
_protocols[i].user = nullptr;
|
||||
_lwsProtocols[i].name = name;
|
||||
_lwsProtocols[i].id = ++__wsId;
|
||||
_lwsProtocols[i].rx_buffer_size = WS_RX_BUFFER_SIZE;
|
||||
_lwsProtocols[i].per_session_data_size = 0;
|
||||
_lwsProtocols[i].user = nullptr;
|
||||
|
||||
_protocolNames += name;
|
||||
_clientSupportedProtocols += name;
|
||||
if (i < (size - 1))
|
||||
{
|
||||
_protocolNames += ",";
|
||||
_clientSupportedProtocols += ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOGD("[WebSocket::init] _host: %s, _port: %d, _path: %s\n", _host.c_str(), _port, _path.c_str());
|
||||
|
||||
// WebSocket thread needs to be invoked at the end of this method.
|
||||
if (__wsHelper == nullptr)
|
||||
{
|
||||
|
@ -761,7 +750,7 @@ WebSocket::State WebSocket::getReadyState()
|
|||
return _readyState;
|
||||
}
|
||||
|
||||
struct lws_vhost* WebSocket::createVhost(struct lws_protocols* protocols)
|
||||
struct lws_vhost* WebSocket::createVhost(struct lws_protocols* protocols, int& sslConnection)
|
||||
{
|
||||
auto fileUtils = FileUtils::getInstance();
|
||||
bool isCAFileExist = fileUtils->isFileExist(_caFilePath);
|
||||
|
@ -772,7 +761,7 @@ struct lws_vhost* WebSocket::createVhost(struct lws_protocols* protocols)
|
|||
|
||||
lws_context_creation_info info = convertToContextCreationInfo(protocols, isCAFileExist);
|
||||
|
||||
if (_SSLConnection != 0)
|
||||
if (sslConnection != 0)
|
||||
{
|
||||
if (isCAFileExist)
|
||||
{
|
||||
|
@ -838,7 +827,7 @@ struct lws_vhost* WebSocket::createVhost(struct lws_protocols* protocols)
|
|||
else
|
||||
{
|
||||
LOGD("WARNING: CA Root file isn't set. SSL connection will not peer server certificate\n");
|
||||
_SSLConnection = _SSLConnection | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
|
||||
sslConnection = sslConnection | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -872,26 +861,46 @@ void WebSocket::onClientOpenConnectionRequest()
|
|||
_readyState = State::CONNECTING;
|
||||
_readyStateMutex.unlock();
|
||||
|
||||
struct lws_vhost* vhost = nullptr;
|
||||
if (_protocols != nullptr)
|
||||
const char* prot = nullptr;
|
||||
const char* address = nullptr;
|
||||
const char* path = nullptr;
|
||||
int port = -1;
|
||||
|
||||
// lws_parse_uri will modify its first parameter, but _url is the member variable that we don't want it to be changed.
|
||||
// Therefore, use a temporary url variable here.
|
||||
std::string tmpUrl = _url;
|
||||
if (lws_parse_uri((char*)tmpUrl.c_str(), &prot, &address, &port, &path))
|
||||
{
|
||||
vhost = createVhost(_protocols);
|
||||
LOGE("lws_parse_uri failed: %s", _url.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
LOGD("protocol: %s, host: %s, port: %d, path: %s\n", prot, address, port, path);
|
||||
|
||||
int sslConnection = 0;
|
||||
if (0 == strcmp(prot, "wss"))
|
||||
sslConnection = LCCSCF_USE_SSL;
|
||||
|
||||
struct lws_vhost* vhost = nullptr;
|
||||
if (_lwsProtocols != nullptr)
|
||||
{
|
||||
vhost = createVhost(_lwsProtocols, sslConnection);
|
||||
}
|
||||
else
|
||||
{
|
||||
vhost = createVhost(__defaultProtocols);
|
||||
vhost = createVhost(__defaultProtocols, sslConnection);
|
||||
}
|
||||
|
||||
struct lws_client_connect_info connectInfo;
|
||||
memset(&connectInfo, 0, sizeof(connectInfo));
|
||||
connectInfo.context = __wsContext;
|
||||
connectInfo.address = _host.c_str();
|
||||
connectInfo.port = _port;
|
||||
connectInfo.ssl_connection = _SSLConnection;
|
||||
connectInfo.path = _path.c_str();
|
||||
connectInfo.host = _host.c_str();
|
||||
connectInfo.origin = _host.c_str();
|
||||
connectInfo.protocol = _protocolNames.empty() ? nullptr : _protocolNames.c_str();
|
||||
connectInfo.address = address;
|
||||
connectInfo.port = port;
|
||||
connectInfo.ssl_connection = sslConnection;
|
||||
connectInfo.path = path;
|
||||
connectInfo.host = address;
|
||||
connectInfo.origin = address;
|
||||
connectInfo.protocol = _clientSupportedProtocols.empty() ? nullptr : _clientSupportedProtocols.c_str();
|
||||
connectInfo.ietf_version_or_minus_one = -1;
|
||||
connectInfo.userdata = this;
|
||||
connectInfo.client_exts = exts;
|
||||
|
@ -1144,7 +1153,9 @@ int WebSocket::onClientReceivedData(void* in, ssize_t len)
|
|||
|
||||
int WebSocket::onConnectionOpened()
|
||||
{
|
||||
LOGD("onConnectionOpened...: %p\n", this);
|
||||
const lws_protocols* lwsSelectedProtocol = lws_get_protocol(_wsInstance);
|
||||
_selectedProtocol = lwsSelectedProtocol->name;
|
||||
LOGD("onConnectionOpened...: %p, client protocols: %s, server selected protocol: %s\n", this, _clientSupportedProtocols.c_str(), _selectedProtocol.c_str());
|
||||
/*
|
||||
* start the ball rolling,
|
||||
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
||||
|
|
|
@ -212,6 +212,16 @@ public:
|
|||
*/
|
||||
State getReadyState();
|
||||
|
||||
/**
|
||||
* @brief Gets the URL of websocket connection.
|
||||
*/
|
||||
inline const std::string& getUrl() const { return _url; }
|
||||
|
||||
/**
|
||||
* @brief Gets the protocol selected by websocket server.
|
||||
*/
|
||||
inline const std::string& getProtocol() const { return _selectedProtocol; }
|
||||
|
||||
private:
|
||||
|
||||
// The following callback functions are invoked in websocket thread
|
||||
|
@ -224,26 +234,24 @@ private:
|
|||
int onConnectionError();
|
||||
int onConnectionClosed();
|
||||
|
||||
struct lws_vhost* createVhost(struct lws_protocols* protocols);
|
||||
struct lws_vhost* createVhost(struct lws_protocols* protocols, int& sslConnection);
|
||||
|
||||
private:
|
||||
|
||||
std::mutex _readyStateMutex;
|
||||
State _readyState;
|
||||
|
||||
std::string _host;
|
||||
unsigned int _port;
|
||||
std::string _path;
|
||||
std::string _url;
|
||||
|
||||
std::vector<char> _receivedData;
|
||||
|
||||
struct lws* _wsInstance;
|
||||
struct lws_protocols* _protocols;
|
||||
std::string _protocolNames;
|
||||
struct lws_protocols* _lwsProtocols;
|
||||
std::string _clientSupportedProtocols;
|
||||
std::string _selectedProtocol;
|
||||
|
||||
std::shared_ptr<std::atomic<bool>> _isDestroyed;
|
||||
Delegate* _delegate;
|
||||
int _SSLConnection;
|
||||
|
||||
std::mutex _closeMutex;
|
||||
std::condition_variable _closeCondition;
|
||||
|
|
|
@ -85,6 +85,12 @@ public:
|
|||
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
|
||||
|
||||
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
||||
|
||||
// Set the protocol which server selects.
|
||||
JS::RootedValue jsprotocol(cx, std_string_to_jsval(cx, ws->getProtocol()));
|
||||
JS::RootedObject wsObj(cx, p->obj);
|
||||
JS_SetProperty(cx, wsObj, "protocol", jsprotocol);
|
||||
|
||||
JS::RootedObject jsobj(cx, JS_NewObject(cx, NULL, JS::NullPtr(), JS::NullPtr()));
|
||||
JS::RootedValue vp(cx);
|
||||
vp = c_string_to_jsval(cx, "open");
|
||||
|
@ -361,11 +367,14 @@ bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, j
|
|||
cobj->init(*delegate, url);
|
||||
}
|
||||
|
||||
JS_DefineProperty(cx, obj, "url", args.get(0), JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
|
||||
|
||||
// The websocket draft uses lowercase 'url', so 'URL' need to be deprecated.
|
||||
JS_DefineProperty(cx, obj, "URL", args.get(0), JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
|
||||
|
||||
//protocol not support yet (always return "")
|
||||
// Initialize protocol property with an empty string, it will be assigned in onOpen delegate.
|
||||
JS::RootedValue jsprotocol(cx, c_string_to_jsval(cx, ""));
|
||||
JS_DefineProperty(cx, obj, "protocol", jsprotocol, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
|
||||
JS_DefineProperty(cx, obj, "protocol", jsprotocol, JSPROP_ENUMERATE | JSPROP_PERMANENT);
|
||||
|
||||
// link the native object with the javascript object
|
||||
js_proxy_t *p = jsb_new_proxy(cobj, obj);
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include "scripting/lua-bindings/manual/CCLuaStack.h"
|
||||
#include "scripting/lua-bindings/manual/CCLuaValue.h"
|
||||
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
|
||||
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
|
||||
#include "scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h"
|
||||
#include "deprecated/CCString.h"
|
||||
|
||||
|
@ -165,27 +166,62 @@ static int tolua_Cocos2d_WebSocket_create00(lua_State* tolua_S)
|
|||
{
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_Error tolua_err;
|
||||
if (
|
||||
!tolua_isusertable(tolua_S,1,"cc.WebSocket",0,&tolua_err) ||
|
||||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
|
||||
!tolua_isnoobj(tolua_S,3,&tolua_err)
|
||||
)
|
||||
goto tolua_lerror;
|
||||
else
|
||||
#endif
|
||||
|
||||
int argumentCount = lua_gettop(tolua_S);
|
||||
if (argumentCount >= 2)
|
||||
{
|
||||
const char* urlName = ((const char*) tolua_tostring(tolua_S,2,0));
|
||||
LuaWebSocket *wSocket = new (std::nothrow) LuaWebSocket();
|
||||
wSocket->init(*wSocket, urlName);
|
||||
tolua_pushusertype(tolua_S,(void*)wSocket,"cc.WebSocket");
|
||||
tolua_register_gc(tolua_S,lua_gettop(tolua_S));
|
||||
std::string url;
|
||||
std::vector<std::string> protocols;
|
||||
std::string caCertPath;
|
||||
|
||||
#ifndef TOLUA_RELEASE
|
||||
if (!tolua_isusertable(tolua_S,1,"cc.WebSocket",0,&tolua_err) ||
|
||||
!tolua_isstring(tolua_S,2,0,&tolua_err)
|
||||
)
|
||||
goto tolua_lerror;
|
||||
#endif
|
||||
if (argumentCount == 3)
|
||||
{
|
||||
if (lua_istable(tolua_S, 3))
|
||||
{
|
||||
luaval_to_std_vector_string(tolua_S, 3, &protocols, "cc.WebSocket.create");
|
||||
}
|
||||
else if (lua_isstring(tolua_S, 3))
|
||||
{
|
||||
luaval_to_std_string(tolua_S, 3, &caCertPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
luaL_error(tolua_S, "The second parameter should be a table contains protocols of string type or a string indicates ca certificate path!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (argumentCount == 4)
|
||||
{
|
||||
#ifndef TOLUA_RELEASE
|
||||
if (!tolua_istable(tolua_S, 3, 0, &tolua_err)
|
||||
|| !tolua_isstring(tolua_S, 4, 0, &tolua_err))
|
||||
goto tolua_lerror;
|
||||
#endif
|
||||
|
||||
luaval_to_std_vector_string(tolua_S, 3, &protocols, "cc.WebSocket.create");
|
||||
luaval_to_std_string(tolua_S, 4, &caCertPath);
|
||||
}
|
||||
|
||||
luaval_to_std_string(tolua_S, 2, &url);
|
||||
|
||||
LuaWebSocket *wSocket = new (std::nothrow) LuaWebSocket();
|
||||
wSocket->init(*wSocket, url, &protocols, caCertPath);
|
||||
tolua_pushusertype(tolua_S,(void*)wSocket,"cc.WebSocket");
|
||||
tolua_register_gc(tolua_S,lua_gettop(tolua_S));
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_lerror:
|
||||
tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
|
||||
return 0;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif //#ifndef TOLUA_DISABLE
|
||||
|
||||
|
@ -313,7 +349,7 @@ static int tolua_Cocos2d_WebSocket_close00(lua_State* tolua_S)
|
|||
{
|
||||
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
|
||||
if (NULL != self ) {
|
||||
self->close();
|
||||
self->closeAsync();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -364,6 +400,45 @@ tolua_lerror:
|
|||
}
|
||||
#endif //#ifndef TOLUA_DISABLE
|
||||
|
||||
static int websocket_url_getter(lua_State* L)
|
||||
{
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_Error tolua_err;
|
||||
if (!tolua_isusertype(L, 1, "cc.WebSocket", 0, &tolua_err))
|
||||
goto tolua_lerror;
|
||||
else
|
||||
#endif
|
||||
{
|
||||
LuaWebSocket* self = (LuaWebSocket*)tolua_tousertype(L, 1, 0);
|
||||
lua_pushstring(L, self->getUrl().c_str());
|
||||
return 1;
|
||||
}
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_lerror:
|
||||
tolua_error(L,"#ferror in function 'sendString'.",&tolua_err);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int websocket_protocol_getter(lua_State* L)
|
||||
{
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_Error tolua_err;
|
||||
if (!tolua_isusertype(L, 1, "cc.WebSocket", 0, &tolua_err))
|
||||
goto tolua_lerror;
|
||||
else
|
||||
#endif
|
||||
{
|
||||
LuaWebSocket* self = (LuaWebSocket*)tolua_tousertype(L, 1, 0);
|
||||
lua_pushstring(L, self->getProtocol().c_str());
|
||||
return 1;
|
||||
}
|
||||
#ifndef TOLUA_RELEASE
|
||||
tolua_lerror:
|
||||
tolua_error(L,"#ferror in function 'sendString'.",&tolua_err);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){
|
||||
tolua_open(tolua_S);
|
||||
|
@ -382,6 +457,8 @@ TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){
|
|||
tolua_function(tolua_S, "getReadyState", tolua_Cocos2d_WebSocket_getReadyState00);
|
||||
tolua_function(tolua_S, "close", tolua_Cocos2d_WebSocket_close00);
|
||||
tolua_function(tolua_S, "sendString", tolua_Cocos2d_WebSocket_sendString00);
|
||||
tolua_variable(tolua_S, "url", websocket_url_getter, nullptr);
|
||||
tolua_variable(tolua_S, "protocol", websocket_protocol_getter, nullptr);
|
||||
tolua_endmodule(tolua_S);
|
||||
tolua_endmodule(tolua_S);
|
||||
return 1;
|
||||
|
|
|
@ -119,7 +119,7 @@ void WebSocketTest::startTestCallback(Ref* sender)
|
|||
retain(); // Retain self to avoid WebSocketTest instance be deleted immediately, it will be released in WebSocketTest::onClose.
|
||||
}
|
||||
|
||||
protocols.pop_back();
|
||||
protocols.erase(protocols.begin());
|
||||
if (!_wsiSendBinary->init(*this, "wss://echo.websocket.org", &protocols))
|
||||
{
|
||||
CC_SAFE_DELETE(_wsiSendBinary);
|
||||
|
@ -142,14 +142,17 @@ void WebSocketTest::startTestCallback(Ref* sender)
|
|||
// Delegate methods
|
||||
void WebSocketTest::onOpen(network::WebSocket* ws)
|
||||
{
|
||||
log("Websocket (%p) opened", ws);
|
||||
char status[256] = {0};
|
||||
sprintf(status, "Opened, url: %s, protocol: %s", ws->getUrl().c_str(), ws->getProtocol().c_str());
|
||||
|
||||
log("Websocket (%p) was opened, url: %s, protocol: %s", ws, ws->getUrl().c_str(), ws->getProtocol().c_str());
|
||||
if (ws == _wsiSendText)
|
||||
{
|
||||
_sendTextStatus->setString("Send Text WS was opened.");
|
||||
_sendTextStatus->setString(status);
|
||||
}
|
||||
else if (ws == _wsiSendBinary)
|
||||
{
|
||||
_sendBinaryStatus->setString("Send Binary WS was opened.");
|
||||
_sendBinaryStatus->setString(status);
|
||||
}
|
||||
else if (ws == _wsiError)
|
||||
{
|
||||
|
|
|
@ -107,9 +107,9 @@ var WebSocketTestLayer = cc.Layer.extend({
|
|||
|
||||
var self = this;
|
||||
|
||||
this._wsiSendText = new WebSocket("ws://echo.websocket.org");
|
||||
this._wsiSendText = new WebSocket("wss://echo.websocket.org", ["protocol2", "protocol1"]);
|
||||
this._wsiSendText.onopen = function(evt) {
|
||||
self._sendTextStatus.setString("Send Text WS was opened.");
|
||||
self._sendTextStatus.setString("Opened, url: " + self._wsiSendText.url + ", protocol: " + self._wsiSendText.protocol);
|
||||
};
|
||||
|
||||
this._wsiSendText.onmessage = function(evt) {
|
||||
|
@ -135,10 +135,10 @@ var WebSocketTestLayer = cc.Layer.extend({
|
|||
};
|
||||
|
||||
|
||||
this._wsiSendBinary = new WebSocket("ws://echo.websocket.org");
|
||||
this._wsiSendBinary = new WebSocket("ws://echo.websocket.org", ["protocol3"]);
|
||||
this._wsiSendBinary.binaryType = "arraybuffer";
|
||||
this._wsiSendBinary.onopen = function(evt) {
|
||||
self._sendBinaryStatus.setString("Send Binary WS was opened.");
|
||||
self._sendBinaryStatus.setString("Opened, url: " + self._wsiSendBinary.url + ", protocol: " + self._wsiSendBinary.protocol);
|
||||
};
|
||||
|
||||
this._wsiSendBinary.onmessage = function(evt) {
|
||||
|
|
|
@ -85,12 +85,12 @@ local function WebSocketTestLayer()
|
|||
toMainMenu:setPosition(cc.p(0, 0))
|
||||
layer:addChild(toMainMenu,10)
|
||||
|
||||
wsSendText = cc.WebSocket:create("ws://echo.websocket.org")
|
||||
wsSendBinary = cc.WebSocket:create("ws://echo.websocket.org")
|
||||
wsError = cc.WebSocket:create("ws://invalid.url.com")
|
||||
wsSendText = cc.WebSocket:create("wss://echo.websocket.org", {"myprotocol_1", "myprotocol_2"}, "cacert.pem")
|
||||
wsSendBinary = cc.WebSocket:create("ws://echo.websocket.org", {"hello"}, "cacert.pem")
|
||||
wsError = cc.WebSocket:create("ws://invalid.url.com", {"invalid_protocol"})
|
||||
|
||||
local function wsSendTextOpen(strData)
|
||||
sendTextStatus:setString("Send Text WS was opened.")
|
||||
sendTextStatus:setString("Send Text WS was opened, url:" .. wsSendText.url .. ", protocol: " .. wsSendText.protocol)
|
||||
end
|
||||
|
||||
local function wsSendTextMessage(strData)
|
||||
|
@ -107,10 +107,11 @@ local function WebSocketTestLayer()
|
|||
|
||||
local function wsSendTextError(strData)
|
||||
print("sendText Error was fired")
|
||||
sendTextStatus:setString("sendText Error was fired")
|
||||
end
|
||||
|
||||
local function wsSendBinaryOpen(strData)
|
||||
sendBinaryStatus:setString("Send Binary WS was opened.")
|
||||
sendBinaryStatus:setString("Send Binary WS was opened, url:" .. wsSendBinary.url .. ", protocol: " .. wsSendBinary.protocol)
|
||||
end
|
||||
|
||||
local function wsSendBinaryMessage(paramTable)
|
||||
|
@ -137,6 +138,7 @@ local function WebSocketTestLayer()
|
|||
|
||||
local function wsSendBinaryError(strData)
|
||||
print("sendBinary Error was fired")
|
||||
sendBinaryStatus:setString("sendBinary Error was fired")
|
||||
end
|
||||
|
||||
local function wsErrorOpen(strData)
|
||||
|
|
Loading…
Reference in New Issue