Module: Async::IO::Peer

Included in:
BasicSocket, SSLSocket, TCPSocket, UNIXSocket
Defined in:
lib/async/io/socket.rb

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Is it likely that the socket is still connected? May return false positive, but won’t return false negative.

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/async/io/socket.rb', line 29

def connected?
  return false if @io.closed?
  
  # If we can wait for the socket to become readable, we know that the socket may still be open.
  result = to_io.recv_nonblock(1, Socket::MSG_PEEK, exception: false)
  
  # Either there was some data available, or we can wait to see if there is data avaialble.
  return !result.empty? || result == :wait_readable
end