close

说明:

  1. void Connection::close(mixed $data = '')

安全的关闭连接.

调用close会等待发送缓冲区的数据发送完毕后才关闭连接,并触发连接的onClose回调。

参数

$data

可选参数,要发送的数据(如果有指定协议,则会自动调用协议的encode方法打包$data数据),当数据发送完毕后关闭连接,随后会触发onClose回调

范例

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. $worker = new Worker('websocket://0.0.0.0:8484');
  5. $worker->onMessage = function(TcpConnection $connection, $data)
  6. {
  7. $connection->close("hello\n");
  8. };
  9. // 运行worker
  10. Worker::runAll();