-
-
Save zontan/0aee537ce488bac1bfade2b44ac8b19b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var messageList: [String] = [] | |
func addMessage(user: String, message: String) { | |
let message = "\(user): \(message)" | |
messageList.append(message) | |
let indexPath = IndexPath(row: self.messageList.count-1, section: 0) | |
self.tableView.insertRows(at: [indexPath], with: UITableView.RowAnimation.automatic) | |
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true) | |
} | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
if let text = textField.text, text != "" { | |
channel?.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 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
messageList.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) | |
if let chatCell = cell as? ChatTableViewCell { | |
let message = messageList[indexPath.row] | |
chatCell.messageLabel.text = message | |
} | |
return cell | |
} | |
... | |
extension ChatViewController: 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