# OnlineBattleManager.sendCustomMessage(Object option)
以 Promise 风格调用:支持
# 功能描述
发送自定义消息给房间内或队伍内玩家。接收方会触发 onCustomMessage 事件。
# 参数
# Object option
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| data | Object | 是 | 自定义消息数据 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
# data 对象结构
| 属性 | 类型 | 必填 | 说明 |
| msg | string | 是 | 自定义消息内容(UTF-8字符串,最大2048字节) |
| type | number | 是 | 消息接收者类型:0-房间内所有玩家,1-队伍内所有玩家 |
# 注意事项
- 频率限制:与 updateRoomProperties 共享每秒15次
- 消息内容最大2048字节
- 超过限制会导致调用失败
- 适合用于状态同步、房间通信、游戏事件等场景
# 示例代码
let tapOnlineBattle = tap.getOnlineBattleManager();
// 回调风格
tapOnlineBattle.sendCustomMessage({
data: {
msg: "hello from mini game custom message",
type: 0 // 0-房间内所有玩家,1-队伍内所有玩家
},
success: (res) => {
console.log('发送自定义消息成功');
},
fail: ({errMsg, errno}) => {
console.error('发送自定义消息失败:', errMsg);
}
});
// Promise风格
tapOnlineBattle.sendCustomMessage({
data: {
msg: "hello from mini game custom message",
type: 0
}
}).then(res => {
console.log('发送自定义消息成功');
}).catch(error => {
console.error('发送自定义消息失败:', error);
});
