Ejemplo n.º 1
0
 function onTimer($serv, $interval)
 {
     //        $this->log("timer trigger! \n");
     //每秒检测,如有更新则推送。
     if (count($this->connections) <= 0) {
         return;
     }
     $file_path_prefix = __DIR__ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR;
     //todo 可改为循环遍历目录;就无需手动配置,加异常文件的容错处理
     $file_arr = array("btcchina.json", "huobi.json", "okcoin.json", "chbtc.json", "bitstamp.json", "btce.json");
     foreach ($file_arr as $key => $value) {
         swoole_async_readfile($file_path_prefix . $value, function ($filename, $content) {
             $tmp = json_decode($content, true);
             if (!isset($tmp["time"])) {
                 return;
             }
             if (!isset($this->timestamp[$tmp['key']])) {
                 $this->timestamp[$tmp['key']] = 0;
             }
             //todo 有些自己生成的time的就没办法比对了;
             if ($tmp["time"] == $this->timestamp[$tmp['key']]) {
                 return;
             }
             $this->timestamp[$tmp['key']] = $tmp['time'];
             //推送到每个fd
             foreach ($this->connections as $clid => $info) {
                 $this->send($clid, $content);
             }
         });
     }
 }
Ejemplo n.º 2
0
 public function onReceive(swoole_server $serv, $fd, $from_id, $data)
 {
     echo $serv->worker_pid . PHP_EOL;
     $case = intval($data);
     switch ($case) {
         case 1:
             swoole_async_readfile(__DIR__ . "/Test.txt", function ($filename, $content) {
                 echo "{$filename}: {$content}";
             });
             break;
         case 2:
             swoole_async_writefile('test_2.log', "This is a test log", function ($filename) {
                 echo "wirte ok.\n";
             });
             break;
         case 3:
             swoole_async_read(__DIR__ . "/Test.txt", function ($filename, $content) {
                 if (empty($content)) {
                     return false;
                 } else {
                     echo "{$filename}: {$content}";
                     return true;
                 }
             }, 16);
             break;
         default:
             // 注:此处存在一个Bug,如果文件不存在并且没有指定offset(或者指定为-1),会引发一个错误。这里需要注意一下。
             swoole_async_write('test_1.log', "This is a test log\n", -1, function ($filename, $writen) {
                 var_dump(func_get_args());
             });
             break;
     }
 }
Ejemplo n.º 3
0
<?php

swoole_async_readfile(__DIR__ . '/../server.php', function ($filename, $content) {
    echo "file: {$filename}\ncontent-length: " . strlen($content) . "\nContent:\n";
    swoole_async_writefile(__DIR__ . '/test.copy', $content, function ($write_file) {
        echo "file: {$write_file}\n";
        swoole_event_exit();
    });
});