Netty服务器如何将数据发送到不同的客户端?

时间:2014-10-31 12:36:17

标签: java netty

我想做一些简单的聊天。 服务器必须在客户端列表中添加新客户端,当一个客户端向服务器发送消息时,服务器必须将此消息重新发送给其他客户端。 我知道,如何从客户端阅读消息,但我不知道如何从服务器向客户端发送消息。而且我不确定应该在哪里列出客户端,但在处理程序类中猜测。 这是我的主要类,它初始化服务器类

package firstPackage;

public class main {

    public static void main(String[] args) throws Exception
    {
    Server server = new Server(9050);
    server.run();
    }
}

这是Server类

package firstPackage;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.channel.socket.SocketChannel;


public class Server {

private int port;

public Server(int port)
{
    this.port=port;
}

public void run() throws Exception
{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try{
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup,workerGroup)
         .channel(NioServerSocketChannel.class)
         .childHandler(new ChannelInitializer<SocketChannel>() {
             @Override
             public void initChannel(SocketChannel ch) throws Exception{
                 ch.pipeline().addLast(new DiscardServerHandler());
             }
         })
         .option(ChannelOption.SO_BACKLOG,128)
         .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    }
    finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

}

这里是Handler类

package firstPackage;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            System.out.print((char) in.readByte());
            System.out.flush();
        }
        System.out.println();

        ctx.writeAndFlush("hey"); // вот здесь я думал, что сообщение будет отправлятся клиенту, от которого я получил сообщение, но не отправляется

    } finally {
        ReferenceCountUtil.release(msg);
    }
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel is active");
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel is invactive");
}

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    System.out.println("handler added");
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    ctx.close();
}
}

实际上现在我还没有客户列表,因为我甚至都不知道这个列表必须包含什么类型的对象,在C#中它是Socket对象,那么在Netty中呢?

1 个答案:

答案 0 :(得分:1)

我投注this example应该会帮助你(这是一段YouTube视频,所以请不要忘记有关已损坏的链接的评论)。特别是,它使用ChannelGroup来解决您提出的问题。而且,是的,它位于服务器端的处理程序中。

编辑:

另请注意,在示例中ChannelGroup是静态的。我可能会争辩说不使用静态成员并从服务器类向处理程序注入ChannelGroup会更好,但如果你只是想让某些东西快速运行,那么静态成员的简单性可能更好。 / p>

相关问题