<?php<liu21st@gmail.com>namespace think\worker;
use think\Facade;
use Workerman\Worker as WorkerServer;
class Worker extends Server
{
protected $app;
protected $appPath;
public function __construct($host, $port, $context = [])
{
$this->worker = new WorkerServer('http://' . $host . ':' . $port, $context);
foreach ($this->event as $event) {
if (method_exists($this, $event)) {
$this->worker->$event = [$this, $event];
}
}
}
public function setAppPath($path)
{
$this->appPath = $path;
}
public function setStaticOption($name, $value)
{
WorkerServer::${$name} = $value;
}
public function option(array $option)
{
if (!empty($option)) {
foreach ($option as $key => $val) {
$this->worker->$key = $val;
}
}
}
public function onWorkerStart($worker)
{
$this->app = new Application($this->appPath);
Facade::bind([
'think\facade\Cookie' => Cookie::class,
]);
$this->app->initialize();
$this->app->bindTo([
'cookie' => Cookie::class,
]);
}
public function onMessage($connection, $data)
{
$this->app->worker($connection, $data);
}
public function start()
{
WorkerServer::runAll();
}
public function stop()
{
WorkerServer::stopAll();
}
}