I am trying to send message from netty server to connected client.
What I want to do is very similar with this question . However, I am using io.netty (the one with org.jboss.netty.* package) version 3.10.0.Final
Here is what I have done :
TelnetServer.java contains code for initiate the server.
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContext.newServerContext(ssc.certificate(),
ssc.privateKey());
} else {
sslCtx = null;
}
// Configure the server.
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Configure the pipeline factory.
bootstrap.setPipelineFactory(new TelnetServerPipelineFactory(sslCtx));
// Bind and start to accept incoming connections.
Channel channel = bootstrap.bind(new InetSocketAddress(PORT));
}
TelentServerPipelineFactory.java :
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.ssl.SslContext;
import static org.jboss.netty.channel.Channels.*;
/**
* Creates a newly configured {@link ChannelPipeline} for a new channel.
*/
public class TelnetServerPipelineFactory implements ChannelPipelineFactory {
private final SslContext sslCtx;
public TelnetServerPipelineFactory(SslContext sslCtx) {
this.sslCtx = sslCtx;
}
public ChannelPipeline getPipeline() {
// Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline();
if (sslCtx != null) {
pipeline.addLast("ssl", sslCtx.newHandler());
}
// Add the text line codec combination first,
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new TelnetServerHandler());
pipeline.addLast("downstream", new TelnetServerOutgoingHandler());
return pipeline;
}
}
and in the TelnetServerHandler.java , I stored the reference of a channel when it is bounded:
public class TelnetServerHandler extends SimpleChannelUpstreamHandler {
private static Channel channel;
public static Channel getChannel() {
return channel;
}
.
.
.
.
@Override
public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
// TODO Auto-generated method stub
channel = ctx.getChannel();
}
}
I've tried to send a message outside TelnetServerHandler using this code :
Scanner scanner = new Scanner(System.in);
System.out.printf("Type message : ");
message = scanner.nextLine();
try{
if(channel.isWritable()){
ChannelFuture future = TelnetServerHandler.getChannel().write(message);
future.sync();
if(!future.isSuccess()){
System.out.println("Send failed : " + future.getCause());
}
}
}catch (Exception e) {
e.printStackTrace();
}
It prints no error. When I debug, future.isSucess() returns true value. The client is connected to the server. However, the client never received the message from server.
Did I miss something here? Any help is very appreciated. Thanks in advance! :)
I've found the solution.
Change :
ChannelFuture future = TelnetServerHandler.getChannel().write(message);
To :
ChannelFuture future = TelnetServerHandler.getChannel().write(message+"\r\n");
This link points out that it needs line delimiter so the decoder will read out message correctly.
Hope it helps. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.