iOS | Android | macOS | Windows | Electron | Flutter | web |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ | × |
sendCustomCmdMsg
API of TRTCCloud, and the following four parameters need to be specified during sending:Parameter Name | Description |
cmdID | Message ID. Value range: 1–10. Messages in different business types should use different cmdIDs . |
data | Message to be sent, which can contain up to 1 KB (1,000 bytes) of data. |
reliable | Whether reliable sending is enabled; if yes, the receiver needs to temporarily store the data of a certain period to wait for re-sending, which will cause certain delay. |
ordered | Whether orderly sending is enabled, i.e., whether the data should be received in the same order in which it is sent; if yes, the receiver needs to temporarily store and sort messages, which will cause certain delay. |
reliable
and ordered
must be set to the same value (YES
or NO
) and cannot be set to different values currently.// Sample code for sending a custom message- (void)sendHello {// Command word for the custom message. A set of rules needs to be customized according to the business needs. 0x1 is used as an example to send a text broadcast messageNSInteger cmdID = 0x1;NSData *data = [@"Hello" dataUsingEncoding:NSUTF8StringEncoding];// `reliable` and `ordered` need to be consistent for now. Orderly sending is used as an example here[trtcCloud sendCustomCmdMsg:cmdID data:data reliable:YES ordered:YES];}
// Sample code for sending a custom messagepublic void sendHello() {try {// Command word for the custom message. A set of rules needs to be customized according to the business needs. 0x1 is used as an example to send a text broadcast messageint cmdID = 0x1;String hello = "Hello";byte[] data = hello.getBytes("UTF-8");// `reliable` and `ordered` need to be consistent for now. Orderly sending is used as an example heretrtcCloud.sendCustomCmdMsg(cmdID, data, true, true);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}
// Sample code for sending a custom messagevoid sendHello(){// Command word for the custom message. A set of rules needs to be customized according to the business needs. 0x1 is used as an example to send a text broadcast messageuint32_t cmdID = 0x1;uint8_t* data = { '1', '2', '3' };uint32_t dataSize = 3; // Length of data// `reliable` and `ordered` need to be consistent for now. Orderly sending is used as an example heretrtcCloud->sendCustomCmdMsg(cmdID, data, dataSize, true, true);}
// Sample code for sending a custom messageprivate void sendHello(){// Command word for the custom message. A set of rules needs to be customized according to the business needs. 0x1 is used as an example to send a text broadcast messageuint cmdID = 0x1;byte[] data = { '1', '2', '3' };uint dataSize = 3; // Length of data// `reliable` and `ordered` need to be consistent for now. Orderly sending is used as an example heremTRTCCloud.sendCustomCmdMsg(cmdID, data, dataSize, true, true);}
// Sample code for sending a custom messagesendHello() {try {// Command word for the custom message. A set of rules needs to be customized according to the business needs. 0x1 is used as an example to send a text broadcast messageint cmdID = 0x1;String hello = "Hello";// `reliable` and `ordered` need to be consistent for now. Orderly sending is used as an example heretrtcCloud.sendCustomCmdMsg(cmdID, hello, true, true);} catch (e) {print(e);}}
sendCustomCmdMsg
to send a custom message, other users in the room can receive the message through the onRecvCustomCmdMsg
API in the SDK callback.// Receive and process messages sent by other users in the room- (void)onRecvCustomCmdMsgUserId:(NSString *)userId cmdID:(NSInteger)cmdId seq:(UInt32)seq message:(NSData *)message{// Receive the message sent by `userId`switch (cmdId) // `cmdId` agreed upon between sender and receiver{case 0:// Process the message with `cmdId` = 0break;case 1:// Process the message with `cmdId` = 1break;case 2:// Process the message with `cmdId` = 2break;default:break;}}
// Inherit `TRTCCloudListener` and implement the `onRecvCustomCmdMsg` method to receive and process messages sent by others in the roompublic void onRecvCustomCmdMsg(String userId, int cmdId, int seq, byte[] message) {// Receive the message sent by `userId`switch (cmdId) // `cmdId` agreed upon between sender and receiver{case 0:// Process the message with `cmdId` = 0break;case 1:// Process the message with `cmdId` = 1break;case 2:// Process the message with `cmdId` = 2break;default:break;}
// Receive and process messages sent by other users in the roomvoid TRTCCloudCallbackImpl::onRecvCustomCmdMsg(const char* userId, int32_t cmdId, uint32_t seq, const uint8_t* msg, uint32_t msgSize){// Receive the message sent by `userId`switch (cmdId) // `cmdId` agreed upon between sender and receiver{case 0:// Process the message with `cmdId` = 0break;case 1:// Process the message with `cmdId` = 1break;case 2:// Process the message with `cmdId` = 2break;default:break;}}
// Receive and process messages sent by other users in the roompublic void onRecvCustomCmdMsg(string userId, int cmdId, uint seq, byte[] msg, uint msgSize){// Receive the message sent by `userId`switch (cmdId) // `cmdId` agreed upon between sender and receiver{case 0:// Process the message with `cmdId` = 0break;case 1:// Process the message with `cmdId` = 1break;case 2:// Process the message with `cmdId` = 2break;default:break;}}
// Register trtc callbacktrtcCloud.registerListener(_onRtcListener);// Implement the onRecvCustomCmdMsg method to receive and process messages sent by other people in the room_onRtcListener(type, param) async {if (type == TRTCCloudListener.onRecvCustomCmdMsg) {// Receive a message sent by userIdString userId = param['userId'];// cmdId agreed upon by the sender and receiverswitch (param['cmdID']) {case 0:// Process cmdID = 0 messagesbreak;case 1:// Process cmdID = 1 messagesbreak;case 2:// Process cmdID = 2 messagesbreak;default:break;}}}
Was this page helpful?