Server
import java.util.concurrent.atomic.AtomicReference;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.spdy.DefaultSpdySynStreamFrame;
public class SpdyChannelUpStreamHandler extends SimpleChannelUpstreamHandler {
volatile Channel channel;
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
System.out.println("Channel In Open Stage");
channel = e.getChannel();
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
System.out.println("Channel In Connected Stage");
Channels.write(channel, new DefaultSpdySynStreamFrame(1, 1, (byte)0));
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
System.out.println("Message Received on Server Side");
Channels.write(channel, e.getMessage(), e.getRemoteAddress());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
if (exception.compareAndSet(null, e.getCause())) {
e.getChannel().close();
}
}
}
import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.spdy.SpdyFrameDecoder;
import org.jboss.netty.handler.codec.spdy.SpdyFrameEncoder;
import org.jboss.netty.handler.codec.spdy.SpdySessionHandler;
public class SpdyPipeLineFactory implements ChannelPipelineFactory{
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new SpdyFrameDecoder(2));
pipeline.addLast("encoder", new SpdyFrameEncoder(2));
//pipeline.addLast("sessionHandler",new SpdySessionHandler(2,true));
pipeline.addLast("handler", new SpdyChannelUpStreamHandler());
return pipeline;
}
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class StartServer {
public static void main(String[] args){
ServerBootstrap bootStrapServer = new ServerBootstrap(new
NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootStrapServer.setPipelineFactory(new SpdyPipeLineFactory());
bootStrapServer.bind(new InetSocketAddress(8443));
}
}
这是启用了 SPDY 的服务器示例,我可以通过在 Internet 上的多个位置阅读来使用 netty 库将其组合在一起。当我运行这个服务器并使用 SPDY 客户端连接时,我的连接是成功的,因为我可以在 channelOpen 和 channelConnected 的函数中看到消息。
我想问几个问题,因为我对 SPDY 协议的了解非常有限。我将从我想做的第一件事开始。
1 - 服务器如何将消息发送到客户端,目前我在 channelConnected 方法中执行此操作,我可以在客户端看到,但这给了我发送消息的机会非常有限,并且 channelConnected 事件在通道设置过程中发生一次,
有什么方法可以获取 SPDY 服务器上当前所有打开通道的句柄并识别这些通道,以便我可以按需选择通道并使用它们发送消息?