File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -70,4 +70,8 @@ void custom_websocket_client::send_message(const std::string& text) {
7070 } else {
7171 std::cerr << " [경고] Cannot send. Connection is not open." << std::endl;
7272 }
73- }
73+ }
74+
75+ bool custom_websocket_client::isReady () const {
76+ return _web_socket.getReadyState () == ix::ReadyState::Open;
77+ }
Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ class custom_websocket_client {
1616 // 외부 쓰레드 안전 송신
1717 void send_message (const std::string& text);
1818
19+ // 연결 상태 확인
20+ bool isReady () const ;
21+
1922private:
2023 void setup_callback ();
2124
Original file line number Diff line number Diff line change @@ -13,14 +13,23 @@ int main() {
1313 std::cout << " --- Enter message to send (type 'exit' to quit) ---" << std::endl;
1414
1515 std::string user_input;
16+ user_input = " hello world" ; // 테스트용 초기 메시지
17+
1618 while (true ) {
17- if (!std::getline (std::cin, user_input)) break ;
18- if (user_input == " exit" ) break ;
19+ // if (!std::getline(std::cin, user_input)) break;
20+ // if (user_input == "exit") break;
1921
2022 if (!user_input.empty ()) {
21- // 쓰레드 세이프하게 메시지 송신. 외부 쓰레드에서 호출해도 됨.
22- client.send_message (user_input);
23+ if (client.isReady ()) {
24+ std::cout << " [송신] " << user_input << std::endl;
25+ client.send_message (user_input);
26+ // 쓰레드 세이프하게 메시지 송신. 외부 쓰레드에서 호출해도 됨.
27+ } else {
28+ std::cerr << " [경고] Connection is not open. Message will be sent when connection is established." << std::endl;
29+ }
2330 }
31+
32+ std::this_thread::sleep_for (std::chrono::milliseconds (1000 )); // CPU 과부하 방지 위해 잠시 대기
2433 }
2534
2635 client.stop (); // 웹소켓 클라이언트 종료
You can’t perform that action at this time.
0 commit comments