LobbyActivity.kt 4.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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!!)

        }
    }
}