Skip to content

Instantly share code, notes, and snippets.

@zontan
Created September 9, 2020 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zontan/a80c7021f586d04371d6125469d3e322 to your computer and use it in GitHub Desktop.
Save zontan/a80c7021f586d04371d6125469d3e322 to your computer and use it in GitHub Desktop.
extension CustomAgoraViewController: UITextFieldDelegate {
func addMessage(user: String, message: String) {
let message = "\(user): \(message)"
messageList.append(message)
let indexPath = IndexPath(row: self.messageList.count-1, section: 0)
self.chatTableView.insertRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
self.chatTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let text = textField.text, text != "" {
rtmChannel?.send(AgoraRtmMessage(text: text), completion: { (error) in
if error != .errorOk {
print("Failed to send message: ", error)
} else {
self.addMessage(user: self.currentUser!.displayName ?? self.currentUser!.uid, message: text)
}
})
textField.text = ""
}
return true
}
}
extension CustomAgoraViewController: AgoraRtmChannelDelegate {
func channel(_ channel: AgoraRtmChannel, messageReceived message: AgoraRtmMessage, from member: AgoraRtmMember) {
addMessage(user: member.userId, message: message.text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment