同步websocket

This commit is contained in:
helloworldlv
2025-09-17 18:43:06 +08:00
parent f48ceda70e
commit add1d1a369
2 changed files with 11 additions and 7 deletions
@@ -85,7 +85,7 @@ namespace laya
m_nWebSocketState = WSS_INIT;
if (Init(p_sUrl))
{
m_nWebSocketState = WSS_OPEN;
//m_nWebSocketState = WSS_OPEN;
}
else
{
@@ -120,7 +120,8 @@ namespace laya
if (!cbref.lock())return;
closeTime = closetm;
LOGI("JSWebSocket::onSocketCloseCallJSFunction this=%x", (long) this);
if (m_nWebSocketState == WSS_OPEN)
//if (m_nWebSocketState == WSS_OPEN || m_nWebSocketState == WSS_CLOSEING)
if (m_nWebSocketState != WSS_CLOSE)
{
m_nWebSocketState = WSS_CLOSE;
m_pJSFunctionOnClose.Call(p_sEvent.c_str());
@@ -136,7 +137,8 @@ namespace laya
{
if (!cbref.lock())
return;
if (m_nWebSocketState == WSS_OPEN)
//if (m_nWebSocketState == WSS_OPEN)
if (m_nWebSocketState != WSS_CLOSE)
{
m_nWebSocketState = WSS_CLOSE;
m_pJSFunctionOnError.Call(p_sEvent.c_str());
+6 -4
View File
@@ -303,8 +303,9 @@ void WebSocket::send(const std::string& message)
msg->what = WS_MSG_TO_SUBTRHEAD_SENDING_STRING;
Data* data = new Data();
data->bytes = new char[message.length()+1];
strcpy(data->bytes, message.c_str());
memcpy(data->bytes, message.c_str(), message.length());
data->len = message.length();
data->bytes[message.length()] = '\0';
msg->obj = data;
m_wsHelper->sendMessageToSubThread(msg);
}
@@ -341,7 +342,7 @@ void WebSocket::close()
m_readyState = State::CLOSED;
m_bWantClose = true;
lws_cancel_service(m_wsContext);
m_wsHelper->joinSubThread();
// onClose callback needs to be invoked at the end of this method
@@ -473,7 +474,7 @@ int WebSocket::onSocketCallback(
int reason,
void *user, void *in, size_t len)
{
//CCLOG("socket callback for %d reason", reason);
//assert(m_wsContext == nullptr || ctx == m_wsContext);
//assert(m_wsInstance == nullptr || wsi == nullptr || wsi == m_wsInstance);
switch (reason)
@@ -482,6 +483,7 @@ int WebSocket::onSocketCallback(
case LWS_CALLBACK_PROTOCOL_DESTROY:
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
{
LOGI("WebSocket onSocketCallback connection error %s %d reason", in != nullptr ? (const char*)in : "", reason);
WsMessage* msg = nullptr;
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && m_readyState == State::CONNECTING)
@@ -619,7 +621,7 @@ int WebSocket::onSocketCallback(
{
// CCLOG("%s", "connection closing..");
LOGI("WebSocket onSocketCallback connection closing %d reason", reason);
m_wsHelper->quitSubThread();
if (m_readyState != State::CLOSED)