fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace Webman\GatewayWorker;
class BusinessWorker extends \GatewayWorker\BusinessWorker
{
public function __construct($config)
{
foreach ($config as $key => $value)
{
$this->$key = $value;
}
}
public function onWorkerStart()
{
$this->_onWorkerStart = $this->onWorkerStart;
$this->_onWorkerReload = $this->onWorkerReload;
$this->_onWorkerStop = $this->onWorkerStop;
$this->onWorkerStop = array($this, 'onWorkerStop');
$this->onWorkerStart = array($this, 'onWorkerStart');
$this->onWorkerReload = array($this, 'onWorkerReload');
$args = func_get_args();
$this->id = $args[0]->id;
parent::onWorkerStart();
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Webman\GatewayWorker;
class Gateway extends \GatewayWorker\Gateway
{
public function __construct($config)
{
foreach ($config as $key => $value)
{
$this->$key = $value;
}
$this->router = array("\\GatewayWorker\\Gateway", 'routerBind');
}
public function onConnect($connection)
{
parent::onClientConnect($connection);
}
public function onClose($connection)
{
parent::onClientClose($connection);
}
public function onMessage($connection, $data)
{
parent::onClientMessage($connection, $data);
}
public function onWorkerStart()
{
// 保存用户的回调,当对应的事件发生时触发
$this->_onWorkerStart = $this->onWorkerStart;
$this->onWorkerStart = array($this, 'onWorkerStart');
// 保存用户的回调,当对应的事件发生时触发
$this->_onConnect = $this->onConnect;
$this->onConnect = array($this, 'onClientConnect');
// onMessage禁止用户设置回调
$this->onMessage = array($this, 'onClientMessage');
// 保存用户的回调,当对应的事件发生时触发
$this->_onClose = $this->onClose;
$this->onClose = array($this, 'onClientClose');
// 保存用户的回调,当对应的事件发生时触发
$this->_onWorkerStop = $this->onWorkerStop;
$this->onWorkerStop = array($this, 'onWorkerStop');
if (!is_array($this->registerAddress)) {
$this->registerAddress = array($this->registerAddress);
}
// 记录进程启动的时间
$this->_startTime = time();
$args = func_get_args();
$this->id = $args[0]->id;
$worker = func_get_arg(0);
$this->_gatewayPort = substr(strrchr($worker->getSocketName(),':'),1);
parent::onWorkerStart();
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Webman\GatewayWorker;
class Install
{
const WEBMAN_PLUGIN = true;
/**
* @var array
*/
protected static $pathRelation = array (
'plugin/webman/gateway' => 'plugin/webman/gateway',
'config/plugin/webman/gateway-worker' => 'config/plugin/webman/gateway-worker',
);
/**
* Install
* @return void
*/
public static function install()
{
static::installByRelation();
}
/**
* Uninstall
* @return void
*/
public static function uninstall()
{
self::uninstallByRelation();
}
/**
* installByRelation
* @return void
*/
public static function installByRelation()
{
foreach (static::$pathRelation as $source => $dest) {
if ($pos = strrpos($dest, '/')) {
$parent_dir = base_path().'/'.substr($dest, 0, $pos);
if (!is_dir($parent_dir)) {
mkdir($parent_dir, 0777, true);
}
}
//symlink(__DIR__ . "/$source", base_path()."/$dest");
copy_dir(__DIR__ . "/$source", base_path()."/$dest");
}
}
/**
* uninstallByRelation
* @return void
*/
public static function uninstallByRelation()
{
foreach (static::$pathRelation as $source => $dest) {
$path = base_path()."/$dest";
if (!is_dir($path) && !is_file($path)) {
continue;
}
/*if (is_link($path) {
unlink($path);
}*/
remove_dir($path);
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Webman\GatewayWorker;
class Register extends \GatewayWorker\Register
{
public function __construct()
{
}
public function onWorkerstart()
{
// 设置 onMessage 连接回调
$this->onConnect = array($this, 'onConnect');
// 设置 onMessage 回调
$this->onMessage = array($this, 'onMessage');
// 设置 onClose 回调
$this->onClose = array($this, 'onClose');
// 记录进程启动的时间
$this->_startTime = time();
// 强制使用text协议
$this->protocol = '\Workerman\Protocols\Text';
// reusePort
$this->reusePort = false;
}
}

View File

@@ -0,0 +1,4 @@
<?php
return [
'enable' => true,
];

View File

@@ -0,0 +1,37 @@
<?php
use Webman\GatewayWorker\Gateway;
use Webman\GatewayWorker\BusinessWorker;
use Webman\GatewayWorker\Register;
return [
'gateway' => [
'handler' => Gateway::class,
'listen' => 'websocket://0.0.0.0:7272',
'count' => cpu_count(),
'reloadable' => false,
'constructor' => ['config' => [
'lanIp' => '127.0.0.1',
'startPort' => 2300,
'pingInterval' => 25,
'pingData' => '{"type":"ping"}',
'registerAddress' => '127.0.0.1:1236',
'onConnect' => function(){},
]]
],
'worker' => [
'handler' => BusinessWorker::class,
'count' => cpu_count()*2,
'constructor' => ['config' => [
'eventHandler' => plugin\webman\gateway\Events::class,
'name' => 'ChatBusinessWorker',
'registerAddress' => '127.0.0.1:1236',
]]
],
'register' => [
'handler' => Register::class,
'listen' => 'text://0.0.0.0:1236',
'count' => 1, // Must be 1
'constructor' => []
],
];

View File

@@ -0,0 +1,34 @@
<?php
namespace plugin\webman\gateway;
use GatewayWorker\Lib\Gateway;
class Events
{
public static function onWorkerStart($worker)
{
}
public static function onConnect($client_id)
{
}
public static function onWebSocketConnect($client_id, $data)
{
}
public static function onMessage($client_id, $message)
{
Gateway::sendToClient($client_id, "receive message $message");
}
public static function onClose($client_id)
{
}
}