send
Description:
mixed Connection::send(mixed $data [,$raw = false])
Sends data to the client
Parameters
$data
The data to be sent. If a protocol was specified when initializing the Worker class, the protocol's encode method will be automatically called, and the data will be sent to the client after being packaged.
$raw
Whether to send raw data, i.e., not calling the protocol's encode method. The default is false, which means the protocol's encode method will be called automatically.
Return Value
true means that the data has been successfully written to the socket send buffer at the operating system level for this connection.
null means that the data has been written to the application layer send buffer for this connection, waiting to be written to the system-level socket send buffer.
false means that the send operation failed. The failure reasons may include that the client connection has been closed or that the application layer send buffer for this connection is full.
Note
The return value true from send indicates that the data has been successfully written to the operating system's socket send buffer for this connection, but it does not mean that the data has been successfully sent to the counterpart's socket receive buffer, nor does it indicate that the counterpart's application has read the data from the local socket receive buffer. However, as long as send does not return false and the network is not disconnected, and the client is receiving normally, the data can essentially be considered to be 100% delivered to the other party.
Since the data in the socket send buffer is sent asynchronously to the other party by the operating system, and the operating system does not provide an acknowledgment mechanism for the application layer, the application layer cannot know when the data in the socket send buffer starts being sent, nor can the application layer know whether the data in the socket send buffer was sent successfully. For these reasons, Workerman cannot directly provide a message acknowledgment interface.
If the business requires ensuring that every message is received by the client, an acknowledgment mechanism can be added at the business level. The acknowledgment mechanism may vary depending on the business, and even the same business's acknowledgment mechanism can have multiple methods.
For example, a chat system could use an acknowledgment mechanism like this: Store each message in a database, with each message having a "read" field. Each time the client receives a message, it sends an acknowledgment packet to the server, which marks the corresponding message as read. When the client connects to the server (usually when the user logs in or reconnects after a disconnection), it queries the database for any unread messages, and if any exist, the server sends them to the client. Similarly, the client notifies the server when it has read the messages. This way, it can ensure that each message is received by the other party. Of course, developers can also use their own acknowledgment logic.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, $data)
{
// Automatically calls \Workerman\Protocols\Websocket::encode to package as websocket protocol data before sending
$connection->send("hello\n");
};
// Run the worker
Worker::runAll();