broadcast
介绍
广播是通信方式之一,广播也称推送。常用于主动给一个、多个或全服的用户发送数据。比如:
- 一个 : 给指定的在线用户发送一些奖励。
- 多个 : 给在同一个房间内的用户广播一些数据,如某一个用户射击子弹,把这子弹的数据广播给房间内的其他用户。
- 全服 : 给全服的所有在线用户广播消息,如广播公告、即将停服维护等。
提示
通信模型的请求参数支持 int、long、bool、String、Object、 List<Integer>、List<Long>、List<Boolean>、List<String>、List<YourClass>
Example Source Code
see https://github.com/iohao/ionet-examples
path : ionet-cookbook-code
- BroadcastAction
广播给指定单个用户
广播给指定单个用户, api 名字以 broadcastUser 打头。
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);
}
}
广播给指定多个用户
广播给指定多个用户, api 名字以 broadcastUsers 打头。
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUsers)
private void triggerBroadcastUsers(List<Long> userIdList) {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUsers(userIdList, BroadcastCmd.broadcastUsersObject, dataObject);
}
}
广播给所有人
广播给所有人, api 名字以 broadcastMulticast 打头。
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastMulticast)
private void triggerBroadcastMulticast() {
var communication = CommunicationKit.getCommunication();
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastMulticast(BroadcastCmd.broadcastMulticastObject, dataObject);
}
}
广播的路由建议
使用广播起始标记递增的方式来指定广播路由,这种方式可以让我们不需要关注具体的路由值。
public interface BroadcastCmd {
int cmd = CmdModule.broadcastCmd;
// ---------- broadcastUser ----------
int triggerBroadcastUser = 1;
...
AtomicInteger inc = new AtomicInteger(10);
private static CmdInfo ofBroadcastCmd() {
return CmdInfo.of(cmd, inc.getAndIncrement());
}
// ---------- broadcastUser ----------
CmdInfo broadcastUserEmpty = ofBroadcastCmd();
CmdInfo broadcastUserInt = ofBroadcastCmd();
CmdInfo broadcastUserBool = ofBroadcastCmd();
CmdInfo broadcastUserLong = ofBroadcastCmd();
}
// test
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
var communication = CommunicationKit.getCommunication();
// ---------- empty ----------
communication.broadcastUser(userId, BroadcastCmd.broadcastUserEmpty);
// ---------- int ----------
int dataInt = inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserInt, dataInt);
// ---------- boolean ----------
boolean dataBool = inc.getAndIncrement() % 2 == 0;
communication.broadcastUser(userId, BroadcastCmd.broadcastUserBool, dataBool);
}
}
More Examples
更多使用示例,请阅读 Examples Communication Broadcast。
开启广播日志
注意
该功能为企业级功能
开启广播的日志后,可以获取广播的代码定位。
@ActionController(BroadcastCmd.cmd)
public class BroadcastAction {
AtomicInteger inc = new AtomicInteger();
@ActionMethod(triggerBroadcastUser)
private void triggerBroadcastUser(long userId) {
// ---------- object ----------
BookMessage dataObject = new BookMessage();
dataObject.authorName = "ionet";
dataObject.bookName = "book-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserObject, dataObject);
// ---------- string ----------
String dataString = "ionet-" + inc.getAndIncrement();
communication.broadcastUser(userId, BroadcastCmd.broadcastUserString, dataString);
}
}
点击控制台打印的 BroadcastAction.java:45 就可以跳转到对应的广播业务代码。
- CN
- EN
┏━━━━━ 广播. [(BroadcastAction.java:11)] ━━━ [cmd:7-15 458767]
┣ userId: 1378604058
┣ 数据: BookMessage(bookName=book-19, authorName=ionet)
┣ 时间: 2025-10-29 13:07:25
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ 广播. [(BroadcastAction.java:15)] ━━━ [cmd:7-14 458766]
┣ userId: 1378604058
┣ 数据: ionet-18
┣ 时间: 2025-10-29 13:07:25
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ Broadcast. [(BroadcastAction.java:11)] ━━━ [cmd:7-15 458767]
┣ userId: 1378604058
┣ Data: BookMessage(bookName=book-19, authorName=ionet)
┣ Time: 2025-10-29 13:03:46
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━ Broadcast. [(BroadcastAction.java:15)] ━━━ [cmd:7-14 458766]
┣ userId: 1378604058
┣ Data: ionet-18
┣ Time: 2025-10-29 13:03:46
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
x