# Channel Event Delegation

When receiving a chat event, the event is processed through the delegate attribute of the Channel.
Users can create a class that complies with the ChannelDelegate protocol and register it with Channel.delegate to create their event handling code.

# Getting Started

# Inheriting ChannelDelegate Protocol

First, create a new class that inherits the ChannelDelegate protocol. In the example below, we used the name MyChannel.

class MyChannel: ChannelDelegate {
    /// Run when message is received
    func onMessage(_ channelResultModel: ChannelResultModel) {
        // ...
    }
    /// Run upon receipt of whispering
    func onWhisper(_ channelResultModel: ChannelResultModel) {
        // ...
    }
    // ...
}
1
2
3
4
5
6
7
8
9
10
11

Below is a description of each event and an example code used in the sample app. You can define and use the required methods in a new class.

# Message receiving event

A function that is executed when a general message is received.

  • Example Code
// Add chat data to the self.myChatlog array.
private func addMyChatlog(_ channelResultModel: ChannelResultModel) {
    var temp = channelResultModel
    switch temp.address {
    // Remove a type of client key that does not require user-specific information (to maintain message layout)
    case .notice, .join, .leave:
        temp.body["clientKey"] = ""
    default:
        break
    }
    
    if let userModel = userViewModel?.userModel,
       let chatResult = temp.computedChatResult(userModel: userModel, prevChannelResultModel: chatlog.last, nextChannelResultModel: nil) {
        myChatlog.append(chatResult)

        if let langCode = translateUserClientKeyMap[temp.body["clientKey"] as! String],
           let channelKey = chatroomViewModel?.channelKey {
            if chatResult.mimeType != .text {
                return
            }
            
            Task {
                // Users registered with the translateUserClientKeyMap run translations.
                let response = await VChatCloudAPI.googleTranslation(text: chatResult.message, targetLanguageCode: langCode, roomId: channelKey)
                DispatchQueue.main.async {
                    if let text = response?.data {
                        chatResult.message = text
                        chatResult.isTranslated = true
                    }
                }
            }
        }
    }
}

func onMessage(_ channelResultModel: ChannelResultModel) {
    addMyChatlog(channelResultModel)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data

# Whispering Event

A function that is executed upon receipt of a whisper.

  • Example Code
func onWhisper(_ channelResultModel: ChannelResultModel) {
    addMyChatlog(channelResultModel)
}
1
2
3
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data

# Announcement receiving event

Function executed when receiving an operator's notice.

  • Example Code
func onNotice(_ channelResultModel: ChannelResultModel) {
    addMyChatlog(channelResultModel)
}
1
2
3
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data

# User Connection Event

A function that is executed when a user connects.

  • Example Code
func onJoinUser(_ channelResultModel: ChannelResultModel) {
    addMyChatlog(channelResultModel)
}
1
2
3
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data

# User exit event

Function executed when a user who is connecting leaves.

  • Example Code
func onLeaveUser(_ channelResultModel: ChannelResultModel) {
    addMyChatlog(channelResultModel)
}
1
2
3
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data

# Custom Event Receive Event

A function that runs when a custom event is received.

  • Example Code
func onCustom(_ channelResultModel: ChannelResultModel) {
    // Create code for your personal custom event}
1
2
  • Parameter value

    Value Identifier Description
    channelResultModel ChannelResultModel Received Message Data
Copyright 2022. E7Works Inc. & JOYTUNE Corp. All Rights Reserved.