如果一个套接字仍然连接,如果我没有套接字处理程序,如何检入PHP?

ahm*_*med 8 php sockets linux twitter

我正在使用一个作曲家包owlycode/streaming-bird来调用twitter流API.流API在您的应用和推特之间打开一个套接字,以接收具有指定关键字的推文.在我的例子中,关键字是'hello'.

这是使用owlycode/streaming-bird包的代码:

 <?PHP
    $oauthToken = '';
    $oauthSecret = '';
    $consumerKey = '';
    $consumerSecret = '';

    $bird = new StreamingBird($consumerKey, $consumerSecret, $oauthToken, $oauthSecret);

    $bird
        ->createStreamReader(StreamReader::METHOD_FILTER)
        ->setTrack(['hello']) // Fetch every tweet containing one of the following words
        ->consume(function ($tweet) { // Now we provide a callback to execute on every received tweet.
            echo '------------------------' . "\n";
            echo $tweet['text'] . "\n";
        });
  ?>
Run Code Online (Sandbox Code Playgroud)

我的问题是当这个连接被错误关闭时,我无法知道.所以我无法再次与twitter重新联系.

PHP中是否存在基于其域名搜索打开套接字的内容?

也许是这样的

  check_if_socket_open('https://stream.twitter.com/1.1/statuses/firehose.json')
Run Code Online (Sandbox Code Playgroud)

注意:我不能使用socket_get_status,因为我没有socket变量.

小智 2

看起来,如果您只是在包本身中进行一些小的添加,那么您毕竟可以使用 socket_get_status 。

这两个函数位于streamreader类中,套接字处理程序在这里可用。

public function consume(callable $handler)
{
    $this->running = true;
    while ($this->running) { /// while $this->running is true socket will try to reconnect always. 
        $this->consumeOnce($handler);
    }
}
    protected function consumeOnce(callable $handler)
    {
        $this->connection = $this->connect();
        $lastStreamActivity = time();
        $this->connection->read(function ($tweet) use (&$lastStreamActivity, $handler) {
            $idle = (time() - $lastStreamActivity);
            $this->monitor->stat('max_idle_time', $idle);
            $this->monitor->stat('idle_time', $idle);
            $this->monitor->stat('tweets', 1);
            $lastStreamActivity = time();
            call_user_func($handler, $tweet, $this->monitor);
        });
        $this->connection->close();
    }
Run Code Online (Sandbox Code Playgroud)

在连接类中,您可以使用套接字处理程序,因此您可以在尝试从套接字读取数据时获取套接字状态。下面是一个稍微修改过的读取函数

public function read(callable $callback, $timeout = 5)
        {
            $this->pool = [$this->connection];
    stream_set_timeout($this->connection, $timeout);
    $info = stream_get_meta_data($this->connection);
            while ($this->connection !== null && !feof($this->connection) && stream_select($this->pool, $fdw, $fde, $timeout) !== false && $info['timed_out']!==true) {
                // @todo safeguard no tweets but connection OK. (reconnect)
                $this->pool = [$this->connection];
                $chunkInfo = trim(fgets($this->connection));
                if (!$chunkInfo) {
                    continue;
                }
                $len = hexdec($chunkInfo) + 2;
                $streamInput = '';
                while (!feof($this->connection)) {
                    $streamInput .= fread($this->connection, $len-strlen($streamInput));
                    if (strlen($streamInput)>=$len) {
                        break;
                    }
                }
                $this->buffer .= substr($streamInput, 0, -2);
                $data = json_decode($this->buffer, true);
                if ($data) {
                    call_user_func($callback, $data);
                    $this->buffer = '';
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)