websocket网络协议
WebSocket是一种在单个TCP连接上进行全双工通信的网络协议。它提供了一种在客户端和服务器之间进行实时、双向通信的方式,允许服务器主动向客户端推送数据,而不需要客户端发起请求。 ...
WebSocket是一种在单个TCP连接上进行全双工通信的网络协议。它提供了一种在客户端和服务器之间进行实时、双向通信的方式,允许服务器主动向客户端推送数据,而不需要客户端发起请求。 ...
reactor模型 特点 事件驱动 可以处理一个或多个输入源 通过多路复用将请求的事件分发给对应的处理器处理 根据大神Doug Lea 在 《Scalable IO in Java 》中的介绍,Reacotr模型主要分为三个角色 ...
介绍websocket协议 WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通信的网络技术,WebSocket通信协议于2011年被IETF定为 标准RFC6455,WebSocket API被W3C定为标准。 ...
代码 HttpFileServer package http; import echo.server.EchoServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.handler.stream.ChunkedWriteHandler; /** * @author ggBall * @version 1.0.0 * @ClassName HttpFileServer.java * @Description TODO * @createTime 2022年02月08日 09:41:00 */ public class HttpFileServer { private static final String default_url = "/src/main/java/http/file/netty/"; public void run(final int port , final String url) throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss,worker) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { /** * This method will be called once the {@link Channel} was registered. After the method returns this instance * will be removed from the {@link ChannelPipeline} of the {@link Channel}. * * @param ch the {@link Channel} which was registered. * @throws Exception is thrown if an error occurs. In that case the {@link Channel} will be closed. */ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); // 请求消息解码器 ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// 目的是将多个消息转换为单一的request或者response对象 ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//响应解码器 ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//目的是支持异步大文件传输() ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// 业务逻辑 } }); ChannelFuture future = bootstrap.bind("127.0.0.1", port).sync(); System.out.println("http文件服务器,地址:http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); worker.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args.length > 0) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); } } String url = default_url; if (args.length > 1) url = args[1]; new HttpFileServer().run(port, url); } } HttpFileServerHandler ...
Java1.4之前的早期版本,Java对I/O的支持并不完善,开发人员在开发高性能I/O程序的时候,会面临一些巨大的挑战和困难,主要问题如下: ...