|
| 1 | +package com.github.dsrees.chatexample |
| 2 | + |
| 3 | +import androidx.appcompat.app.AppCompatActivity |
| 4 | +import android.os.Bundle |
| 5 | +import android.util.Log |
| 6 | +import android.widget.ArrayAdapter |
| 7 | +import android.widget.Button |
| 8 | +import android.widget.EditText |
| 9 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 10 | +import kotlinx.android.synthetic.main.activity_main.* |
| 11 | +import org.phoenixframework.Channel |
| 12 | +import org.phoenixframework.Socket |
| 13 | + |
| 14 | +class MainActivity : AppCompatActivity() { |
| 15 | + |
| 16 | + companion object { |
| 17 | + const val TAG = "MainActivity" |
| 18 | + } |
| 19 | + |
| 20 | + private val messagesAdapter = MessagesAdapter() |
| 21 | + private val layoutManager = LinearLayoutManager(this) |
| 22 | + |
| 23 | + |
| 24 | + // Use when connecting to https://github.com/dwyl/phoenix-chat-example |
| 25 | + private val socket = Socket("https://phxchat.herokuapp.com/socket/websocket") |
| 26 | + private val topic = "rooms:lobby" |
| 27 | + |
| 28 | + // Use when connecting to local server |
| 29 | +// private val socket = Socket("ws://10.0.2.2:4000/socket/websocket") |
| 30 | +// private val topic = "room:lobby" |
| 31 | + |
| 32 | + private var lobbyChannel: Channel? = null |
| 33 | + |
| 34 | + private val username: String |
| 35 | + get() = username_input.text.toString() |
| 36 | + |
| 37 | + private val message: String |
| 38 | + get() = message_input.text.toString() |
| 39 | + |
| 40 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 41 | + super.onCreate(savedInstanceState) |
| 42 | + setContentView(R.layout.activity_main) |
| 43 | + |
| 44 | + |
| 45 | + layoutManager.stackFromEnd = true |
| 46 | + |
| 47 | + messages_recycler_view.layoutManager = layoutManager |
| 48 | + messages_recycler_view.adapter = messagesAdapter |
| 49 | + |
| 50 | + socket.onOpen { |
| 51 | + this.addText("Socket Opened") |
| 52 | + runOnUiThread { connect_button.text = "Disconnect" } |
| 53 | + } |
| 54 | + |
| 55 | + socket.onClose { |
| 56 | + this.addText("Socket Closed") |
| 57 | + runOnUiThread { connect_button.text = "Connect" } |
| 58 | + } |
| 59 | + |
| 60 | + socket.onError { throwable, response -> |
| 61 | + Log.e(TAG, "Socket Errored $response", throwable) |
| 62 | + this.addText("Socket Error") |
| 63 | + } |
| 64 | + |
| 65 | + socket.logger = { |
| 66 | + Log.d(TAG, "SOCKET $it") |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + connect_button.setOnClickListener { |
| 71 | + if (socket.isConnected) { |
| 72 | + this.disconnectAndLeave() |
| 73 | + } else { |
| 74 | + this.disconnectAndLeave() |
| 75 | + this.connectAndJoin() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + send_button.setOnClickListener { sendMessage() } |
| 80 | + } |
| 81 | + |
| 82 | + private fun sendMessage() { |
| 83 | + val payload = mapOf("user" to username, "body" to message) |
| 84 | + this.lobbyChannel?.push("new:msg", payload) |
| 85 | + ?.receive("ok") { Log.d(TAG, "success $it") } |
| 86 | + ?.receive("error") { Log.d(TAG, "error $it") } |
| 87 | + |
| 88 | + message_input.text.clear() |
| 89 | + } |
| 90 | + |
| 91 | + private fun disconnectAndLeave() { |
| 92 | + // Be sure the leave the channel or call socket.remove(lobbyChannel) |
| 93 | + lobbyChannel?.leave() |
| 94 | + socket.disconnect { this.addText("Socket Disconnected") } |
| 95 | + } |
| 96 | + |
| 97 | + private fun connectAndJoin() { |
| 98 | + val channel = socket.channel(topic, mapOf("status" to "joining")) |
| 99 | + channel.on("join") { |
| 100 | + this.addText("You joined the room") |
| 101 | + } |
| 102 | + |
| 103 | + channel.on("new:msg") { message -> |
| 104 | + val payload = message.payload |
| 105 | + val username = payload["user"] as? String |
| 106 | + val body = payload["body"] |
| 107 | + |
| 108 | + |
| 109 | + if (username != null && body != null) { |
| 110 | + this.addText("[$username] $body") |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + channel.on("user:entered") { |
| 115 | + this.addText("[anonymous entered]") |
| 116 | + } |
| 117 | + |
| 118 | + this.lobbyChannel = channel |
| 119 | + channel |
| 120 | + .join() |
| 121 | + .receive("ok") { |
| 122 | + this.addText("Joined Channel") |
| 123 | + } |
| 124 | + .receive("error") { |
| 125 | + this.addText("Failed to join channel: ${it.payload}") |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + this.socket.connect() |
| 130 | + } |
| 131 | + |
| 132 | + private fun addText(message: String) { |
| 133 | + runOnUiThread { |
| 134 | + this.messagesAdapter.add(message) |
| 135 | + layoutManager.smoothScrollToPosition(messages_recycler_view, null, messagesAdapter.itemCount) |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments