C++ में Long polling बनाम WebSocket: रियल-टाइम की लड़ाई
\n\nकल्पना करें: आप C++ में चैट, शेयर बाजार टर्मिनल या सर्वर मॉनिटरिंग सिस्टम लिख रहे हैं। क्लाइंट को तुरंत डेटा प्राप्त करना चाहिए। HTTP अनुरोध "पूछो-जवाब दो" उपयुक्त नहीं हैं - बहुत धीमे और बेकार। दो विकल्प हैं: Long polling (पुराना स्कूल) और WebSocket (आधुनिक मानक)। C++ प्रोजेक्ट के लिए क्या चुनें? आइए वास्तविक उदाहरणों पर समझते हैं।
\n\nC++ में Long polling कैसे काम करता है (और यह अभी भी क्यों जीवित है)
\n\nLong polling सामान्य HTTP के ऊपर रियल-टाइम का अनुकरण है। क्लाइंट अनुरोध भेजता है, सर्वर तुरंत जवाब नहीं देता, बल्कि नया डेटा आने या टाइमआउट होने तक "लटका" रहता है। जैसे ही डेटा आता है - सर्वर जवाब भेजता है, क्लाइंट तुरंत नया अनुरोध भेजता है। निरंतर प्रवाह का भ्रम पैदा होता है।
\n\nC++ में इस दृष्टिकोण को अक्सर क्लाइंट पर libcurl और सर्वर पर किसी HTTP सर्वर (जैसे cpp-httplib या Boost.Beast) के माध्यम से लागू किया जाता है। Boost.Beast पर क्लासिक सर्वर साइड इस प्रकार दिखती है:
#include <boost/beast/core.hpp>\n#include <boost/beast/http.hpp>\n#include <boost/beast/version.hpp>\n#include <boost/asio.hpp>\n#include <queue>\n#include <mutex>\n#include <condition_variable>\n\nnamespace beast = boost::beast;\nnamespace http = beast::http;\nnamespace net = boost::asio;\n\nclass LongPollHandler {\n std::queue<std::string> messages_;\n std::mutex mtx_;\n std::condition_variable cv_;\npublic:\n // थ्रेड-सुरक्षित संदेश जोड़ने की विधि\n void push(const std::string& msg) {\n std::lock_guard<std::mutex> lock(mtx_);\n messages_.push(msg);\n cv_.notify_one();\n }\n\n // Long Polling अनुरोध हैंडलर\n void handle(http::request<http::string_body>& req,\n http::response<http::string_body>& res) {\n std::unique_lock<std::mutex> lock(mtx_);\n // नया संदेश या 30 सेकंड टाइमआउट की प्रतीक्षा करें\n if (cv_.wait_for(lock, std::chrono::seconds(30),\n [this](){ return !messages_.empty(); })) {\n std::string msg = messages_.front();\n messages_.pop();\n res.result(http::status::ok);\n res.body() = msg;\n } else {\n // टाइमआउट - खाली जवाब लौटाएं\n res.result(http::status::no_content);\n res.body() = "";\n }\n res.prepare_payload();\n }\n};\n\nlibcurl पर क्लाइंट इस प्रकार दिखेगा:
\n\n#include <curl/curl.h>\n#include <string>\n#include <iostream>\n\nsize_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {\n ((std::string*)userp)->append((char*)contents, size * nmemb);\n return size * nmemb;\n}\n\nvoid long_poll_loop(CURL* curl, const std::string& url) {\n std::string response;\n while (true) {\n curl_easy_setopt(curl, CURLOPT_URL, url.c_str());\n curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);\n curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);\n curl_easy_setopt(curl, CURLOPT_TIMEOUT, 35L); // सर्वर टाइमआउट से थोड़ा अधिक\n\n CURLcode res = curl_easy_perform(curl);\n if (res == CURLE_OK && !response.empty()) {\n std::cout << "प्राप्त हुआ: " << response << std::endl;\n response.clear();\n // तुरंत नया अनुरोध भेजें\n continue;\n }\n // त्रुटि होने पर 5 सेकंड प्रतीक्षा करें और दोहराएं\n std::this_thread::sleep_for(std::chrono::seconds(5));\n }\n}\n\nC++ में Long polling के लाभ: सरलता, सभी प्रॉक्सी और फायरवॉल के साथ संगतता, विशेष लाइब्रेरी की आवश्यकता नहीं। नुकसान: HTTP हेडर की भारी मात्रा, कनेक्शन स्थापित करने में देरी (TCP ha