mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
Sync with cocos2d-x v3 branch ddb4dc8 commit.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "network/WebSocket.h"
|
||||
#include "network/Uri.h"
|
||||
#include "base/CCDirector.h"
|
||||
#include "base/CCScheduler.h"
|
||||
#include "base/CCEventDispatcher.h"
|
||||
@@ -361,6 +362,7 @@ void WsThreadHelper::onSubThreadLoop()
|
||||
if (msg->what == WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION)
|
||||
{
|
||||
ws->onClientOpenConnectionRequest();
|
||||
delete *iter;
|
||||
iter = __wsHelper->_subThreadWsMessageQueue->erase(iter);
|
||||
}
|
||||
else
|
||||
@@ -508,6 +510,7 @@ void WebSocket::closeAllConnections()
|
||||
|
||||
std::lock_guard<std::mutex> lk(__instanceMutex);
|
||||
__websocketInstances->clear();
|
||||
delete __websocketInstances;
|
||||
__websocketInstances = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -581,12 +584,9 @@ bool WebSocket::init(const Delegate& delegate,
|
||||
_url = url;
|
||||
_caFilePath = caFilePath;
|
||||
|
||||
// make sure url start with 'ws://' or 'wss://'
|
||||
if (_url.size() < 6)
|
||||
if (_url.empty())
|
||||
return false;
|
||||
|
||||
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();
|
||||
@@ -616,11 +616,11 @@ bool WebSocket::init(const Delegate& delegate,
|
||||
}
|
||||
}
|
||||
|
||||
// WebSocket thread needs to be invoked at the end of this method.
|
||||
bool isWebSocketThreadCreated = true;
|
||||
if (__wsHelper == nullptr)
|
||||
{
|
||||
__wsHelper = new (std::nothrow) WsThreadHelper();
|
||||
__wsHelper->createWebSocketThread();
|
||||
isWebSocketThreadCreated = false;
|
||||
}
|
||||
|
||||
WsMessage* msg = new (std::nothrow) WsMessage();
|
||||
@@ -628,6 +628,14 @@ bool WebSocket::init(const Delegate& delegate,
|
||||
msg->user = this;
|
||||
__wsHelper->sendMessageToWebSocketThread(msg);
|
||||
|
||||
// fixed https://github.com/cocos2d/cocos2d-x/issues/17433
|
||||
// createWebSocketThread has to be after message WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION was sent.
|
||||
// And websocket thread should only be created once.
|
||||
if (!isWebSocketThreadCreated)
|
||||
{
|
||||
__wsHelper->createWebSocketThread();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -861,24 +869,11 @@ void WebSocket::onClientOpenConnectionRequest()
|
||||
_readyState = State::CONNECTING;
|
||||
_readyStateMutex.unlock();
|
||||
|
||||
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))
|
||||
{
|
||||
LOGE("lws_parse_uri failed: %s", _url.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
LOGD("protocol: %s, host: %s, port: %d, path: %s\n", prot, address, port, path);
|
||||
Uri uri = Uri::parse(_url);
|
||||
LOGD("scheme: %s, host: %s, port: %d, path: %s\n", uri.getScheme().c_str(), uri.getHostName().c_str(), static_cast<int>(uri.getPort()), uri.getPathEtc().c_str());
|
||||
|
||||
int sslConnection = 0;
|
||||
if (0 == strcmp(prot, "wss"))
|
||||
if (uri.isSecure())
|
||||
sslConnection = LCCSCF_USE_SSL;
|
||||
|
||||
struct lws_vhost* vhost = nullptr;
|
||||
@@ -891,15 +886,25 @@ void WebSocket::onClientOpenConnectionRequest()
|
||||
vhost = createVhost(__defaultProtocols, sslConnection);
|
||||
}
|
||||
|
||||
int port = static_cast<int>(uri.getPort());
|
||||
if (port == 0)
|
||||
port = uri.isSecure() ? 443 : 80;
|
||||
|
||||
const std::string& hostName = uri.getHostName();
|
||||
std::string path = uri.getPathEtc();
|
||||
const std::string& authority = uri.getAuthority();
|
||||
if (path.empty())
|
||||
path = "/";
|
||||
|
||||
struct lws_client_connect_info connectInfo;
|
||||
memset(&connectInfo, 0, sizeof(connectInfo));
|
||||
connectInfo.context = __wsContext;
|
||||
connectInfo.address = address;
|
||||
connectInfo.address = hostName.c_str();
|
||||
connectInfo.port = port;
|
||||
connectInfo.ssl_connection = sslConnection;
|
||||
connectInfo.path = path;
|
||||
connectInfo.host = address;
|
||||
connectInfo.origin = address;
|
||||
connectInfo.path = path.c_str();
|
||||
connectInfo.host = hostName.c_str();
|
||||
connectInfo.origin = authority.c_str();
|
||||
connectInfo.protocol = _clientSupportedProtocols.empty() ? nullptr : _clientSupportedProtocols.c_str();
|
||||
connectInfo.ietf_version_or_minus_one = -1;
|
||||
connectInfo.userdata = this;
|
||||
|
||||
Reference in New Issue
Block a user