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);
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。