编辑
2024-07-27
后端
00
请注意,本文编写于 287 天前,最后修改于 287 天前,其中某些信息可能已经过时。

SPDY(读作"speedy")是一个由谷歌开发的开放网络传输协议,旨在通过减少网页加载时间来加速网络通信。它最初在2009年被提出,并在2012年首次公开发布。SPDY设计用于减少网页内容加载的延迟,通过多路复用流、优先级排序、服务器推送等技术手段,提高网络传输效率。

SPDY并不是http的替代协议,而是对http1.1的补充升级,现已被http2替代。

在netty中依然提供了对SPDY协议的支持

如下:

java
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.spdy.SpdyFrameCodec; import io.netty.handler.codec.spdy.SpdyHttpDecoder; import io.netty.handler.codec.spdy.SpdyHttpEncoder; import io.netty.handler.codec.spdy.SpdySessionHandler; import io.netty.handler.ssl.ApplicationProtocolConfig; import io.netty.handler.ssl.ApplicationProtocolNames; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.SelfSignedCertificate; import javax.net.ssl.SSLException; import java.security.cert.CertificateException; public class SpdyServer { static final int PORT = Integer.parseInt(System.getProperty("port", "8443")); public static void main(String[] args) throws CertificateException, SSLException, InterruptedException { // 使用自签名证书 SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .applicationProtocolConfig(new ApplicationProtocolConfig( ApplicationProtocolConfig.Protocol.NPN, ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.SPDY_3_1)) .build(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(sslCtx.newHandler(ch.alloc())); pipeline.addLast(new SpdyFrameCodec(SpdyVersion.SPDY_3_1)); pipeline.addLast(new SpdySessionHandler(SpdyVersion.SPDY_3_1, true)); pipeline.addLast(new SpdyHttpDecoder(SpdyVersion.SPDY_3_1, 8192)); pipeline.addLast(new SpdyHttpEncoder(SpdyVersion.SPDY_3_1)); pipeline.addLast(new SpdyServerHandler()); } }); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.spdy.SpdyHttpResponseStreamIdHandler; import io.netty.handler.codec.spdy.SpdySessionHandler; import io.netty.handler.codec.spdy.SpdyStreamFrame; public class SpdyServerHandler extends SimpleChannelInboundHandler<SpdyStreamFrame> { @Override protected void channelRead0(ChannelHandlerContext ctx, SpdyStreamFrame msg) throws Exception { System.out.println("Received SPDY frame: " + msg); ctx.writeAndFlush("Hello, SPDY"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!