Java-java的nio服务器,在chrome内核访问时出现seletionkey重复
while (true) {
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
iterator.remove();
if (selectionKey.isAcceptable()) {
System.out.println("Accept!" + selectionKey);
SocketChannel socketChannel = ((ServerSocketChannel)
selectionKey.channel()).accept();
if (null != socketChannel) {
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
}
}
else if (selectionKey.isReadable()) {
System.out.println("Read!" + selectionKey);
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
if (socketChannel != null) {
socketChannel.configureBlocking(false);
ByteBuffer input = ByteBuffer.allocate(1024);
input.clear();
socketChannel.read(input);
CoreController.getInstance().dispatch(
new RequestEvent(socketChannel, selectionKey, input));
}
selectionKey.interestOps(selectionKey.interestOps()
& ~SelectionKey.OP_READ);
}
}
selectedKeys.clear();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看你的前台代码怎么写的了,真的要用NIO给浏览器做socket 建议用websocket+NIO框架。只是学习的话,debug一下你的demo代码。多找几个demo看看~