使用基于 Java 的 netty 套接字 io 客户端向所有浏览器发送消息

发布于 2024-12-25 04:23:35 字数 1085 浏览 0 评论 0原文

我目前正在尝试向基于 java 的套接字 io socket.io 客户端的所有浏览器发送消息-netty。但目前它一次仅向一个浏览器发送消息。 片段如下:

public class IOClientListener implements INSIOHandler {
    private INSIOClient ioClient=null;
    private final static Logger log=LoggerFactory.getLogger(IOClientListener.class);

    @Override
    public void OnConnect(INSIOClient client) {
        System.out.println("A user connected :: " + client.getSessionID());
                client.send("Hey you are connected to myhope.com");

    }

    @Override
    public void OnDisconnect(INSIOClient client) {
        System.out.println("A user disconnected :: " + client.getSessionID() + " :: hope it was fun");  
    }

    @Override
    public void OnMessage(INSIOClient client, String message) {
        System.out.println("A message received:: " + message + " :: hope it was fun");
    }

    @Override
    public void OnShutdown() {
    }

    public INSIOClient getIOClient(){
        return this.ioClient;
    }
}

我想知道如何使用此客户端广播消息。提前致谢

I'm currently trying to send a message to all the browsers form java based client for socket io socket.io-netty.But currently it is sending message to only one browser at a time.
the snippent is following:

public class IOClientListener implements INSIOHandler {
    private INSIOClient ioClient=null;
    private final static Logger log=LoggerFactory.getLogger(IOClientListener.class);

    @Override
    public void OnConnect(INSIOClient client) {
        System.out.println("A user connected :: " + client.getSessionID());
                client.send("Hey you are connected to myhope.com");

    }

    @Override
    public void OnDisconnect(INSIOClient client) {
        System.out.println("A user disconnected :: " + client.getSessionID() + " :: hope it was fun");  
    }

    @Override
    public void OnMessage(INSIOClient client, String message) {
        System.out.println("A message received:: " + message + " :: hope it was fun");
    }

    @Override
    public void OnShutdown() {
    }

    public INSIOClient getIOClient(){
        return this.ioClient;
    }
}

I want to know how to broadcast the message using this client. Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2025-01-01 04:23:35

您可以使用 Netty ChannelGroup 来实现此目的

....
 static ChannelGroup allClientChannels = new DefaultChannelGroup();


 @Override
public void OnConnect(INSIOClient client) {
    System.out.println("A user connected :: " + client.getSessionID());
            client.send("Hey you are connected to myhope.com");

   allClientChannels.add(client.getCTX().getChannel());
}


@Override
public void OnDisconnect(INSIOClient client) {
    System.out.println("A user disconnected :: " + client.getSessionID() + " :: hope it was fun");
    allClientChannels.remove(client.getCTX().getChannel());

}

...

//when you want to send a broadcase message

allClientChannels.write(yourMessage);

有关更多详细信息,请查看 ChannelGroup API 文档

you can use Netty ChannelGroup for this purpose

....
 static ChannelGroup allClientChannels = new DefaultChannelGroup();


 @Override
public void OnConnect(INSIOClient client) {
    System.out.println("A user connected :: " + client.getSessionID());
            client.send("Hey you are connected to myhope.com");

   allClientChannels.add(client.getCTX().getChannel());
}


@Override
public void OnDisconnect(INSIOClient client) {
    System.out.println("A user disconnected :: " + client.getSessionID() + " :: hope it was fun");
    allClientChannels.remove(client.getCTX().getChannel());

}

...

//when you want to send a broadcase message

allClientChannels.write(yourMessage);

For more detail, have a look on ChannelGroup API Doc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文