java - Server Chat to all Client Online/Connected Netty -


i have little trouble.

i'm stuck @ broadcast message using chat server client using netty,

i'll can chat client server , autoreplay server (using handling), want server can chat client , broadcast client(channel active).

i'm trying copy client, 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 android tag, because later want use code client on android considering nio doesn't work on android, oio work.

i include code of work,

//chatserver.java public class chatserver {     private final int port;      public static void main(string[] args) throws exception {         system.out.println("server started @ " + 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();         (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();         (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");         (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 " + "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());     } } 

ok, found case, must create new thread in order make server can chatting client. create new thread server before it's close sync, im using chatserverbroadcast. call direct handler using static send 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 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 can looking answer in case me.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -