Documentation

Socket extends ReadableStream, WritableStream
in

A `ReadableStream` allows reading byte streams in chunks.

Example

function readAll(ReadableStream $source): string {
    $buffer = "";

    while (null !== $chunk = $source->read()) {
        $buffer .= $chunk;
    }

    return $buffer;
}

Table of Contents

Methods

close()  : void
Closes the resource, marking it as unusable.
end()  : void
Marks the stream as no longer writable.
getLocalAddress()  : SocketAddress
getRemoteAddress()  : SocketAddress
getTlsInfo()  : TlsInfo|null
getTlsState()  : TlsState
isClosed()  : bool
Returns whether this resource has been closed.
isReadable()  : bool
isTlsConfigurationAvailable()  : bool
isWritable()  : bool
onClose()  : void
Registers a callback that is invoked when this resource is closed.
read()  : string|null
Reads data from the stream.
setupTls()  : void
shutdownTls()  : void
write()  : void
Writes data to the stream.

Methods

close()

Closes the resource, marking it as unusable.

public close() : void

Whether pending operations are aborted or not is implementation dependent.

end()

Marks the stream as no longer writable.

public end() : void

Note that this is not the same as forcefully closing the stream. This method waits for all pending writes to complete before closing the stream. Socket streams implementing this interface should only close the writable side of the stream.

Tags
throws
ClosedException

If the stream has already been closed.

throws
StreamException

If writing to the stream fails.

getTlsInfo()

public getTlsInfo() : TlsInfo|null
Return values
TlsInfo|null

The TLS (crypto) context info if TLS is enabled on the socket or null otherwise.

isClosed()

Returns whether this resource has been closed.

public isClosed() : bool
Return values
bool

true if closed, otherwise false.

isReadable()

public isReadable() : bool
Return values
bool

A stream may become unreadable if the underlying source is closed or lost.

isTlsConfigurationAvailable()

public isTlsConfigurationAvailable() : bool
Return values
bool

isWritable()

public isWritable() : bool
Return values
bool

A stream may no longer be writable if it is closed or ended using end().

onClose()

Registers a callback that is invoked when this resource is closed.

public onClose(callable(): void $onClose) : void
Parameters
$onClose : callable(): void

read()

Reads data from the stream.

public read([Cancellation|null $cancellation = null ][, positive-int|null $limit = null ]) : string|null
Parameters
$cancellation : Cancellation|null = null

Cancel the read operation. The state in which the stream will be after a cancelled operation is implementation dependent.

$limit : positive-int|null = null

Read at most $limit bytes from the socket. null uses an implementation defined limit.

Return values
string|null

Returns a string when new data is available or null if the stream has closed.

setupTls()

public setupTls([Cancellation|null $cancellation = null ]) : void
Parameters
$cancellation : Cancellation|null = null
Tags
throws
SocketException

Socket is closed if setting up TLS fails.

Return values
void

Returns when TLS is successfully set up on the socket.

shutdownTls()

public shutdownTls([Cancellation|null $cancellation = null ]) : void
Parameters
$cancellation : Cancellation|null = null
Tags
throws
SocketException

Socket is closed if shutting down TLS fails.

Return values
void

Returns when TLS is successfully shutdown.

write()

Writes data to the stream.

public write(string $bytes) : void
Parameters
$bytes : string

Bytes to write.

Tags
throws
ClosedException

If the stream has already been closed.

throws
StreamException

If writing to the stream fails.


        
On this page

Search results