gpt4 book ai didi

java - Web Socket 和非阻塞模式

转载 作者:行者123 更新时间:2023-11-30 05:07:02 25 4
gpt4 key购买 nike

如果我们将 Socket Channel configureBlocking 设置为 false,那么连接会自动变为双工模式吗?我想说的是,那么,服务器可以异步地将数据推送到客户端浏览器吗?我需要手动编程此行为吗?

最佳答案

ServerSocketChannel和SocketChannel的configureBlocking方法继承自AbstractSelectableChannel类。 configureBlocking 方法返回一个 SelectableChannel 对象。

以下是有关 SelectableChannel 对象的文档的片段:

Blocking mode

A selectable channel is either in blocking mode or in non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode an I/O operation will never block and may transfer fewer bytes than were requested or possibly no bytes at all. The blocking mode of a selectable channel may be determined by invoking its isBlocking method.

Newly-created selectable channels are always in blocking mode. Non-blocking mode is most useful in conjunction with selector-based multiplexing. A channel must be placed into non-blocking mode before being registered with a selector, and may not be returned to blocking mode until it has been deregistered.

新创建的可选 channel 始终处于阻塞模式,因此如果您想要非阻塞双工模式,则必须显式调用configureBlocking(false)。

以下是可能对您有帮助的 Javadoc 列表: http://download.oracle.com/javase/7/docs/api/java/nio/channels/SocketChannel.html
http://download.oracle.com/javase/6/docs/api/java/nio/channels/ServerSocketChannel.html

http://download.oracle.com/javase/7/docs/api/java/nio/channels/SelectableChannel.html

http://download.oracle.com/javase/7/docs/api/java/nio/channels/spi/AbstractSelectableChannel.html#configureBlocking(boolean )

关于java - Web Socket 和非阻塞模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4743068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com