Created
May 9, 2022 08:43
-
-
Save warodri-sendbird/374e06ff78346b8334255778bea1c517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Flag to check if the chat is connected or not | |
connected = false; | |
// String with the error (in case of any, when creating a group channel) | |
startConversationResult: string; | |
// List of this signed user's group channels | |
conversations: Array<SendBird.GroupChannel> | null; | |
// String with the error (in case of any, when trying to list our group channels) | |
listConversationsResult: string | null; | |
// Keep track of the selected group channel | |
selectedChannel: SendBird.GroupChannel | null; | |
// The list of messages obtained from the selected group channel | |
messages: Array<SendBird.UserMessage | SendBird.FileMessage | SendBird.AdminMessage> | null; | |
// Input text to send as a message to the group channel | |
textMessage: any; |
This file contains hidden or 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
connect() { | |
this.chatService.connect('sendbird', null, (error: any, user: any) => { | |
if (!error) { | |
// We are connected to Sendbird servers! | |
this.registerEventHandlers(); | |
this.getMyConversations(); | |
this.connected = true; | |
} | |
}); | |
} |
This file contains hidden or 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
getMyConversations() { | |
this.chatService.getMyGroupChannels( | |
( | |
error: SendBird.SendBirdError, | |
groupChannels: Array<SendBird.GroupChannel> | |
) => { | |
if (error) { | |
this.listConversationsResult = 'Unable to get your conversations'; | |
} else { | |
this.conversations = groupChannels; | |
} | |
} | |
); | |
} |
This file contains hidden or 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
export class AppComponent implements OnInit { | |
... | |
} | |
This file contains hidden or 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
getMessages(channel: SendBird.GroupChannel) { | |
this.selectedChannel = channel; | |
this.chatService.getMessagesFromChannel( | |
channel, | |
( | |
error: SendBird.SendBirdError, | |
messages: Array< | |
SendBird.UserMessage | SendBird.FileMessage | SendBird.AdminMessage | |
> | |
) => { | |
if (!error) { | |
this.messages = messages; | |
} | |
} | |
); | |
} |
This file contains hidden or 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
registerEventHandlers() { | |
this.chatService.registerEventHandlers( | |
'123', | |
(data: { event: string; data: any }) => { | |
console.log('New event: ' + data.event, data.data); | |
if (this.selectedChannel) { | |
if (data.event == 'onMessageReceived' && this.messages) { | |
if (data.data.channel.url == this.selectedChannel.url) { | |
this.messages.push(data.data.message); | |
} | |
} | |
} | |
} | |
); | |
} |
This file contains hidden or 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
sendMessage() { | |
this.chatService.sendMessage( | |
this.selectedChannel, | |
this.textMessage, | |
(error: SendBird.SendBirdError, userMessage: SendBird.UserMessage) => { | |
this.getMessages(this.selectedChannel); | |
} | |
); | |
} |
This file contains hidden or 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
startConversation() { | |
let channelName = 'android-tutorial'; | |
let userIds = ['myTestUserId']; | |
this.chatService.createGroupChannel( | |
channelName, | |
userIds, | |
(error: SendBird.SendBirdError, groupChannel: SendBird.GroupChannel) => { | |
if (error) { | |
this.startConversationResult = 'Error creating the conversation'; | |
} else { | |
this.startConversationResult = 'Conversation created'; | |
this.getMyConversations(); | |
} | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment