# Chat message event
When a chat message is received, the method of ChannelHandler is executed through the Channel object. Users can inherit ChannelHandler, define their own EventHandler, and register event handlers to the Channel object using the VChatCloud.connect(handler) method.
# Getting Started
# Inherit ChannelHandler class
First, create a new class that inherits ChannelHandler. The example below uses the name CustomHandler.
class CustomHandler extends ChannelHandler {
/// Run when message is received
void onMessage(ChannelMessageModel message) {
// ...
}
/// Executed when a whisper is received
void onWhisper(ChannelMessageModel message) {
// ...
}
// ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Below, descriptions of each event and example code used in the sample app are attached. You can define the necessary methods by overriding them in a new class.
# Message received event
This is a function that runs when a normal message is received.
- example code
void onMessage(ChannelMessageModel message) {
var chatData = ChatItem.fromChannelMessageModel(message);
_channel.addChatLog(chatData);
}
2
3
4
5
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data
# Whisper Receive Event
This function is executed when a whisper is received.
- example code
void onWhisper(ChannelMessageModel message) {
var chatData = ChatItem.fromChannelMessageModel(message)
..messageType = MessageType.whisper;
_channel.addChatLog(chatData);
}
2
3
4
5
6
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data
# Notice reception event
This function is executed when an operator's announcement is received.
- example code
void onNotice(ChannelMessageModel message) {
var chatData = ChatItem.fromChannelMessageModel(message)
..messageType = MessageType.notice;
_channel.addChatLog(chatData);
}
2
3
4
5
6
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data
# User connection event
This function is executed when a user connects.
- example code
void onJoinUser(ChannelMessageModel message) async {
Map<String, dynamic> data = message. body;
var chatData = ChatItem.fromChannelMessageModel(message)
..messageType = MessageType.join
..clientKey = '';
_channel.addChatLog(chatData);
if (_channel.channel != null) {
_channel.setClientList(await _channel.channel!.requestClientList());
}
}
2
3
4
5
6
7
8
9
10
11
12
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data
# User exit event
This function is executed when the logged in user exits.
- example code
void onLeaveUser(ChannelMessageModel message) {
var chatData = ChatItem.fromChannelMessageModel(message)
..messageType = MessageType.leave
..clientKey = '';
_channel.addChatLog(chatData);
}
2
3
4
5
6
7
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data
# Custom event receive event
A function that is executed when a custom event is received.
- example code
void onCustom(ChannelMessageModel message) {
// Write code for individual CustomEvent
}
2
3
4
parameter value
value identifier Description message ChannelMessageModel (opens new window) Received message data