package com.example.tolucagraphics import android.annotation.SuppressLint import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.tolucagraphics.adapters.JugadoresAdapter import com.example.tolucagraphics.datas.Data import com.example.tolucagraphics.datas.JoinData import com.example.tolucagraphics.datas.JoinTable import com.example.tolucagraphics.events.Events import com.example.tolucagraphics.events.UserElement import com.google.gson.Gson import kotlinx.android.synthetic.main.activity_mesas_players.* import okhttp3.Response import okhttp3.WebSocket import okhttp3.WebSocketListener import okio.ByteString class MesasActivity : AppCompatActivity() { var selector = 1 val gson = Gson() val decoJson = Gson() var eventoRecibido = Events() private val conection2 = LobbyActivity.Conection private lateinit var adapterJugadores : JugadoresAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_mesas_players) conection2.webSocketSingle?.close(1000, "Change Activity") conection2.listener = TestListener() conection2.conectarconWS() /********************PETICION DE MESAS******************/ val joinRoom = JoinData("join_room", Data("1")) val joinRoomJSON = gson.toJson(joinRoom) Log.d("request",joinRoomJSON) conection2.webSocketSingle?.send(joinRoomJSON) btnCreaMesa.setOnClickListener { Log.d("request", "mesnaje a enviar:{\"command\":\"create_room_table\",\"data\":{\"roomId\":\"1\",\"points\":30}} ") conection2.webSocketSingle?.send("{\"command\":\"create_room_table\",\"data\":{\"roomId\":\"1\",\"points\":30}}") } } inner class TestListener : WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) { //Log.d("request", "connection successfully") Log.d("request","vuelto a abrir???") //webSocket.close(1000, "finish") } @SuppressLint("SetTextI18n") override fun onMessage(webSocket: WebSocket, text: String) { //usernameReceived.text = "cargando Rooms" runOnUiThread { Log.d("request", "testRecibe : $text") //--------eventoRecibido = decoJson.fromJson(text, Events::class.java) Log.d("request", "evento Jugadores a EventsClass: $eventoRecibido") //------if (eventoRecibido.type == "TRUCO_ROOM_EVENT") { if (text.contains("TRUCO_ROOM_EVENT", true)) { eventoRecibido = decoJson.fromJson(text, Events::class.java) Log.d("request", "ENTRO AL ADAPTER!!!") val jugadores:ArrayList = eventoRecibido.data?.room?.users as ArrayList Log.d("request", "JUGADORES TIENE ESTO $jugadores") adapterJugadores = JugadoresAdapter(jugadores) lista_jugadores.apply { layoutManager = LinearLayoutManager(this@MesasActivity) adapter = adapterJugadores } //disparaAdapter() }else{ Log.d("request", "MENSAJE: $text") } } } override fun onMessage(webSocket: WebSocket, bytes: ByteString) { super.onMessage(webSocket, bytes) Log.d("request", "testRecibe : $bytes") } override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { Log.d("request","testCierra : $code / $reason") } override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { Log.d("request","Error : " + t.message +response.toString()) LobbyActivity.Conection.conectarconWS() val joinRoom = JoinData("join_room", Data("1")) val joinRoomJSON = gson.toJson(joinRoom) Log.d("request",joinRoomJSON) LobbyActivity.Conection.webSocketSingle?.send(joinRoomJSON) } } }