兩次嘗試,如下。
嘗試一,在config/bootstrap.php
配置文件添加一個Bootstrap
,然后在start
方法里面直接new channel、GlobalData服務(wù)。
代碼如下:
use Webman\Bootstrap as WebmanBootstrap;
class Bootstrap implements WebmanBootstrap
{
public static function start($worker)
{
$channel = new \Channel\Server();
$global = new \GlobalData\Server();
}
}
結(jié)果:
嘗試二,配置process.php:
'websocket' => [
'handler' => process\GlobalChannelServer::class,
'count' => 1,
],
在GlobalChannelServer的onWorkerStart
方法中new channel、GlobalData服務(wù)。
代碼如下:
<?php
namespace process;
use Channel\Server;
class GlobalChannelServer
{
public function onWorkerStart()
{
$channel = new Server();
$global = new \GlobalData\Server();
}
}
結(jié)果:
嘗試了以上兩種方法,問題都是服務(wù)不能起來。在根目錄創(chuàng)建server文件卻可以跑起來。
代碼如下:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Channel\Server as ChannelServer;
use GlobalData\Server as GlobalDataServer;
$channelServer = new ChannelServer();
$globalDataServer = new GlobalDataServer();
\Workerman\Worker::runAll();
雖然最后解決了問題,但是不理解為什么前面兩種不能成功創(chuàng)建服務(wù)。在新文件中也是Worker::runAll,啟動,在webman中最后也是Worker::runAll啟動,為什么不行呢?如果想集成到webman中,有什么辦法嗎?