您好,登錄后才能下訂單哦!
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();//當(dāng)接收到客戶端消息時更新代碼,下次再執(zhí)行work代碼時變(onWorkerStart),本次執(zhí)行還是不變
$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);
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。