是否有任何Symfony听众做套接字工作?

时间:2016-02-18 09:48:34

标签: php function sockets symfony loops

如何在Symfony中创建一个侦听器来执行以下示例中的套接字操作:

public function socketestAction($sizeData)    //For TEST
    {
        session_write_close();
        ob_implicit_flush(true);
        ob_end_flush();

        $offset = 0;
        $limit = 3;
        $sizeData /= $limit;

        // Init IPC connection
        $server = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errorMessage);
        if ($server === false) {
            throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
        }

        for( $i = 0; $i < $sizeData; $i++)
        {
            // Check our socket for data
            $client = @stream_socket_accept($server);
            if ($client) {
                // Read sent data
                $data = fread($client, 1024);

                // Probably break
                if ($data === 'stop') {
                    break;
                }
            }

            $offset += $limit;
            var_dump($offset);
        }
        // Close socket after sending all messages
        fclose($client);
    }

StopAction:

public function stopAction()
{
    $socket = stream_socket_client('tcp://127.0.0.1:8000');
    fwrite($socket, 'stop');
    fclose($socket);

}

但问题是,每个插座都必须更换端口,&amp;不能使用很多套接字..而且它需要花费很多时间来执行! 另一个问题:是否有可能创建一个简单的txt文件?

1 个答案:

答案 0 :(得分:0)

您可以使用EventEmitter扩展,从github下载 https://github.com/christopherobin/EventEmitter