LobbyActivity.kt 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
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
11
import com.example.tolucagraphics.events.EventGetRoom
12 13 14 15 16 17 18
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_lobby.*
import okhttp3.*
import java.util.concurrent.TimeUnit

class LobbyActivity : AppCompatActivity(){

19
    val conection = Socket
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    private val SERVER_PATH = "ws://3.222.70.21:5000/ws?"
    var username : String? = null
    private val gson = Gson()
    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 {
78
                val eventReceived = gsonreceived.fromJson(text, EventGetRoom::class.java)
79 80 81
                campoIDroom = findViewById(R.id.campoID)
                campotestNameRoom = findViewById(R.id.campoNAME)
                if (eventReceived != null) {
82 83
                    campoIDroom.text = eventReceived.data?.rooms?.get(0)?.id ?: "0"
                    campotestNameRoom.text = eventReceived.data?.rooms?.get(0)?.name ?: "-"
84 85 86 87 88 89 90 91 92 93 94
                }
            }
        }
        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)
        }
    }
}