Skip to content

Instantly share code, notes, and snippets.

@joyceHong0524
Last active February 21, 2023 12:40
Show Gist options
  • Save joyceHong0524/f4a8df4b1dec61aa14d51dbf1af44ef6 to your computer and use it in GitHub Desktop.
Save joyceHong0524/f4a8df4b1dec61aa14d51dbf1af44ef6 to your computer and use it in GitHub Desktop.
ChatRoomActivity.kt
package com.junga.socketio_android
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.junga.socketio_android.model.MessageType
import io.socket.client.IO
import io.socket.client.Socket
import io.socket.emitter.Emitter
import kotlinx.android.synthetic.main.activity_chatroom.*
class ChatRoomActivity : AppCompatActivity(), View.OnClickListener {
val TAG = ChatRoomActivity::class.java.simpleName
lateinit var mSocket: Socket;
lateinit var userName: String;
lateinit var roomName: String;
val gson: Gson = Gson()
//For setting the recyclerView.
val chatList: ArrayList<Message> = arrayListOf();
lateinit var chatRoomAdapter: ChatRoomAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chatroom)
send.setOnClickListener(this)
leave.setOnClickListener(this)
//Get the nickname and roomname from entrance activity.
try {
userName = intent.getStringExtra("userName")!!
roomName = intent.getStringExtra("roomName")!!
} catch (e: Exception) {
e.printStackTrace()
}
//Set Chatroom RecyclerView adapter
chatRoomAdapter = ChatRoomAdapter(this, chatList);
recyclerView.adapter = chatRoomAdapter;
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
//Let's connect to our Chat room! :D
try {
//This address is the way you can connect to localhost with AVD(Android Virtual Device)
mSocket = IO.socket("http://10.0.2.2:3000")
Log.d("success", mSocket.id())
} catch (e: Exception) {
e.printStackTrace()
Log.d("fail", "Failed to connect")
}
mSocket.connect()
//Register all the listener and callbacks here.
mSocket.on(Socket.EVENT_CONNECT, onConnect)
mSocket.on("newUserToChatRoom", onNewUser) // To know if the new user entered the room.
mSocket.on("updateChat", onUpdateChat) // To update if someone send a message to chatroom
mSocket.on("userLeftChatRoom", onUserLeft) // To know if the user left the chatroom.
}
// <----- Callback functions ------->
var onConnect = Emitter.Listener {
//After getting a Socket.EVENT_CONNECT which indicate socket has been connected to server,
//send userName and roomName so that they can join the room.
val data = initialData(userName, roomName)
val jsonData = gson.toJson(data) // Gson changes data object to Json type.
mSocket.emit("subscribe", jsonData)
}
var onNewUser = Emitter.Listener {
val name = it[0] as String //This pass the userName!
val chat = Message(name, "", roomName, MessageType.USER_JOIN.index)
addItemToRecyclerView(chat)
Log.d(TAG, "on New User triggered.")
}
var onUserLeft = Emitter.Listener {
val leftUserName = it[0] as String
val chat: Message = Message(leftUserName, "", "", MessageType.USER_LEAVE.index)
addItemToRecyclerView(chat)
}
var onUpdateChat = Emitter.Listener {
val chat: Message = gson.fromJson(it[0].toString(), Message::class.java)
chat.viewType = MessageType.CHAT_PARTNER.index
addItemToRecyclerView(chat)
}
private fun sendMessage() {
val content = editText.text.toString()
val sendData = SendMessage(userName, content, roomName)
val jsonData = gson.toJson(sendData)
mSocket.emit("newMessage", jsonData)
val message = Message(userName, content, roomName, MessageType.CHAT_MINE.index)
addItemToRecyclerView(message)
}
private fun addItemToRecyclerView(message: Message) {
//Since this function is inside of the listener,
//You need to do it on UIThread!
runOnUiThread {
chatList.add(message)
chatRoomAdapter.notifyItemInserted(chatList.size)
editText.setText("")
recyclerView.scrollToPosition(chatList.size - 1) //move focus on last message
}
}
override fun onClick(p0: View?) {
when (p0!!.id) {
R.id.send -> sendMessage()
R.id.leave -> onDestroy()
}
}
override fun onDestroy() {
super.onDestroy()
val data = initialData(userName, roomName)
val jsonData = gson.toJson(data)
//Before disconnecting, send "unsubscribe" event to server so that
//server can send "userLeftChatRoom" event to other users in chatroom
mSocket.emit("unsubscribe", jsonData)
mSocket.disconnect()
}
}
@Naveedkhan011
Copy link

can you send me link to full code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment