阅读背景:

使用netty的java中的SPDY客户端和服务器

来源:互联网 
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();
     }
}
Server

import java.util.concurrent.atomic.Atom



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: