package com.example.tolucagraphics import android.annotation.SuppressLint import android.content.Intent import android.os.Bundle import android.util.Log import android.view.View import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import com.example.tolucagraphics.datas.GetRooms import com.example.tolucagraphics.events.Events import com.google.gson.Gson import kotlinx.android.synthetic.main.activity_lobby.* import okhttp3.* import java.util.concurrent.TimeUnit class LobbyActivity : AppCompatActivity(){ val conection = Conection private val SERVER_PATH = "ws://3.222.70.21:5000/ws?" lateinit var webSocket : WebSocket var username : String? = null private val gson = Gson() var eventReceived : Events?= null lateinit var campoIDroom : TextView lateinit var campotestNameRoom : TextView @SuppressLint("SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_lobby) username = intent.getStringExtra("USERNAME") initiateSocketConnection() // inicia conexion con websocket\ /*********peticion de ROOMS**********/ val getRooms = GetRooms("get_rooms") val getRoomsJSON = gson.toJson(getRooms) Log.d("request",getRoomsJSON) //webSocket.send(getRoomsJSON) //webSocket.close(1000, "finish") conection.webSocketSingle?.send(getRoomsJSON) btnSelect.setOnClickListener { actualizarView() val segundaPantalla = Intent(this, MesasActivity::class.java) startActivity(segundaPantalla) } //////////////////////webSocket.close(1000, "Finish") } private fun actualizarView() { lobbyRooms.visibility = View.INVISIBLE } private fun initiateSocketConnection() { ////////////////////////// conection.SERVER_PATH = SERVER_PATH conection.username = username conection.realizarConex() /*val wsListener = WSListener () webSocket = conection.client.newWebSocket(conection.request!!, wsListener) // realiza la conexion webSocket*/ conection.listener = WSListener() conection.conectarconWS() } inner class WSListener : WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) { //Log.d("request", "connection successfully") Log.d("request","Conection Success desde onOpen") //webSocket.close(1000, "finish") } @SuppressLint("SetTextI18n") override fun onMessage(webSocket: WebSocket, text: String) { //usernameReceived.text = "cargando Rooms" Log.d("request", "Receiving : $text") val gsonreceived = Gson() runOnUiThread { eventReceived = gsonreceived.fromJson(text, Events::class.java) campoIDroom = findViewById(R.id.campoID) campotestNameRoom = findViewById(R.id.campoNAME) if (eventReceived != null) { campoIDroom.text = eventReceived?.data?.rooms?.get(0)?.id ?: "0" campotestNameRoom.text = eventReceived?.data?.rooms?.get(0)?.name ?: "-" } } } override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { Log.d("request","Closing : $code / $reason") } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { Log.d("request","Error : " + t.message) } } object Conection{ var username: String?= null var SERVER_PATH: String?= null val client = OkHttpClient.Builder() .readTimeout(1, TimeUnit.SECONDS) .build() var request : Request?=null var listener : WebSocketListener?=null fun realizarConex() { request = Request.Builder() .url(SERVER_PATH + username) .build() } var webSocketSingle : WebSocket?=null fun conectarconWS(){ webSocketSingle = client.newWebSocket(request!!, listener!!) } } }