वेब अनुप्रयोगों में रीयल-टाइम निर्माण के लिए WebSocket Kotlin लाइब्रेरीज़

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

WebSocket Kotlin लाइब्रेरीज़: रीयल-टाइम अनुप्रयोग बनाना



WebSocket एक प्रोटोकॉल है जो क्लाइंट और सर्वर के बीच स्थायी द्विदिश कनेक्शन प्रदान करता है। HTTP के विपरीत, जहाँ अनुरोध क्लाइंट द्वारा भेजा जाता है और सर्वर उत्तर देता है, WebSocket सर्वर को बिना अनुरोध के डेटा भेजने की अनुमति देता है। यह इसे चैट, ऑनलाइन गेम, स्टॉक कोटेशन, सूचनाएँ और अन्य प्रणालियों के लिए आदर्श बनाता है जहाँ तत्काल डेटा स्थानांतरण की आवश्यकता होती है।



Kotlin, एक आधुनिक JVM भाषा होने के नाते, WebSocket के साथ काम करने के लिए कई शक्तिशाली लाइब्रेरीज़ प्रदान करता है। इस लेख में हम तीन सबसे लोकप्रिय विकल्पों पर विचार करेंगे: Ktor, OkHttp और kweb।



1. Ktor – सर्वर और क्लाइंट के लिए फ्रेमवर्क



Ktor JetBrains का एक एसिंक्रोनस फ्रेमवर्क है जो पूरी तरह से Kotlin में लिखा गया है। यह सर्वर और क्लाइंट दोनों पक्षों पर WebSocket का समर्थन करता है। Ktor कोरूटीन का उपयोग करता है, जो कोड को संक्षिप्त और कुशल बनाता है।



स्थापना (Gradle)

dependencies {    implementation("io.ktor:ktor-server-core:2.3.0")    implementation("io.ktor:ktor-server-netty:2.3.0")    implementation("io.ktor:ktor-server-websockets:2.3.0")    implementation("io.ktor:ktor-client-core:2.3.0")    implementation("io.ktor:ktor-client-cio:2.3.0")    implementation("io.ktor:ktor-client-websockets:2.3.0")}


Ktor पर सर्वर का उदाहरण

import io.ktor.server.engine.*import io.ktor.server.netty.*import io.ktor.server.routing.*import io.ktor.server.websocket.*import io.ktor.websocket.*

fun main() { embeddedServer(Netty, port = 8080) { install(WebSockets) routing { webSocket("/chat") { send(Frame.Text("चैट में आपका स्वागत है!")) for (frame in incoming) { if (frame is Frame.Text) { val text = frame.readText() send(Frame.Text("आपने लिखा: $text")) } } } } }.start(wait = true)}


Ktor पर क्लाइंट का उदाहरण

import io.ktor.client.*import io.ktor.client.engine.cio.*import io.ktor.client.plugins.websocket.*import io.ktor.websocket.*import kotlinx.coroutines.*

fun main() = runBlocking { val client = HttpClient(CIO) { install(WebSockets) } client.webSocket("ws://localhost:8080/chat") { send(Frame.Text("नमस्ते, सर्वर!")) for (frame in incoming) { if (frame is Frame.Text) { println("उत्तर: ${frame.readText()}") } } } client.close()}


Ktor के लाभ:

  • Kotlin कोरूटीन का पूर्ण समर्थन
  • बिना ब्लॉकिंग के एसिंक्रोनस प्रसंस्करण
  • सर्वर और क्लाइंट के लिए एकीकृत सिंटैक्स
  • SSL, संपीड़न और अन्य प्रोटोकॉल का अंतर्निहित समर्थन


2. OkHttp – विश्वसनीय HTTP/WebSocket क्लाइंट



OkHttp Square की एक लोकप्रिय लाइब्रेरी है, जो मूल रूप से HTTP पर केंद्रित थी, लेकिन WebSocket के लिए उत्कृष्ट समर्थन रखती है। इसका उपयोग Android विकास और सर्वर अनुप्रयोगों में किया जाता है। OkHttp सिंक्रोनस और एसिंक्रोनस दोनों तरह से काम करता है, पुनः कनेक्शन और कनेक्शन पूल का समर्थन करता है।



स्थापना (Gradle)

dependencies {    implementation("com.squareup.okhttp3:okhttp:4.12.0")}


OkHttp पर क्लाइंट का उदाहरण

import okhttp3.*

fun main() { val client = OkHttpClient() val request = Request.Builder().url("ws://echo.websocket.org").build() val ws = client.newWebSocket(request, object : WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) { webSocket.send("OkHttp से नमस्ते!") } override fun onMessage(webSocket: WebSocket, text: String) {

अनुशंसाएँ