Reputation: 503
i have little trouble.
I'm stuck at broadcast message using chat from server to client using Netty,
I'll already can chat from client to server and autoreplay from server (using handling), now what i want is Server can chat like client and broadcast it to all client(channel active).
I'm trying to copy from my client, but it didn't work,
Channel channel = boostrap.bind(port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true) {
channel.writeAndFlush(in.readLine() + "\r\n");
}
Sorry about Android tag, because later I want to use this code Client on Android Considering Nio doesn't work on Android, but Oio work.
i include all code of my work,
//ChatServer.java
public class ChatServer {
private final int port;
public static void main(String[] args) throws Exception {
System.out.println("Server Started at " + InetAddress.getLocalHost().getHostAddress() + " port " + 9999);
new ChatServer(9999).run();
}
public ChatServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap boostrap = new ServerBootstrap()
.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChatServerInitializer());
Channel channel = boostrap.bind(port).sync().channel();
// channel.closeFuture().sync();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true) {
channel.writeAndFlush(in.readLine() + "\r\n");
}
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
.
// ChatServerHandler.java
public class ChatServerhandler extends SimpleChannelInboundHandler<String> {
private static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel incoming = ctx.channel();
for (Channel channel: channels) {
channel.writeAndFlush("[SERVER] " + incoming.remoteAddress() + " has joined \n");
}
channels.add(ctx.channel());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel incoming = ctx.channel();
for (Channel channel: channels) {
channel.flush();
channel.writeAndFlush("[SERVER] " + incoming.remoteAddress() + " has left \n");
}
channels.remove(ctx.channel());
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
Channel incoming = ctx.channel();
System.out.println("[" + incoming.remoteAddress() + "] " + msg + "\n");
for (Channel channel: channels) {
if(channel != incoming) {
channel.writeAndFlush("[" + incoming.remoteAddress() + "] " + msg + "\n");
}
}
}
}
.
//ChatServerInitializer.java
public class ChatServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ChatServerhandler());
}
}
.
//ChatClient.java
public class ChatClient {
private final String host;
private final int port;
public static void main(String[] args) throws Exception {
System.out.println("Client Started, conntected to " + "192.168.0.61:9999");
new ChatClient("192.168.0.61", 9999).run();
}
public ChatClient(String host, int port) {
this.host = host;
this.port = port;
}
private void run() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChatClientInitializer());
Channel channel = bootstrap.connect(host, port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true) {
channel.writeAndFlush(in.readLine() + "\r\n");
}
}finally {
group.shutdownGracefully();
}
}
}
.
// ChatClientHandler.java
public class ChatClienthandler extends SimpleChannelInboundHandler<String>{
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg);
}
}
.
// ChatClientInitializer.java
public class ChatClientInitializer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ChatClienthandler());
}
}
Upvotes: 0
Views: 1807
Reputation: 503
Ok, i found for my case, I must create new thread in order to make server can chatting like client. Create a new thread for server before it's close sync, im using ChatServerBroadcast. then call it direct to handler using static to send to all channels active.
ChatServer.java/run
ChannelFuture f = boostrap.bind(PORT).sync();
ChatServerBroadcast cst = new ChatServerBroadcast();
f.channel().closeFuture().sync();
ChatServerBroadcast.java
public class ChatServerBroadcast implements Runnable{
private String message = "";
public ChatServerBroadcast() {
new Thread(this).start();
}
@Override
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ready to chat ");
while(true) {
try {
message = in.readLine();
} catch (IOException e) {
message = "";
}
if(!message.isEmpty()) {
message = "[SERVER BROADCAST] " + message + "\r\n";
ChatServerhandler.sendServerMessage(message);
message = "";
}
}
}
}
ChatServerHandler.java/sendServeMessage(String)
public static void sendServerMessage(String message) {
if (channels.isEmpty()) {
return;
}
channels.writeAndFlush(message);
}
Maybe it can help someone looking answer in case like me.
Upvotes: 1
Reputation: 23567
You should move your code from handlerAdded to channelActive and from handlerRemoved to channelInactive. This should do the job I think.
Upvotes: 0