title: WebSocket Implementation in Android using Kotlin
body: <p>I am trying to implement simple message transferring using web sockets in android using Kotlin.
  I am new to kotlin and migrated my code manually from a working java code.
  But my code does not work, nothing shows - even after using the <code>network_security_config</code> as </p>
  
  <pre><code>&lt;network-security-config&gt;
  &lt;base-config cleartextTrafficPermitted="true" &gt;
  &lt;/base-config&gt;
  </code></pre>
  
  <p></p>
  
  <p>And my code for the websocket implementation is -></p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
      private lateinit var client: OkHttpClient
      private var startButton: Button? = null
      private var outputText: TextView? = null
  
  
      private inner class EchoWebSocketListener : WebSocketListener() {
          private var NORMAL_CLOSURE_STATUS: Int = 1000
  
          override fun onOpen(webSocket: WebSocket, response: Response) {
              webSocket.send("Hola there!")
              webSocket.send("cool")
              webSocket.close(NORMAL_CLOSURE_STATUS,"GoodBye!!")
          }
  
          override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
              output("Receiving "+ bytes.toString())
          }
  
          override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
              webSocket.close(NORMAL_CLOSURE_STATUS, null)
              output("Closing : "+ code + "/" + reason)
          }
  
          override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
              output("Error : " + t.message)
          }
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          startButton = findViewById&lt;Button&gt;(R.id.start)
          outputText = findViewById&lt;TextView&gt;(R.id.output)
          client = OkHttpClient()
          startButton!!.setOnClickListener{
              start()
          }
      }
  
      private fun start() {
          val request: Request = Request.Builder().url("ws://echo.websocket.org").build()
          val listener: EchoWebSocketListener = EchoWebSocketListener()
          val ws: WebSocket = client.newWebSocket(request, listener)
          client.dispatcher().executorService().shutdown()
      }
  
      private fun output(s: String) {
          runOnUiThread({
              object : Runnable {
                  override fun run() {
                      outputText!!.setText(outputText!!.text.toString() + "\n\n" + s)
                  }
              }
          })
      }
  }
  </code></pre>
  
  <p>Any help is very much appreciated</p>