mirror of https://github.com/axmolengine/axmol.git
Merge pull request #14818 from WenhaiLin/oh3.10
Fixed WebSocket may automatically disconnected if invoke `send` several times in quick succession on Android.
This commit is contained in:
commit
ba32f4ea2b
|
@ -530,10 +530,11 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lk(_wsHelper->_subThreadWsMessageQueueMutex);
|
std::lock_guard<std::mutex> lk(_wsHelper->_subThreadWsMessageQueueMutex);
|
||||||
|
|
||||||
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
auto iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
||||||
|
|
||||||
int bytesWrite = 0;
|
//To avoid automatically disconnected on Android,send only one WsMessage at a time.
|
||||||
for (; iter != _wsHelper->_subThreadWsMessageQueue->end();)
|
//for (; iter != _wsHelper->_subThreadWsMessageQueue->end();)
|
||||||
|
if (iter != _wsHelper->_subThreadWsMessageQueue->end())
|
||||||
{
|
{
|
||||||
WsMessage* subThreadMsg = *iter;
|
WsMessage* subThreadMsg = *iter;
|
||||||
|
|
||||||
|
@ -546,8 +547,6 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
|
|
||||||
size_t remaining = data->len - data->issued;
|
size_t remaining = data->len - data->issued;
|
||||||
size_t n = std::min(remaining, c_bufferSize );
|
size_t n = std::min(remaining, c_bufferSize );
|
||||||
//fixme: the log is not thread safe
|
|
||||||
// CCLOG("[websocket:send] total: %d, sent: %d, remaining: %d, buffer size: %d", static_cast<int>(data->len), static_cast<int>(data->issued), static_cast<int>(remaining), static_cast<int>(n));
|
|
||||||
|
|
||||||
unsigned char* buf = new unsigned char[LWS_SEND_BUFFER_PRE_PADDING + n + LWS_SEND_BUFFER_POST_PADDING];
|
unsigned char* buf = new unsigned char[LWS_SEND_BUFFER_PRE_PADDING + n + LWS_SEND_BUFFER_POST_PADDING];
|
||||||
|
|
||||||
|
@ -576,20 +575,18 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
||||||
writeProtocol |= LWS_WRITE_NO_FIN;
|
writeProtocol |= LWS_WRITE_NO_FIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, (libwebsocket_write_protocol)writeProtocol);
|
auto bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, (libwebsocket_write_protocol)writeProtocol);
|
||||||
//fixme: the log is not thread safe
|
|
||||||
// CCLOG("[websocket:send] bytesWrite => %d", bytesWrite);
|
|
||||||
|
|
||||||
// Buffer overrun?
|
// Buffer overrun?
|
||||||
if (bytesWrite < 0)
|
if (bytesWrite < 0)
|
||||||
{
|
{
|
||||||
break;
|
//break;
|
||||||
}
|
}
|
||||||
// Do we have another fragments to send?
|
// Do we have another fragments to send?
|
||||||
else if (remaining != n)
|
else if (remaining != n)
|
||||||
{
|
{
|
||||||
data->issued += n;
|
data->issued += n;
|
||||||
break;
|
//break;
|
||||||
}
|
}
|
||||||
// Safely done!
|
// Safely done!
|
||||||
else
|
else
|
||||||
|
|
|
@ -34,10 +34,15 @@ WebSocketTest::WebSocketTest()
|
||||||
itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
||||||
menuRequest->addChild(itemSendText);
|
menuRequest->addChild(itemSendText);
|
||||||
|
|
||||||
|
labelSendText = Label::createWithTTF("Send Multiple Text", "fonts/arial.ttf", 20);
|
||||||
|
itemSendText = MenuItemLabel::create(labelSendText, CC_CALLBACK_1(WebSocketTest::onMenuSendMultipleTextClicked, this));
|
||||||
|
itemSendText->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
||||||
|
menuRequest->addChild(itemSendText);
|
||||||
|
|
||||||
// Send Binary
|
// Send Binary
|
||||||
auto labelSendBinary = Label::createWithTTF("Send Binary", "fonts/arial.ttf", 20);
|
auto labelSendBinary = Label::createWithTTF("Send Binary", "fonts/arial.ttf", 20);
|
||||||
auto itemSendBinary = MenuItemLabel::create(labelSendBinary, CC_CALLBACK_1(WebSocketTest::onMenuSendBinaryClicked, this));
|
auto itemSendBinary = MenuItemLabel::create(labelSendBinary, CC_CALLBACK_1(WebSocketTest::onMenuSendBinaryClicked, this));
|
||||||
itemSendBinary->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
itemSendBinary->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
|
||||||
menuRequest->addChild(itemSendBinary);
|
menuRequest->addChild(itemSendBinary);
|
||||||
|
|
||||||
|
|
||||||
|
@ -210,6 +215,28 @@ void WebSocketTest::onMenuSendTextClicked(cocos2d::Ref *sender)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebSocketTest::onMenuSendMultipleTextClicked(cocos2d::Ref *sender)
|
||||||
|
{
|
||||||
|
if (! _wsiSendText)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_wsiSendText->getReadyState() == network::WebSocket::State::OPEN)
|
||||||
|
{
|
||||||
|
_sendTextStatus->setString("Send Multiple Text WS is waiting...");
|
||||||
|
for (int index = 0; index < 15; ++index) {
|
||||||
|
_wsiSendText->send(StringUtils::format("Hello WebSocket, text message index:%d", index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::string warningStr = "send text websocket instance wasn't ready...";
|
||||||
|
log("%s", warningStr.c_str());
|
||||||
|
_sendTextStatus->setString(warningStr.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WebSocketTest::onMenuSendBinaryClicked(cocos2d::Ref *sender)
|
void WebSocketTest::onMenuSendBinaryClicked(cocos2d::Ref *sender)
|
||||||
{
|
{
|
||||||
if (! _wsiSendBinary) {
|
if (! _wsiSendBinary) {
|
||||||
|
|
|
@ -32,6 +32,7 @@ public:
|
||||||
|
|
||||||
// Menu Callbacks
|
// Menu Callbacks
|
||||||
void onMenuSendTextClicked(cocos2d::Ref *sender);
|
void onMenuSendTextClicked(cocos2d::Ref *sender);
|
||||||
|
void onMenuSendMultipleTextClicked(cocos2d::Ref *sender);
|
||||||
void onMenuSendBinaryClicked(cocos2d::Ref *sender);
|
void onMenuSendBinaryClicked(cocos2d::Ref *sender);
|
||||||
|
|
||||||
virtual std::string title() const override { return "WebSocket Test"; }
|
virtual std::string title() const override { return "WebSocket Test"; }
|
||||||
|
|
Loading…
Reference in New Issue