PHP WebSocket2 Library
PHP5 Library for browsing web
|
Base class for network socket implementations. More...
Public Member Functions | |
setOnReceiveBodyCallback (callable $on_receive_body=null) | |
Sets callback function that will be called when a part of response body is read from socket. More... | |
getOnReceiveBodyCallback () | |
Returns callback function that will be called when a part of response body is read from socket. More... | |
setOnReceiveHeadersCallback (callable $on_receive_headers=null) | |
Sets callback function that will be called when response headers are read from socket. More... | |
getOnReceiveHeadersCallback () | |
Returns callback function that will be called when response headers are read from socket. More... | |
sendRequest (WebRequest $request) | |
setTimeout ($timeout) | |
Sets timeout in seconds for socket operations. More... | |
getTimeout () | |
Returns timeout in seconds for socket operations. More... | |
getAvailableTime () | |
Returns timeout in seconds for socket operations. More... | |
setTimeoutMode ($timeout_mode) | |
Sets timeout calculation mode for socket operations. More... | |
getTimeoutMode () | |
Returns timeout calculation mode for socket operations. More... | |
writeRaw ($data) | |
Public Member Functions inherited from HttpSocket | |
__destruct () | |
Destructor. | |
sendRequest (WebRequest $request) | |
Sends request to socket and returns response. More... | |
writeRaw ($request) | |
Writes raw data to socket. More... | |
Data Fields | |
const | TIMEOUT_MODE_EVERY_OPERATION = 0 |
Timeout calculation mode: check for timeout for each network operation independently. More... | |
const | TIMEOUT_MODE_TOTAL = 1 |
Timeout calculation mode: check for timeout for all network operations in total from open() call. More... | |
const | TIMEOUT_MODE_SUM_OF_OPERATIONS = 2 |
Timeout calculation mode: check for timeout for all network operations in total. More... | |
Protected Member Functions | |
readLength ($length, $headers, WebRequest $request, &$aborted) | |
readAll ($headers, WebRequest $request) | |
setSecure ($secure) | |
Sets socket to secure mode or turns off secure mode. More... | |
isSecure () | |
Returns true if socket is in secure mode. More... | |
writeRawPart ($data) | |
Writes part of raw data to socket. More... | |
startTimeMeasurement ($socket_open=false) | |
Starts time measurement for socket operation (read, write, connect, etc). More... | |
checkPoint () | |
Stops time measurement for socket operation (read, write, connect, etc) and checks for timeout. More... | |
Protected Member Functions inherited from HttpSocket | |
open ($address, $port) | |
Opens socket. More... | |
read ($size) | |
Reads raw data from socket. More... | |
close () | |
Closes socket. | |
isOpen () | |
Returns true if socket is open. More... | |
readChunked ($headers, WebRequest $request) | |
Reads chunked content from socket and puts chunks to single string. More... | |
readLength ($length, $headers, WebRequest $request, &$aborted) | |
Reads data with specified length from socket. If socket closes connection or no more data is available to reach length, throws WebRequestException. More... | |
readAll ($headers, WebRequest $request) | |
Reads all available data from socket until it closes connection. More... | |
readUntil ($text) | |
Reads all available data from socket until specified substring is found inside contents. More... | |
readHeaders () | |
Reads response headers. More... | |
write (WebRequest $request) | |
Writes request to socket. More... | |
Protected Attributes | |
$timeout = 30 | |
$timeout_mode = self::TIMEOUT_MODE_SUM_OF_OPERATIONS | |
$start_time = 0 | |
$socket_open_time = 0 | |
$time_taken = 0 | |
Base class for network socket implementations.
|
protected |
Stops time measurement for socket operation (read, write, connect, etc) and checks for timeout.
WebRequestException | if timeout was detected |
getAvailableTime | ( | ) |
Returns timeout in seconds for socket operations.
Differs from getTimeout when TIMEOUT_MODE_TOTAL or TIMEOUT_MODE_SUM_OF_OPERATIONS mode is selected (see setTimeoutMode). In this case, returns number of seconds that is available for socket operation. For example, if socket open operation took 5 seconds and total timeout was set to 30 seconds, then further calls will have only 25 seconds to complete.
float | Timeout in seconds for socket operations |
getOnReceiveBodyCallback | ( | ) |
Returns callback function that will be called when a part of response body is read from socket.
See also setOnReceiveBodyCallback.
callable | Callback function or null |
getOnReceiveHeadersCallback | ( | ) |
Returns callback function that will be called when response headers are read from socket.
See also setOnReceiveHeadersCallback.
callable | Callback function or null |
getTimeout | ( | ) |
Returns timeout in seconds for socket operations.
Default value is 30 seconds.
int | Timeout in seconds for socket operations |
getTimeoutMode | ( | ) |
Returns timeout calculation mode for socket operations.
See also TIMEOUT_MODE_TOTAL, TIMEOUT_MODE_EVERY_OPERATION, TIMEOUT_MODE_SUM_OF_OPERATIONS. Default value is TIMEOUT_MODE_SUM_OF_OPERATIONS.
int | Timeout calculation mode for socket operations |
|
abstractprotected |
Returns true if socket is in secure mode.
bool | True if socket is in secure mode |
setOnReceiveBodyCallback | ( | callable | $on_receive_body = null | ) |
Sets callback function that will be called when a part of response body is read from socket.
Callback function prototype:
Where: headers - raw response headers data;
data_part - part of response body data;
this - this HttpSocket instance;
request - WebRequest that is being sent.
This function must return true to continue reading from socket or false to abort reading. When this callback is specified, NetworkSocket::sendRequest function will always return just headers string. This will allow to still use HttpRequestManager for automatic redirection, cookies and authentication processing.
callable | $on_receive_body | Callback function or null |
setOnReceiveHeadersCallback | ( | callable | $on_receive_headers = null | ) |
Sets callback function that will be called when response headers are read from socket.
Callback function prototype:
Where: headers - raw response headers data;
this - this HttpSocket instance;
request - WebRequest that is being sent.
This function must return true to continue reading from socket or false to abort reading. When false is returned from callback, NetworkSocket::sendRequest function will return null.
callable | $on_receive_headers | Callback function or null |
|
abstractprotected |
Sets socket to secure mode or turns off secure mode.
bool | $secure | True to set socket to secure mode, false to turn secure mode off |
WebRequestException | in case of errors |
setTimeout | ( | $timeout | ) |
Sets timeout in seconds for socket operations.
Default value is 30 seconds.
int | $timeout | Timeout in seconds for socket operations |
setTimeoutMode | ( | $timeout_mode | ) |
Sets timeout calculation mode for socket operations.
See also TIMEOUT_MODE_TOTAL, TIMEOUT_MODE_EVERY_OPERATION, TIMEOUT_MODE_SUM_OF_OPERATIONS. Default value is TIMEOUT_MODE_SUM_OF_OPERATIONS.
int | $timeout_mode | Timeout calculation mode for socket operations |
|
protected |
Starts time measurement for socket operation (read, write, connect, etc).
bool | $socket_open | Set to true, if startTimeMeasurement is made from open function |
|
abstractprotected |
Writes part of raw data to socket.
int | Number of bytes written |
const TIMEOUT_MODE_EVERY_OPERATION = 0 |
Timeout calculation mode: check for timeout for each network operation independently.
This mode is used to calculate timeout for each socket operation independently. If you set timeout value to 30 seconds, then each call to open, read, write, etc will have 30 seconds to complete.
const TIMEOUT_MODE_SUM_OF_OPERATIONS = 2 |
Timeout calculation mode: check for timeout for all network operations in total.
This mode is used to calculate total time taken by all calls to open, read, write, etc. This may be not very accurate.
const TIMEOUT_MODE_TOTAL = 1 |
Timeout calculation mode: check for timeout for all network operations in total from open() call.
This mode is used to calculate elapsed time from socket open operation till any other socket operation. For example, if timeout value is 30 seconds and socket was opened at 11:11:00, then you have 30 seconds to do all operations like reading and writing. If you put sleep(10) between read and write calls, this time will count, too. This timeout is flushed when socket is closed.