swoole reload 代码热更新 example
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swoole reload 代码热更新 example相关的知识,希望对你有一定的参考价值。
server:
class Server
{
private $_serv = null;
private $_e = null;
public function __construct()
{
$this->_serv = new swoole_server("0.0.0.0", 9501);
$this->_serv->set(array(
‘worker_num‘ => 8,
‘daemonize‘ => false,
‘max_request‘ => 10000,
‘dispatch_mode‘ => 2,
‘debug_mode‘=> 1,
));
$this->_serv->on(‘Start‘, array($this, ‘onStart‘));
$this->_serv->on(‘WorkerStart‘, array($this, ‘onWorkerStart‘));
$this->_serv->on(‘Connect‘, array($this, ‘onConnect‘));
$this->_serv->on(‘Receive‘, array($this, ‘onReceive‘));
$this->_serv->on(‘Close‘, array($this, ‘onClose‘));
$this->_serv->start();
}
public function onStart($serv)
{
echo "start\n";
cli_set_process_title(‘reload_master‘);
}
public function onWorkerStart($serv, $worker_id)
{
spl_autoload_register(function($class){
$classPath = str_replace(‘\\‘, ‘/‘, $class) .‘.php‘;
if (file_exists($classPath)) {
include $classPath;
}
});
$this->_e = new \plugin\Event();
}
public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
$serv->reload();//当接收到客户端消息时更新代码,下次再执行work代码时变(onWorkerStart),本次执行还是不变
$this->_e->say();
echo "Get Message From Client {$fd}:{$data}\n";
}
public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}
new Server();
client:
$cli = new Swoole_client(SWOOLE_SOCK_TCP);
$cli->connect(‘127.0.0.1‘, 9501, 1);
fwrite(STDOUT, ‘输入消息:‘);
$msg = trim(fgets(STDIN));
$cli->send($msg);
本文出自 “我是程序我最大” 博客,请务必保留此出处http://skinglzw.blog.51cto.com/10729606/1964105
以上是关于swoole reload 代码热更新 example的主要内容,如果未能解决你的问题,请参考以下文章
swoole mixphp swoolefor热更新使用方法
swoole mixphp swoolefor热更新使用方法
swoole soa 每次修改server 端的方法时都需要重启server吗
Flutter“不能热加载(hot reload),热重载按钮灰色且无法点击”的解决方案