OpenSwoole\WebSocket\Server->isEstablished(...)

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\WebSocket\Server->isEstablished(int $fd): bool

Parameters

fd

The fd of the WebSocket connection, it can found from the server event callback parameter $fd or from the Swoole data frame object. The fd must be a valid WebSocket connection fd.

Return

If success, it returns true, otherwise it returns false

Description

Check if the WebSocket connection is established, has completed the handshake stage and that the WebSocket connection is valid.

This method is different from exists() because that is used only to check TCP connections, not WebSocket connections. You can use isEstablished() to check for valid WebSocket TCP connections.

Example

Inside a normal server.php file, we can start up a WebSocket server which responds to HTTP requests and broadcasts back to all clients who are connected. In this example we are using the function isEstablished to check if the WebSocket client is still connected and has completed the handshake stage, allowing us to send a message back to the clients.

<?php


$server = new OpenSwoole\WebSocket\Server("127.0.0.1", 9501);

$server->on('Open', function(OpenSwoole\WebSocket\Server $server, $request)
{
    echo "server and client handshake success with fd{$request->fd}\n";
});

$server->on('Message', function(OpenSwoole\WebSocket\Server $server, $frame)
{
    echo "Received message from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";

    $server->push($frame->fd, "This is a message from the server");
});

$server->on('Close', function(OpenSwoole\WebSocket\Server $server, $fd)
{
    echo "Client connection {$fd} closed\n";
});

// The Request event closure callback is passed the context of $server
$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server)
{
    /*
     * Loop through all the WebSocket connections to
     * send back a response to all clients. Broadcast
     * a message back to every WebSocket client.
     */
    foreach($server->connections as $fd)
    {
        // Validate a correct WebSocket connection otherwise a push may fail
        if($server->isEstablished($fd))
        {
            $server->push($fd, $request->get['message']);
        }
    }
});

$server->start();

Accessing file descriptor through event callback parameters

<?php

// Server setup...

$server->on('Receive', function($server, $fd, $threadId, $data)
{
    // Other logic which may take some time...

    // Checking if the WebSocket connection is valid...
    if($server->isEstablished($fd) === false)
    {
      echo "WebSocket connection from $fd is NOT valid...\n";

      // 1015 = Bad TLS handshake, disconnect the client
      OpenSwoole\WebSocket\Server->disconnect($fd, 1015, 'Bad TLS handshake');
    }

    echo "Connection from client ($fd) is valid, pushing data back...\n";
    $server->push($fd, 'Your connection is valid!');
});

// ...
Last updated on September 1, 2022