Kotlin WebSocket लाइब्रेरी: Ktor WebSocket
\n\nWebSocket एक प्रोटोकॉल है जो क्लाइंट और सर्वर के बीच स्थायी द्विदिश कनेक्शन प्रदान करता है। Kotlin इकोसिस्टम में, WebSocket के साथ काम करने के लिए सबसे शक्तिशाली और लोकप्रिय लाइब्रेरी में से एक Ktor है - JetBrains द्वारा बनाया गया एक एसिंक्रोनस फ्रेमवर्क। Ktor सर्वर और क्लाइंट दोनों पक्षों पर WebSocket का पूर्ण समर्थन प्रदान करता है, जिससे रीयल-टाइम एप्लिकेशन बनाना आसान हो जाता है: चैट, सूचनाएं, ऑनलाइन गेम, डेटा ट्रैकर और बहुत कुछ।
\n\nइस लेख में, हम विस्तार से देखेंगे कि Ktor का उपयोग करके Kotlin में WebSocket का उपयोग कैसे करें। आप सीखेंगे कि निर्भरताएँ कैसे सेट करें, WebSocket सर्वर और क्लाइंट कैसे बनाएं, संदेश भेजें और प्राप्त करें, साथ ही त्रुटियों और कनेक्शन बंद करने को कैसे संभालें।
\n\nइंस्टॉलेशन और सेटअप
\n\nशुरू करने के लिए, build.gradle.kts (या build.gradle) फ़ाइल में आवश्यक निर्भरताएँ जोड़ें। यदि आप Gradle का उपयोग कर रहे हैं, तो निम्नलिखित मॉड्यूल शामिल करें:
// build.gradle.kts\ndependencies {\n // सर्वर पक्ष\n implementation("io.ktor:ktor-server-core:2.3.0")\n implementation("io.ktor:ktor-server-netty:2.3.0")\n implementation("io.ktor:ktor-server-websockets:2.3.0")\n\n // क्लाइंट पक्ष\n implementation("io.ktor:ktor-client-core:2.3.0")\n implementation("io.ktor:ktor-client-cio:2.3.0")\n implementation("io.ktor:ktor-client-websockets:2.3.0")\n}\n\nMaven के लिए समान आर्टिफैक्ट का उपयोग करें। Ktor संस्करण 2.x.x के सभी मॉड्यूल Kotlin Coroutines का समर्थन करते हैं और JVM, Android, iOS और अन्य प्लेटफार्मों पर काम करते हैं।
\n\nKtor पर WebSocket सर्वर बनाना
\n\nहम एक सरल इको-सर्वर लिखेंगे जो क्लाइंट से संदेश प्राप्त करता है और उन्हें वापस भेजता है। सर्वर पोर्ट 8080 पर सुनेगा।
\n\nimport io.ktor.server.engine.*\nimport io.ktor.server.netty.*\nimport io.ktor.server.websocket.*\nimport io.ktor.websocket.*\nimport kotlinx.coroutines.*\n\nfun main() {\n embeddedServer(Netty, port = 8080) {\n install(WebSockets)\n routing {\n webSocket("/chat") {\n send("चैट में आपका स्वागत है!")\n for (frame in incoming) {\n if (frame is Frame.Text) {\n val text = frame.readText()\n println("प्राप्त: $text")\n send("इको: $text")\n }\n }\n }\n }\n }.start(wait = true)\n}\n\nइस उदाहरण में:
\n- \n
install(WebSockets)- सर्वर में WebSocket प्लगइन जोड़ता है। \n webSocket("/chat")- कनेक्शन के लिए endpoint परिभाषित करता है। \n send()- क्लाइंट को संदेश भेजता है। \n incoming- वह चैनल जिसके माध्यम से क्लाइंट से फ्रेम आते हैं। \n - फ्रेम टेक्स्ट (
Frame.Text), बाइनरी (Frame.Binary) या नियंत्रण प्रकार के हो सकते हैं। \n
Ktor पर WebSocket क्लाइंट बनाना
\n\nअब हम एक क्लाइंट लिखेंगे जो हमारे सर्वर से जुड़ता है, अभिवादन भेजता है और उत्तर पढ़ता है।
\n\nimport io.ktor.client.*\nimport io.ktor.client.engine.cio.*\nimport io.ktor.client.plugins.websocket.*\nimport io.ktor.websocket.*\nimport kotlinx.coroutines.*\n\nsuspend fun main() {\n val client = HttpClient(CIO) {\n install(WebSockets)\n }\n\n client.webSocket("ws://localhost:8080/chat") {\n // अभिवादन पढ़ें\n val welcome = incoming.receive() as Frame.Text\n println("सर्वर: ${welcome.readText()}")\n\n // संदेश भेजें\n send(Frame.Text("नमस्ते, सर्वर!"))\n\n // इको-उत्तर की प्रतीक्षा करें\n val response = incoming.receive() as Frame.Text\n println("सर्वर: ${response.readText()}")\n\n // कनेक्शन बंद करें\n close()\n }\n\n client.close()\n}\n\nक्लाइंट द्वि