本文主要是介绍第13周-Java网络编程进化史:从IO到NIO再到Netty,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第13周-Java网络编程进化史:从IO到NIO再到Netty
- 一.学习和理解网上参考案例,实践练习其中的demo代码
- 1)分别基于IO、NIO、Netty的Java网络程序(如基于TCP的C/S模式的聊天程序)
- IO:
- 1.实现
- 2.结果
- NIO:
- 1.实现
- 2.结果
- NETTY:
- 1.实现
- 2.结果
- 2)基于Web的聊天室(比如用Springboot+netty实现)
- 1.User类
- 2.SocketSession类
- 3.SessionGroup
- 4.WebSocketTextHandler类
- 5.WebSocketServer类
- 6.index.html
- 二. 动态网页的信息爬取
- 1)对一个网页进行自动化测试。比如自动填充百度网页的查询关键字,完成自动搜索。
- 2)爬取一个动态网页的数据,按附件要求实现代码。
- 3)爬取京东网站上的感兴趣书籍信息(如关键字“python编程”的前200本图书),并保存
- 三.参考
一.学习和理解网上参考案例,实践练习其中的demo代码
1)分别基于IO、NIO、Netty的Java网络程序(如基于TCP的C/S模式的聊天程序)
IO:
1.实现
服务器
package io;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class IOServer {@SuppressWarnings("resource")public static void main(String[] args) throws Exception {ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();//创建socket服务,监听8081端口ServerSocket server=new ServerSocket(8081);System.out.println("服务器启动!");int count=0;while(true){//获取一个套接字(阻塞)final Socket socket = server.accept();System.out.println("欢迎第"+(++count)+"个客户");newCachedThreadPool.execute(new Runnable() {@Overridepublic void run() {//业务处理handler(socket);}});}}/*** 读取数据* @param socket* @throws Exception*/public static void handler(Socket socket){try {byte[] bytes = new byte[1024];InputStream inputStream = socket.getInputStream();while(true){//读取数据(阻塞)int read = inputStream.read(bytes);if(read != -1){System.out.println(new String(bytes, 0, read));}else{break;}}} catch (Exception e) {e.printStackTrace();}finally{try {System.out.println("socket关闭");socket.close();} catch (IOException e) {e.printStackTrace();}}}}
2.客户端
package io;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class IOClient {public static void main(String[] args) throws IOException {//发送十次for (int i=0;i<10;i++){Socket socket=new Socket("192.168.0.178", 8081);//写数据OutputStream os=socket.getOutputStream();os.write(("xyj"+i).getBytes());//释放资源socket.close();}}}
2.结果
NIO:
1.实现
服务器
package nio;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/*** author:醉意丶千层梦*/public class NIOServer {// 通道管理器private Selector selector;/*** 启动服务端测试** @throws IOException*/public static void main(String[] args) throws IOException {NIOServer server = new NIOServer();server.initServer(8081);server.listen();}/*** 获得一个ServerSocket通道,并对该通道做一些初始化的工作** @param port* 绑定的端口号* @throws IOException*/public void initServer(int port) throws IOException {// 获得一个ServerSocket通道ServerSocketChannel serverChannel = ServerSocketChannel.open();// 设置通道为非阻塞serverChannel.configureBlocking(false);// 将该通道对应的ServerSocket绑定到port端口serverChannel.socket().bind(new InetSocketAddress(port));// 获得一个通道管理器this.selector = Selector.open();// 将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后,// 当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。serverChannel.register(selector, SelectionKey.OP_ACCEPT);}/*** 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理** @throws IOException*/public void listen() throws IOException {System.out.println("服务端启动成功!");// 轮询访问selectorwhile (true) {// 当注册的事件到达时,方法返回;否则,该方法会一直阻塞selector.select();// 获得selector中选中的项的迭代器,选中的项为注册的事件Iterator<?> ite = this.selector.selectedKeys().iterator();while (ite.hasNext()) {SelectionKey key = (SelectionKey) ite.next();// 删除已选的key,以防重复处理ite.remove();handler(key);}}}/*** 处理请求** @param key* @throws IOException*/public void handler(SelectionKey key) throws IOException {// 客户端请求连接事件if (key.isAcceptable()) {handlerAccept(key);// 获得了可读的事件} else if (key.isReadable()) {handelerRead(key);}}/*** 处理连接请求** @param key* @throws IOException*/public void handlerAccept(SelectionKey key) throws IOException {ServerSocketChannel server = (ServerSocketChannel) key.channel();// 获得和客户端连接的通道SocketChannel channel = server.accept();// 设置成非阻塞channel.configureBlocking(false);// 在这里可以给客户端发送信息哦System.out.println("新的客户端连接");// 在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。channel.register(this.selector, SelectionKey.OP_READ);}/*** 处理读的事件** @param key* @throws IOException*/public void handelerRead(SelectionKey key) throws IOException {// 服务器可读取消息:得到事件发生的Socket通道SocketChannel channel = (SocketChannel) key.channel();// 创建读取的缓冲区ByteBuffer buffer = ByteBuffer.allocate(1024);int read = channel.read(buffer);if(read > 0){byte[] data = buffer.array();String msg = new String(data).trim();System.out.println("服务端收到信息:" + msg);//回写数据ByteBuffer outBuffer = ByteBuffer.wrap("好的".getBytes());channel.write(outBuffer);// 将消息回送给客户端}else{System.out.println("客户端关闭");key.cancel();}}
}
客户端
package nio;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;public class NIOClient {public static void main(String[] args) throws Exception {final int count[]=new int[1];count[0]=1;for(int i=0;i<5;i++){new Thread(new Runnable() {@Overridepublic void run() {SocketChannel socketChannel = null;//发送的数据String str = "xyj"+count[0]++;ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());//接受的数据ByteBuffer buffer = ByteBuffer.allocate(1024);try {//建立连接socketChannel = SocketChannel.open();socketChannel.configureBlocking(false);if (!socketChannel.connect(new InetSocketAddress("127.0.0.1", 8081))) {//等待连接while (!socketChannel.finishConnect()) {}}//写入数据socketChannel.write(byteBuffer);} catch (IOException e) {e.printStackTrace();}//10s后自动断开连接int time=1;while (time<10){time++;try {//读取数据int read=socketChannel.read(buffer);if(read > 0) {byte[] data = buffer.array();String msg = new String(data).trim();System.out.println("客户端收到信息:" + msg);}Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}try {socketChannel.close();} catch (IOException e) {e.printStackTrace();}}}).start();Thread.sleep(100);}}}
2.结果
NETTY:
1.实现
服务器
package netty;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.Scanner;
/*** author:醉意丶千层梦*/public class NettyServer {public static void main(String[] args) {//用于处理服务器端接收客户端连接NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);//进行网络通信(读写)NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {//辅助工具类,用于服务器通道的一系列配置ServerBootstrap bootstrap = new ServerBootstrap();//绑定两个线程组bootstrap.group(bossGroup,workerGroup)//设置boss selector建立channel使用的对象.channel(NioServerSocketChannel.class)//boss 等待连接的 队列长度.option(ChannelOption.SO_BACKLOG,1024)//处理消息对象.childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {//创建管道ChannelPipeline pipeline = ch.pipeline();//解码方式pipeline.addLast("decoder",new StringDecoder());//编码方式pipeline.addLast("encoder",new StringEncoder());//自定义处理消息对象pipeline.addLast(new ServerHandler());}});System.out.println("服务器正在启动");//绑定端口号ChannelFuture cf = bootstrap.bind(8083).sync();cf.addListener(cd->{if(cd.isSuccess()){System.out.println("启动成功");}else{System.out.println("启动失败");}});//服务端给所有客户端发信息Scanner scanner = new Scanner(System.in);while (scanner.hasNextLine()){String msg = scanner.nextLine();ServerHandler.sendAll(msg);}//阻塞当前线程cf.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}}
package com.company;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
/*** author:醉意丶千层梦*/public class ServerHandler extends SimpleChannelInboundHandler<String> {private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {Channel channel = ctx.channel();System.out.println(channel.remoteAddress()+" == " +msg);channelGroup.forEach(ch->{if (channel!=ch) {ch.writeAndFlush("[ 客户端 ]" + channel.remoteAddress() + "发送了消息 : " + msg + "\n");}else{ch.writeAndFlush("[ 我 ] 发送了消息: " + msg + "\n");}});}//用于服务端发信息给所有客户端public static void sendAll(String msg){channelGroup.forEach(channel -> {channel.writeAndFlush("服务器: "+msg+"\n");});}/*** 当有新的用户连接触发* @param ctx*/public void channelActive(ChannelHandlerContext ctx){Channel channel = ctx.channel();channelGroup.writeAndFlush("[ 客户端 ]"+channel.remoteAddress()+" 上线了 "+sf.format(new Date())+"\n");//把新来的连接加入channelGroup.add(channel);System.out.println(ctx.channel().remoteAddress()+" 上线了" + "\n");}/*** 当用户断开连接触发* @param ctx*/public void channelInactive(ChannelHandlerContext ctx) {Channel channel = ctx.channel();channelGroup.writeAndFlush("[ 客户端 ] " +channel.remoteAddress()+ " 下线了"+"\n");System.out.println(channel.remoteAddress()+" 下线了.\n");System.out.println("channelGroup size = "+ channelGroup.size());}
}
客户端
package netty;import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
/*** author:醉意丶千层梦*/public class ServerHandler extends SimpleChannelInboundHandler<String> {private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {Channel channel = ctx.channel();System.out.println(channel.remoteAddress()+" == " +msg);channelGroup.forEach(ch->{if (channel!=ch) {ch.writeAndFlush("[ 客户端 ]" + channel.remoteAddress() + "发送了消息 : " + msg + "\n");}else{ch.writeAndFlush("[ 我 ] 发送了消息: " + msg + "\n");}});}//用于服务端发信息给所有客户端public static void sendAll(String msg){channelGroup.forEach(channel -> {channel.writeAndFlush("服务器: "+msg+"\n");});}/*** 当有新的用户连接触发* @param ctx*/public void channelActive(ChannelHandlerContext ctx){Channel channel = ctx.channel();channelGroup.writeAndFlush("[ 客户端 ]"+channel.remoteAddress()+" 上线了 "+sf.format(new Date())+"\n");//把新来的连接加入channelGroup.add(channel);System.out.println(ctx.channel().remoteAddress()+" 上线了" + "\n");}/*** 当用户断开连接触发* @param ctx*/public void channelInactive(ChannelHandlerContext ctx) {Channel channel = ctx.channel();channelGroup.writeAndFlush("[ 客户端 ] " +channel.remoteAddress()+ " 下线了"+"\n");System.out.println(channel.remoteAddress()+" 下线了.\n");System.out.println("channelGroup size = "+ channelGroup.size());}
}
package com.company;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;public class ClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {System.out.println(msg.trim());}}
2.结果
2)基于Web的聊天室(比如用Springboot+netty实现)
1.User类
import java.util.Objects;public class User {public String id;public String nickname;public User(String id, String nickname) {super();this.id = id;this.nickname = nickname;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}@Overridepublic boolean equals(Object o) {if (this == o)return true;if (o == null || getClass() != o.getClass())return false;User user = (User) o;return id.equals(user.getId());}@Overridepublic int hashCode() {return Objects.hash(id);}public String getUid() {return id;}
}
2.SocketSession类
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;import java.util.HashMap;
import java.util.Map;
import java.util.UUID;public class SocketSession {public static final AttributeKey<SocketSession> SESSION_KEY = AttributeKey.valueOf("SESSION_KEY");/*** 用户实现服务端会话管理的核心*/
// 通道private Channel channel;// 用户private User user;// session唯一标示private final String sessionId;private String group;/*** session中存储的session 变量属性值*/private Map<String, Object> map = new HashMap<String, Object>();public SocketSession(Channel channel) {//注意传入参数channel。不同客户端会有不同channelthis.channel = channel;this.sessionId = buildNewSessionId();channel.attr(SocketSession.SESSION_KEY).set(this);}// 反向导航public static SocketSession getSession(ChannelHandlerContext ctx) {//注意ctx,不同的客户端会有不同ctxChannel channel = ctx.channel();return channel.attr(SocketSession.SESSION_KEY).get();}// 反向导航public static SocketSession getSession(Channel channel) {return channel.attr(SocketSession.SESSION_KEY).get();}public String getId() {return sessionId;}private static String buildNewSessionId() {String uuid = UUID.randomUUID().toString();return uuid.replaceAll("-", "");}public synchronized void set(String key, Object value) {map.put(key, value);}public synchronized <T> T get(String key) {return (T) map.get(key);}public boolean isValid() {return getUser() != null ? true : false;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String getGroup() {return group;}public void setGroup(String group) {this.group = group;}public Channel getChannel() {return channel;}
}
3.SessionGroup
import com.google.gson.Gson;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.ImmediateEventExecutor;
import org.springframework.util.StringUtils;import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;public final class SessionGroup {private static SessionGroup singleInstance = new SessionGroup();// 组的映射private ConcurrentHashMap<String, ChannelGroup> groupMap = new ConcurrentHashMap<>();public static SessionGroup inst() {return singleInstance;}public void shutdownGracefully() {Iterator<ChannelGroup> groupIterator = groupMap.values().iterator();while (groupIterator.hasNext()) {ChannelGroup group = groupIterator.next();group.close();}}public void sendToOthers(Map<String, String> result, SocketSession s) {// 获取组ChannelGroup group = groupMap.get(s.getGroup());if (null == group) {return;}Gson gson=new Gson();String json = gson.toJson(result);// 自己发送的消息不返回给自己
// Channel channel = s.getChannel();// 从组中移除通道
// group.remove(channel);ChannelGroupFuture future = group.writeAndFlush(new TextWebSocketFrame(json));future.addListener(f -> {System.out.println("完成发送:"+json);
// group.add(channel);//发送消息完毕重新添加。});}public void addSession(SocketSession session) {String groupName = session.getGroup();if (StringUtils.isEmpty(groupName)) {// 组为空,直接返回return;}ChannelGroup group = groupMap.get(groupName);if (null == group) {group = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);groupMap.put(groupName, group);}group.add(session.getChannel());}/*** 关闭连接, 关闭前发送一条通知消息*/public void closeSession(SocketSession session, String echo) {ChannelFuture sendFuture = session.getChannel().writeAndFlush(new TextWebSocketFrame(echo));sendFuture.addListener(new ChannelFutureListener() {public void operationComplete(ChannelFuture future) {System.out.println("关闭连接:"+echo);future.channel().close();}});}/*** 关闭连接*/public void closeSession(SocketSession session) {ChannelFuture sendFuture = session.getChannel().close();sendFuture.addListener(new ChannelFutureListener() {public void operationComplete(ChannelFuture future) {System.out.println("发送所有完成:"+session.getUser().getNickname());}});}/*** 发送消息* @param ctx 上下文* @param msg 待发送的消息*/public void sendMsg(ChannelHandlerContext ctx, String msg) {ChannelFuture sendFuture = ctx.writeAndFlush(new TextWebSocketFrame(msg));sendFuture.addListener(f -> {//发送监听System.out.println("对所有发送完成:"+msg);});}
}
4.WebSocketTextHandler类
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;import java.util.HashMap;
import java.util.Map;public class WebSocketTextHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {SocketSession session = SocketSession.getSession(ctx);TypeToken<HashMap<String, String>> typeToken = new TypeToken<HashMap<String, String>>() {};Gson gson=new Gson();java.util.Map<String,String> map = gson.fromJson(msg.text(), typeToken.getType());User user = null;switch (map.get("type")) {case "msg":Map<String, String> result = new HashMap<>();user = session.getUser();result.put("type", "msg");result.put("msg", map.get("msg"));result.put("sendUser", user.getNickname());SessionGroup.inst().sendToOthers(result, session);break;case "init":String room = map.get("room");session.setGroup(room);String nick = map.get("nick");user = new User(session.getId(), nick);session.setUser(user);SessionGroup.inst().addSession(session);break;}}@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {// 是否握手成功,升级为 Websocket 协议if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {// 握手成功,移除 HttpRequestHandler,因此将不会接收到任何消息// 并把握手成功的 Channel 加入到 ChannelGroup 中new SocketSession(ctx.channel());} else if (evt instanceof IdleStateEvent) {IdleStateEvent stateEvent = (IdleStateEvent) evt;if (stateEvent.state() == IdleState.READER_IDLE) {System.out.println("bb22");}} else {super.userEventTriggered(ctx, evt);}}
}
5.WebSocketServer类
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;import java.util.concurrent.TimeUnit;public class WebSocketServer {private static WebSocketServer wbss;private static final int READ_IDLE_TIME_OUT = 60; // 读超时private static final int WRITE_IDLE_TIME_OUT = 0;// 写超时private static final int ALL_IDLE_TIME_OUT = 0; // 所有超时public static WebSocketServer inst() {return wbss = new WebSocketServer();}public void run(int port) {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer <SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();// Netty自己的http解码器和编码器,报文级别 HTTP请求的解码和编码pipeline.addLast(new HttpServerCodec());// ChunkedWriteHandler 是用于大数据的分区传输// 主要用于处理大数据流,比如一个1G大小的文件如果你直接传输肯定会撑暴jvm内存的;// 增加之后就不用考虑这个问题了pipeline.addLast(new ChunkedWriteHandler());// HttpObjectAggregator 是完全的解析Http消息体请求用的// 把多个消息转换为一个单一的完全FullHttpRequest或是FullHttpResponse,// 原因是HTTP解码器会在每个HTTP消息中生成多个消息对象HttpRequest/HttpResponse,HttpContent,LastHttpContentpipeline.addLast(new HttpObjectAggregator(64 * 1024));// WebSocket数据压缩pipeline.addLast(new WebSocketServerCompressionHandler());// WebSocketServerProtocolHandler是配置websocket的监听地址/协议包长度限制pipeline.addLast(new WebSocketServerProtocolHandler("/ws", null, true, 10 * 1024));// 当连接在60秒内没有接收到消息时,就会触发一个 IdleStateEvent 事件,// 此事件被 HeartbeatHandler 的 userEventTriggered 方法处理到pipeline.addLast(new IdleStateHandler(READ_IDLE_TIME_OUT, WRITE_IDLE_TIME_OUT, ALL_IDLE_TIME_OUT, TimeUnit.SECONDS));// WebSocketServerHandler、TextWebSocketFrameHandler 是自定义逻辑处理器,pipeline.addLast(new WebSocketTextHandler());}});Channel ch = b.bind(port).syncUninterruptibly().channel();ch.closeFuture().syncUninterruptibly();// 返回与当前Java应用程序关联的运行时对象Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic void run() {SessionGroup.inst().shutdownGracefully();bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}});}
}
6.index.html
<!DOCTYPE HTML>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>群聊天室</title><style type="text/css">body {margin-right:50px;margin-left:50px;}.ddois {position: fixed;left: 120px;bottom: 30px;}</style>
</head>
<body>
群名:<input type="text" id="room" name="group" placeholder="请输入群">
<br /><br />
昵称:<input type="text" id="nick" name="name" placeholder="请输入昵称">
<br /><br />
<button type="button" onclick="enter()">进入聊天群</button>
<br /><br />
<div id="message"></div>
<br /><br />
<div class="ddois"><textarea name="send" id="text" rows="10" cols="30" placeholder="输入发送消息"></textarea><br /><br /><button type="button" onclick="send()">发送</button>
</div>
<script type="text/javascript">var webSocket;if (window.WebSocket) {webSocket = new WebSocket("ws://localhost:8088/ws");} else {alert("抱歉,您的浏览器不支持WebSocket协议!");}//连通之后的回调事件webSocket.onopen = function() {console.log("已经连通了websocket");
// setMessageInnerHTML("已经连通了websocket");};//连接发生错误的回调方法webSocket.onerror = function(event){console.log("出错了");
// setMessageInnerHTML("连接失败");};//连接关闭的回调方法webSocket.onclose = function(){console.log("连接已关闭...");}//接收到消息的回调方法webSocket.onmessage = function(event){console.log("bbdds");var data = JSON.parse(event.data)var msg = data.msg;var nick = data.sendUser;switch(data.type){case 'init':console.log("mmll");break;case 'msg':console.log("bblld");setMessageInnerHTML(nick+": "+msg);break;default:break;}}function enter(){var map = new Map();var nick=document.getElementById('nick').value;var room=document.getElementById('room').value;map.set("type","init");map.set("nick",nick);console.log(room);map.set("room",room);var message = Map2Json(map);webSocket.send(message);}function send() {var msg = document.getElementById('text').value;var nick = document.getElementById('nick').value;console.log("1:"+msg);if (msg != null && msg != ""){var map = new Map();map.set("type","msg");map.set("msg",msg);var map2json=Map2Json(map);if (map2json.length < 8000){console.log("4:"+map2json);webSocket.send(map2json);}else {console.log("文本太长了,少写一点吧😭");}}}//将消息显示在网页上function setMessageInnerHTML(innerHTML) {document.getElementById("message").innerHTML += innerHTML + "<br/>";}function Map2Json(map) {var str = "{";map.forEach(function (value, key) {str += '"'+key+'"'+':'+ '"'+value+'",';})str = str.substring(0,str.length-1)str +="}";return str;}</script></body>
</html>
二. 动态网页的信息爬取
1)对一个网页进行自动化测试。比如自动填充百度网页的查询关键字,完成自动搜索。
driver = webdriver.Chrome("E:\GoogleDownload\chromedriver_win32\chromedriver.exe")
# 名言所在网站
driver.get("http://quotes.toscrape.com/js/")
# 表头
csvHeaders = ['作者','名言']
# 所有数据
subjects = []
# 单个数据
subject=[]
# 获取所有含有quote的标签
res_list=driver.find_elements_by_class_name("quote")# 分离出需要的内容
for tmp in res_list:subject.append(tmp.find_element_by_class_name("author").text)subject.append(tmp.find_element_by_class_name("text").text)print(subject)subjects.append(subject)subject=[]
2)爬取一个动态网页的数据,按附件要求实现代码。
3)爬取京东网站上的感兴趣书籍信息(如关键字“python编程”的前200本图书),并保存
driver = webdriver.Chrome("E:\GoogleDownload\chromedriver_win32\chromedriver.exe")
driver.set_window_size(1920,1080)
# 京东网站
driver.get("https://www.jd.com/")# 输入需要查找的关键字
key=driver.find_element_by_id("key").send_keys("python编程")
time.sleep(1)# 点击搜素按钮
button=driver.find_element_by_class_name("button").click()
time.sleep(1)# 获取所有窗口
windows = driver.window_handles
# 切换到最新的窗口
driver.switch_to.window(windows[-1])
time.sleep(1)# js语句
js = 'return document.body.scrollHeight'
# 获取body高度
max_height = driver.execute_script(js)
max_height=(int(max_height/1000))*1000
# 当前滚动条高度
tmp_height=1000
# 所有书籍的字典
res_dict={}# 需要爬取的数量
num=200
while len(res_dict)<num:# 当切换网页后重新设置高度tmp_height = 1000while tmp_height < max_height:# 向下滑动js = "window.scrollBy(0,1000)"driver.execute_script(js)tmp_height += 1000# 书籍列表J_goodsList = driver.find_element_by_id("J_goodsList")ul = J_goodsList.find_element_by_tag_name("ul")# 所有书籍res_list = ul.find_elements_by_tag_name("li")# 把没有记录过的书籍加入字典for res in res_list:# 以书名为键,价格为值# 两种方式获取指定标签值res_dict[res.find_element_by_class_name('p-name').find_element_by_tag_name('em').text] \= res.find_element_by_xpath("//div[@class='p-price']//i").textif len(res_dict)==num:breaktime.sleep(2)if len(res_dict) == num:break# 下一页按钮所在父标签J_bottomPage=driver.find_element_by_id("J_bottomPage")# 下一页按钮next_button=J_bottomPage.find_element_by_class_name("pn-next").click()# 切换窗口windows = driver.window_handlesdriver.switch_to.window(windows[-1])time.sleep(3)# 表头
csvHeaders = ['书名','价格']
# 所有书籍
csvRows=[]
# 书籍
row=[]# 字典转列表
for key,value in res_dict.items():row.append(key)row.append(value)csvRows.append(row)row=[]
# 保存爬取结果
with open('./output/jd_books.csv', 'w', newline='') as file:fileWriter = csv.writer(file)fileWriter.writerow(csvHeaders)fileWriter.writerows(csvRows)
三.参考
读书笔记:《Netty进阶之路》——Netty服务端、Netty客户端、优雅关闭
Netty3学习笔记(一) — 传统IO与NIO比较
Java基于IO、NIO和Netty实现简单C/S聊天
springboot和netty整合的聊天室–群聊
这篇关于第13周-Java网络编程进化史:从IO到NIO再到Netty的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!