tp5.1使用think-swoole


1.下載tp框架,版本5.1.35 ,下載think-swoole,版本2.0.17

2.在config下書寫swoole.php

<?php
use think\facade\Env;

// +----------------------------------------------------------------------
// | Swoole設置 php think swoole命令行下有效
// +----------------------------------------------------------------------
return [
    // 擴展自身配置
    'host'                  => '0.0.0.0', // 監聽地址
    'port'                  => 9501, // 監聽端口
    'worker_num'   =>  4,
    'max_request'   =>  1000,
    'app_path'              => '/web/twzb/application/',
    'cache_size'=>1024*8,
   /* 'mode'                  => '', // 運行模式 默認為SWOOLE_PROCESS
    'sock_type'             => '', // sock type 默認為SWOOLE_SOCK_TCP
    'server_type'           => 'http', // 服務類型 支持 http websocket
    'app_path'              => '', // 應用地址 如果開啟了 'daemonize'=>true 必須設置(使用絕對路徑)
    'file_monitor'          => false, // 是否開啟PHP文件更改監控(調試模式下自動開啟)
    'file_monitor_interval' => 2, // 文件變化監控檢測時間間隔(秒)
    'file_monitor_path'     => [], // 文件監控目錄 默認監控application和config目錄

    // 可以支持swoole的所有配置參數
    'pid_file'              => Env::get('runtime_path') . 'swoole.pid',
    'log_file'              => Env::get('runtime_path') . 'swoole.log',
    'document_root'         => Env::get('root_path') . 'public',
    'enable_static_handler' => true,
    'timer'                 => true,//是否開啟系統定時器
    'interval'              => 500,//系統定時器 時間間隔
    'task_worker_num'       => 1,//swoole 任務工作進程數量*/
];

2.在config下書寫swoole_server.php

<?php
use think\facade\Env;

// +----------------------------------------------------------------------
// | Swoole設置 php think swoole:server 命令行下有效
// +----------------------------------------------------------------------
return [
  
     'swoole_class' => 'app\http\Swoole', // 自定義服務類名稱
];

3.書寫Swoole.php

<?php
namespace app\http;

use think\swoole\Server;

class Swoole extends Server
{
protected $host = '0.0.0.0';
protected $port = 9502;
protected $serverType = 'socket';
protected $mode = SWOOLE_PROCESS;
protected $sockType = SWOOLE_SOCK_TCP;
protected $option = [
'worker_num'=> 4,
'daemonize' => true,
'backlog' => 128
];


public function onReceive($server, $fd, $from_id, $data)
{
$server->send($fd, 'Swoole: '.$data);
}

public function onRequest($request, $response)
{
// $server->send('Swoole: ');
}


/**
* 監聽ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}\n";
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}

}

使用命令 

php think swoole

 

 

使用命令 

php think swoole:server

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM