TP5.1使用 GatewayWorker 進行 socket 通訊
阿新 • • 發佈:2018-11-14
1.安裝 Workerman
composer 安裝GatewayWorker核心檔案(不包含start_gateway.php start_businessworker.php等啟動入口檔案)
composer require workerman/gateway-worker
2.建立 Workerman 啟動檔案
建立一個自定義命令類檔案來啟動 Socket 服務端,新建
application/common/command/Workerman.php
<?php
namespace app\common\command;
use app\workerman\Events;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Worker;
class Workerman extends Command
{
protected function configure()
{
$this->setName('workerman')
->addArgument('action', Argument::OPTIONAL, "action start|stop|restart")
->addArgument('type', Argument::OPTIONAL, "d -d")
->setDescription('workerman chat');
}
protected function execute(Input $input, Output $output)
{
global $argv;
$action = trim($input->getArgument('action'));
$type = trim($input->getArgument('type')) ? '-d' : '';
$argv[0] = 'chat';
$argv[1] = $action;
$argv[2] = $type ? '-d' : '';
$this->start();
}
private function start()
{
$this->startGateWay();
$this->startBusinessWorker();
$this->startRegister();
Worker::runAll();
}
private function startBusinessWorker()
{
$worker = new BusinessWorker();
$worker->name = 'BusinessWorker';
$worker->count = 1;
$worker->registerAddress = '127.0.0.1:1236';
$worker->eventHandler = Events::class;
}
private function startGateWay()
{
$gateway = new Gateway("websocket://0.0.0.0:8282");
$gateway->name = 'Gateway';
$gateway->count = 1;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2300;
$gateway->pingInterval = 30;
$gateway->pingNotResponseLimit = 0;
$gateway->pingData = '{"type":"@ [email protected]"}';
$gateway->registerAddress = '127.0.0.1:1236';
}
private function startRegister()
{
new Register('text://0.0.0.0:1236');
}
}
配置 application/command.php 檔案
return [
'app\common\command\Workerman',
];
3.建立事件監聽檔案
建立 application/workerman/Events.php 檔案來監聽處理 workerman 的各種事件。
<?php namespace app\workerman; use GatewayWorker\Lib\Gateway; class Events { public static function onWorkerStart($businessWorker){ } public static function onConnect($client_id){ } public static function onWebSocketConnect($client_id, $data){ } public static function onMessage($client_id, $message){ } public static function onClose($client_id){ } }
4.啟動 Workerman 服務端
以debug(除錯)方式啟動
php think workerman start
php think workerman start d // 以daemon(守護程序)方式啟動
php think workerman stop
php think workerman restart
php think workerman reload
php think workerman status
當你看到如下結果的時候,workerman已經啟動成功了。
Workerman[chat] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.11 PHP version:7.0.29
------------------------ WORKERS -------------------------------
user worker listen processes status
tegic Gateway websocket://0.0.0.0:8282 1 [OK]
tegic BusinessWorker none 1 [OK]
tegic Register text://0.0.0.0:1236 1 [OK]
----------------------------------------------------------------
Press Ctrl+C to stop. Start success.