// 加載所有Applications/*/start.php,以便啟動(dòng)所有服務(wù)
foreach (glob(__DIR__ .'/Application/*/start*.php') as $start_file)
{
require_once $start_file;
}
加載出所有啟動(dòng)服務(wù)
/**
* 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://wtbis.cn/
* @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';
// WebServer
$web = new WebServer("http://0.0.0.0:55151");
// WebServer進(jìn)程數(shù)量
$web->count = 2;
// 設(shè)置站點(diǎn)根目錄
$web->addRoot('www.your_domain.com', __DIR__.'/Web');
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}use \Workerman\Worker;
use \GatewayWorker\Gateway;
use \Workerman\Autoloader;
require_once __DIR__ . '/../../vendor/autoload.php';
// gateway 進(jìn)程
$gateway = new Gateway("Websocket://0.0.0.0:7272");
// 設(shè)置名稱,方便status時(shí)查看
$gateway->name = 'ChatGateway';
// 設(shè)置進(jìn)程數(shù),gateway進(jìn)程數(shù)建議與cpu核數(shù)相同
$gateway->count = 4;
// 分布式部署時(shí)請(qǐng)?jiān)O(shè)置成內(nèi)網(wǎng)ip(非127.0.0.1)
$gateway->lanIp = '127.0.0.1';
// 內(nèi)部通訊起始端口。假如$gateway->count=4,起始端口為2300
// 則一般會(huì)使用2300 2301 2302 2303 4個(gè)端口作為內(nèi)部通訊端口
$gateway->startPort = 2300;
// 心跳間隔
$gateway->pingInterval = 10;
// 心跳數(shù)據(jù)
$gateway->pingData = '{"type":"ping"}';
// 服務(wù)注冊(cè)地址
$gateway->registerAddress = '127.0.0.1:1236';
啟動(dòng)時(shí)只開啟了 7272端口 端口沒有被占用
start.php 里要定義?define('GLOBAL_START', true);
?
然后每個(gè)start_xx.php里末尾都要有
?
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
? ? Worker::runAll();
}
那你把 start.php 以及所有的 start_xx.php 的完整代碼貼出來大家看看。
start.php
<?php
//錯(cuò)誤提示
ini_set('display_errors', 'on');
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
//驗(yàn)證運(yùn)行操作系統(tǒng)
if(strpos(strtolower(PHP_OS), 'win') === 0)
{
exit("start.php不能在windows系統(tǒng)下直接運(yùn)行,請(qǐng)先運(yùn)行start_for_win_bat");
};
// 加載所有Applications//start.php,以便啟動(dòng)所有服務(wù)
foreach (glob(__DIR__ .'/Application//start*.php') as $start_file)
{
require_once $start_file;
}
// 標(biāo)記是全局啟動(dòng)
define('GLOBAL_START', 1);
//啟動(dòng)所有服務(wù)
Worker::runAll();
start_gateway.php
<?php
/**
require_once __DIR__ . '/../../vendor/autoload.php';
// gateway 進(jìn)程
$gateway = new Gateway("Websocket://0.0.0.0:7272");
// 設(shè)置名稱,方便status時(shí)查看
$gateway->name = 'ChatGateway';
// 設(shè)置進(jìn)程數(shù),gateway進(jìn)程數(shù)建議與cpu核數(shù)相同
$gateway->count = 4;
// 分布式部署時(shí)請(qǐng)?jiān)O(shè)置成內(nèi)網(wǎng)ip(非127.0.0.1)
$gateway->lanIp = '127.0.0.1';
// 內(nèi)部通訊起始端口。假如$gateway->count=4,起始端口為2300
// 則一般會(huì)使用2300 2301 2302 2303 4個(gè)端口作為內(nèi)部通訊端口
$gateway->startPort = 2300;
// 心跳間隔
$gateway->pingInterval = 10;
// 心跳數(shù)據(jù)
$gateway->pingData = '{"type":"ping"}';
// 服務(wù)注冊(cè)地址
$gateway->registerAddress = '127.0.0.1:1236';
/
// 當(dāng)客戶端連接上來時(shí),設(shè)置連接的onWebSocketConnect,即在websocket握手時(shí)的回調(diào)
$gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在這里判斷連接來源是否合法,不合法就關(guān)掉連接
// $_SERVER['HTTP_ORIGIN']標(biāo)識(shí)來自哪個(gè)站點(diǎn)的頁面發(fā)起的websocket鏈接
if($_SERVER['HTTP_ORIGIN'] != '
{
$connection->close();
}
// onWebSocketConnect 里面$_GET $_SERVER是可用的
// var_dump($_GET, $_SERVER);
};
};
/
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
start_web.php
<?php
/**
require_once __DIR__ . '/../../vendor/autoload.php';
// WebServer
$web = new WebServer("http://0.0.0.0:55151");
// WebServer進(jìn)程數(shù)量
$web->count = 2;
// 設(shè)置站點(diǎn)根目錄
$web->addRoot('www.your_domain.com', __DIR__.'/Web');
// 如果不是在根目錄啟動(dòng),則運(yùn)行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
?define('GLOBAL_START', 1);這行代碼位置錯(cuò)了,不能隨便?亂放,至少必須放在 foreach 語句前面,其實(shí)是針對(duì) require_once 設(shè)定的。