tp5+workman(GatewayWorker) 安裝及使用


一、安裝thinkphp5

1、寶塔刪除php禁用函數putenv、pcntl_signal_dispatch、pcntl_wai、pcntl_signal、pcntl_alarm、pcntl_fork,執行安裝命令。

composer create-project topthink/think=5.0.* tp5  --prefer-dist

2、配置好站點之后,瀏覽器打開訪問成功。

 

二、tp5安裝GatewayWorker

1、進入tp5目錄,安裝GatewayWorker

composer require workerman/gateway-worker

2、安裝workman

composer require workerman/workerman

 

3、安裝gatewayclient

 

composer require workerman/gatewayclient

 

 

 

三、使用GatewayWorker

1、創建文件tp5/public/start.php

<?php
/**
 * run with command
 * php start.php start
 */

ini_set('display_errors', 'on');
use Workerman\Worker;

if(strpos(strtolower(PHP_OS), 'win') === 0)
{
    exit("start.php not support windows, please use start_for_win.bat\n");
}

// 檢查擴展
if(!extension_loaded('pcntl'))
{
    exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}

if(!extension_loaded('posix'))
{
    exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}

// 標記是全局啟動
define('GLOBAL_START', 1);

require_once __DIR__ . '/../vendor/autoload.php';
// 加載所有Applications/*/start.php,以便啟動所有服務
foreach(glob(__DIR__.'/../application/Socket/start*.php') as $start_file)
{
    require_once $start_file;
}
// 運行所有服務
Worker::runAll();

2、創建文件tp5/application/Socket/Events.php

<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用於檢測業務代碼死循環或者長時間阻塞等問題
 * 如果發現業務卡死,可以將下面declare打開(去掉//注釋),並執行php start.php reload
 * 然后觀察一段時間workerman.log看是否有process_timeout異常
 */
//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;

/**
 * 主邏輯
 * 主要是處理 onConnect onMessage onClose 三個方法
 * onConnect 和 onClose 如果不需要可以不用實現並刪除
 */
class Events
{
    /**
     * 當客戶端連接時觸發
     * 如果業務不需此回調可以刪除onConnect
     * 
     * @param int $client_id 連接id
     */
    public static function onConnect($client_id)
    {
        echo "【新的客戶端鏈接】:client_id:".$client_id.PHP_EOL;
        // 向當前client_id發送數據 
        Gateway::sendToClient($client_id, "Hello $client_id\r\n");
        // 向所有人發送
        Gateway::sendToAll("$client_id login\r\n");
    }
    
   /**
    * 當客戶端發來消息時觸發
    * @param int $client_id 連接id
    * @param mixed $message 具體消息
    */
   public static function onMessage($client_id, $message)
   {
        // 向所有人發送 
        Gateway::sendToAll("$client_id said $message\r\n");
   }
   
   /**
    * 當用戶斷開連接時觸發
    * @param int $client_id 連接id
    */
   public static function onClose($client_id)
   {
       // 向所有人發送 
       GateWay::sendToAll("$client_id logout\r\n");
   }
}

3、創建文件tp5/application/Socket/start_businessworker.php

<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use Workerman\Worker;
use Workerman\WebServer;
use GatewayWorker\Gateway;
use GatewayWorker\BusinessWorker;
use Workerman\Autoloader;

// 自動加載類
require_once __DIR__ . '/../../vendor/autoload.php';

// bussinessWorker 進程
$worker = new BusinessWorker();
// worker名稱
$worker->name = 'YourAppBusinessWorker';
// bussinessWorker進程數量
$worker->count = 4;
// 服務注冊地址
$worker->registerAddress = '127.0.0.1:1236';

// 如果不是在根目錄啟動,則運行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

4、創建文件tp5/application/Socket/start_gateway.php

 

<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;

// 自動加載類
require_once __DIR__ . '/../../vendor/autoload.php';

// gateway 進程,這里使用Text協議,可以用telnet測試
$gateway = new Gateway("websocket://0.0.0.0:8282");
// gateway名稱,status方便查看
$gateway->name = 'YourAppGateway';
// gateway進程數
$gateway->count = 4;
// 本機ip,分布式部署時使用內網ip
$gateway->lanIp = '127.0.0.1';
// 內部通訊起始端口,假如$gateway->count=4,起始端口為4000
// 則一般會使用4000 4001 4002 4003 4個端口作為內部通訊端口 
$gateway->startPort = 2900;
// 服務注冊地址
$gateway->registerAddress = '127.0.0.1:1236';

// 心跳間隔
//$gateway->pingInterval = 10;
// 心跳數據
//$gateway->pingData = '{"type":"ping"}';

/* 
// 當客戶端連接上來時,設置連接的onWebSocketConnect,即在websocket握手時的回調
$gateway->onConnect = function($connection)
{
    $connection->onWebSocketConnect = function($connection , $http_header)
    {
        // 可以在這里判斷連接來源是否合法,不合法就關掉連接
        // $_SERVER['HTTP_ORIGIN']標識來自哪個站點的頁面發起的websocket鏈接
        if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
        {
            $connection->close();
        }
        // onWebSocketConnect 里面$_GET $_SERVER是可用的
        // var_dump($_GET, $_SERVER);
    };
}; 
*/

// 如果不是在根目錄啟動,則運行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

 

5、創建文件tp5/application/Socket/start_register.php

 

<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \GatewayWorker\Register;

// 自動加載類
require_once __DIR__ . '/../../vendor/autoload.php';

// register 必須是text協議
$register = new Register('text://0.0.0.0:1236');

// 如果不是在根目錄啟動,則運行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

 

6、啟動websocket程序

1、防火牆打開8282、1236端口,執行下面命令

php start.php start

四、使用GatewayWorker發布廣播

1、創建文件tp5/application/index/controller/Index.php,執行這個方法就可以向所有人發布廣播了。

<?php
namespace app\index\controller;
use GatewayClient\Gateway;
class Index
{
    public function index()
    {
        Gateway::sendToAll(" index發的消息 \r\n");
    }
}

 


免責聲明!

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



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