| 786 | } |
| 787 | |
| 788 | void WebSocketImpl::close() { |
| 789 | if (_closeState != CloseState::NONE) { |
| 790 | LOGD("close was invoked, don't invoke it again!\n"); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | _closeState = CloseState::SYNC_CLOSING; |
| 795 | LOGD("close: WebSocket (%p) is closing...\n", this); |
| 796 | { |
| 797 | _readyStateMutex.lock(); |
| 798 | if (_readyState == cc::network::WebSocket::State::CLOSED) { |
| 799 | // If readState is closed, it means that onConnectionClosed was invoked in websocket thread, |
| 800 | // but the callback of performInCocosThread has not been triggered. We need to invoke |
| 801 | // onClose to release the websocket instance. |
| 802 | _readyStateMutex.unlock(); |
| 803 | _delegate->onClose(_ws, 1000, "close_normal", true); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | _readyState = cc::network::WebSocket::State::CLOSING; |
| 808 | _readyStateMutex.unlock(); |
| 809 | } |
| 810 | |
| 811 | { |
| 812 | std::unique_lock<std::mutex> lkClose(_closeMutex); |
| 813 | _closeCondition.wait(lkClose); |
| 814 | _closeState = CloseState::SYNC_CLOSED; |
| 815 | } |
| 816 | |
| 817 | // Wait 5 milliseconds for onConnectionClosed to exit! |
| 818 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 819 | _delegate->onClose(_ws, 1000, "close_normal", true); |
| 820 | } |
| 821 | |
| 822 | void WebSocketImpl::closeAsync(int code, const ccstd::string &reason) { |
| 823 | if (_wsInstance) { |
no test coverage detected