How to implement WebSocket in Kotlin Android
How to implement WebSocket in Kotlin Android.
Here is a detailed step-by-step tutorial on how to implement WebSocket in Kotlin for Android:
Step 1: Add the necessary dependencies
To use WebSocket in your Kotlin Android project, you need to add the necessary dependencies to your project's build.gradle file. Open the build.gradle (app) file and add the following dependencies:
implementation 'org.java-websocket:Java-WebSocket:1.5.1'
Step 2: Create a WebSocket client class
Create a new Kotlin class called WebSocketClient.kt. This class will be responsible for connecting to the WebSocket server and handling incoming messages. Add the following code to the class:
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import java.net.URI
class MyWebSocketClient(serverUri: URI) : WebSocketClient(serverUri) {
override fun onOpen(handshakedata: ServerHandshake?) {
// Called when the WebSocket connection is opened
println("WebSocket connection opened")
}
override fun onClose(code: Int, reason: String?, remote: Boolean) {
// Called when the WebSocket connection is closed
println("WebSocket connection closed")
}
override fun onMessage(message: String?) {
// Called when a message is received from the server
println("Received message: $message")
}
override fun onError(ex: Exception?) {
// Called when an error occurs
println("WebSocket error: ${ex?.message}")
}
}
Step 3: Connect to the WebSocket server
In your activity or fragment, create an instance of the WebSocket client and connect to the WebSocket server. Add the following code to your activity or fragment:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import java.net.URI
class MainActivity : AppCompatActivity() {
private lateinit var webSocketClient: MyWebSocketClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Connect to the WebSocket server
val serverUri = URI("ws://your-websocket-server-url")
webSocketClient = MyWebSocketClient(serverUri)
webSocketClient.connect()
}
override fun onDestroy() {
super.onDestroy()
// Disconnect from the WebSocket server
webSocketClient.close()
}
}
Replace "ws://your-websocket-server-url" with the URL of your WebSocket server.
Step 4: Send and receive messages
To send a message to the WebSocket server, you can call the send
method on the WebSocket client instance. To receive messages from the server, you can handle them in the onMessage
method of the WebSocket client class.
To send a message, add the following code to your activity or fragment:
// Send a message to the WebSocket server
webSocketClient.send("Hello, server!")
To handle incoming messages, modify the onMessage
method in the WebSocket client class:
override fun onMessage(message: String?) {
// Called when a message is received from the server
println("Received message: $message")
// Handle the received message here
}
That's it! You have successfully implemented WebSocket in Kotlin for Android. You can now send and receive messages between your Android app and the WebSocket server.