Skip to content

Instantly share code, notes, and snippets.

@fatihkurcenli
Created November 11, 2021 13:36
Show Gist options
  • Save fatihkurcenli/84d7e48c59b5fac7df8d4fb3b34d43ad to your computer and use it in GitHub Desktop.
Save fatihkurcenli/84d7e48c59b5fac7df8d4fb3b34d43ad to your computer and use it in GitHub Desktop.
socket_main_activity.kt
class MainActivity : AppCompatActivity() {
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!
private lateinit var webSocketClient: WebSocketClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.sendButton.setOnClickListener {
sendMessage()
}
}
private fun initWebSocket() {
val webSocketUrl: URI? = URI(WEB_SOCKET_URL)
createWebSocketClient(webSocketUrl)
//Eğer SSL sertifikalı bir websocket dinliyorsak
//SSl ayarlamasını yapıyoruz.
//val socketFactory: SSLSocketFactory = SSLSocketFactory.getDefault() as SSLSocketFactory
//webSocketClient.setSocketFactory(socketFactory)
webSocketClient.connect()
}
private fun createWebSocketClient(webSocketUrl: URI?) {
webSocketClient = object : WebSocketClient(webSocketUrl) {
override fun onOpen(handshakedata: ServerHandshake?) {
Log.d(TAG, "onOpen")
}
override fun onMessage(message: String?) {
Log.d(TAG, "onMessage: $message")
setUpMessage(message)
}
override fun onClose(code: Int, reason: String?, remote: Boolean) {
Log.d(TAG, "onClose")
}
override fun onError(ex: Exception?) {
Log.e(TAG, "onError: ${ex?.message}")
}
}
}
private fun sendMessage() {
webSocketClient.send(
binding.editText.text.toString()
)
binding.editText.text.clear()
}
private fun setUpMessage(message: String?) {
runOnUiThread {
binding.helloWorldText.text = message.toString()
}
}
override fun onResume() {
super.onResume()
initWebSocket()
}
override fun onPause() {
super.onPause()
webSocketClient.close()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
companion object {
//IPv4 connect to server url
const val WEB_SOCKET_URL = "ws://${Constants.IP_ADDRESS}:${Constants.PORT}/chat"
const val TAG = "WebSocketTag"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment