function run($setting = array()) { $set = array_merge($this->swooleSetting, $setting); $this->sw->set($set); $version = explode('.', SWOOLE_VERSION); //1.7.0 if ($version[1] >= 7) { $this->sw->on('ManagerStart', function ($serv) { global $argv; Swoole\Console::setProcessName('php ' . $argv[0] . ': manager'); }); } $this->sw->on('Start', array($this, 'onMasterStart')); $this->sw->on('WorkerStart', array($this->protocol, 'onStart')); $this->sw->on('Connect', array($this->protocol, 'onConnect')); $this->sw->on('Receive', array($this->protocol, 'onReceive')); $this->sw->on('Close', array($this->protocol, 'onClose')); $this->sw->on('WorkerStop', array($this->protocol, 'onShutdown')); if (is_callable(array($this->protocol, 'onTimer'))) { $this->sw->on('Timer', array($this->protocol, 'onTimer')); } if (is_callable(array($this->protocol, 'onTask'))) { $this->sw->on('Task', array($this->protocol, 'onTask')); $this->sw->on('Finish', array($this->protocol, 'onFinish')); } $this->sw->start(); }
/** * 启动服务器 * @return void */ public function start() { if ($this->isStart) { return; } $this->isStart = true; $this->server->start(); }
function run($host, $port) { register_shutdown_function(array($this, 'errorHandler')); $this->serv = new swoole_server($host, $port); file_put_contents(PID_FILE_NAME, posix_getpid()); $this->serv->set(array('max_request' => 0, 'open_length_check' => true, 'package_max_length' => 81920, 'package_length_type' => 'n', 'package_length_offset' => 0, 'package_body_offset' => 2, 'worker_num' => 2)); $this->serv->on('receive', array($this, 'onReceive')); $this->serv->on('close', array($this, 'onClose')); $this->serv->start(); }
function run($_setting = array()) { $default_setting = array('worker_num' => 4, 'open_eof_check' => true, 'open_eof_split' => true, 'package_eof' => self::EOF); $this->pid_file = $_setting['pid_file']; $setting = array_merge($default_setting, $_setting); $serv = new \swoole_server('0.0.0.0', self::SVR_PORT_AOP, SWOOLE_PROCESS, SWOOLE_TCP); $serv->set($setting); $serv->on('receive', array($this, 'onReceive')); $this->serv = $serv; $this->serv->start(); }
public function Bootstrap() { $initParams = ['serv_host' => '0.0.0.0', 'serv_port' => '13123', 'serv_mode' => SWOOLE_PROCESS, 'sock_type' => SWOOLE_SOCK_TCP]; Application::RegisterClass(\swoole_server::class, $initParams); $this->swoole = Application::GetInstance(\swoole_server::class); $this->setConfig(); $this->initProxy(); //构造时已经自动完成了回调注册 $this->initMonitor(); echo $this->formatSettings($this->swoole); $this->swoole->start(); }
public function serve() { $support_callback = ['start' => [$this, 'onStart'], 'managerStart' => [$this, 'onManagerStart'], 'workerStart' => [$this, 'onWorkerStart'], 'receive' => [$this, 'onReceive'], 'task' => null, 'finish' => null, 'workerStop' => [$this, 'onWorkerStop']]; foreach ($support_callback as $name => $callback) { // If has the dependency injection if (is_callable(Di::get($name))) { $callback = Di::get($name); } if ($callback !== null) { $this->serv->on($name, $callback); } } $this->serv->set($this->swoole_config); $this->serv->start(); }
public function actionRun() { $serv = new swoole_server("127.0.0.1", 9550); self::$q_config = (require 'config.php'); $task_num = 0; foreach (self::$q_config as $key => $val) { self::$event_base[$key] = $task_num; self::$cnt[$key] = 0; $task_num += $val; } $this->config['task_worker_num'] = $task_num; $serv->set($this->config); $serv->on('Start', array($this, 'my_onStart')); $serv->on('Connect', array($this, 'my_onConnect')); $serv->on('Receive', array($this, 'my_onReceive')); $serv->on('Close', array($this, 'my_onClose')); $serv->on('Shutdown', array($this, 'my_onShutdown')); $serv->on('Timer', array($this, 'my_onTimer')); $serv->on('WorkerStart', array($this, 'my_onWorkerStart')); $serv->on('WorkerStop', array($this, 'my_onWorkerStop')); $serv->on('Task', array($this, 'my_onTask')); $serv->on('Finish', array($this, 'my_onFinish')); $serv->on('WorkerError', array($this, 'my_onWorkerError')); $serv->start(); }
public function run() { $config = Config::get('socket'); $config = array('worker_num' => 4, 'task_worker_num' => 8, 'max_request' => 10000, 'dispatch_mode' => 2, 'debug_mode' => 0, 'daemonize' => false); if (isset($argv[1]) and $argv[1] == 'daemon') { $config['daemonize'] = true; } else { $config['daemonize'] = false; } $serv = new \swoole_server("0.0.0.0", 8808); $serv->set($config); $serv->config = $config; $handler = new Handler(); $serv->on('Start', array($handler, "start")); $serv->on('Connect', array($handler, "connect")); $serv->on('Receive', array($handler, "receive")); $serv->on('Close', array($handler, "close")); $serv->on('Shutdown', array($handler, "shutdown")); $serv->on('Timer', array($handler, "timer")); $serv->on('WorkerStart', array($handler, "workStart")); $serv->on('WorkerStop', array($handler, "workStop")); $serv->on('Task', array($handler, "task")); $serv->on('Finish', array($handler, "finish")); $serv->on('WorkerError', array($handler, "workError")); $serv->start(); }
public function __construct() { $this->table = new swoole_table(1024); $this->table->column('serverfd', swoole_table::TYPE_INT, 8); $this->table->create(); define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); define('MYPATH', dirname(APPLICATION_PATH)); $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini"); $this->application->bootstrap(); $config_obj = Yaf_Registry::get("config"); $distributed_config = $config_obj->distributed->toArray(); $server = new swoole_server($distributed_config['ServerIp'], $distributed_config['port'], SWOOLE_PROCESS, SWOOLE_SOCK_TCP); if (isset($distributed_config['logfile'])) { $server->set(array('worker_num' => 4, 'task_worker_num' => 4, 'dispatch_mode' => 4, 'daemonize' => true, 'log_file' => $distributed_config['logfile'])); } else { $server->set(array('worker_num' => 4, 'task_worker_num' => 4, 'dispatch_mode' => 4, 'daemonize' => true)); } require_once __DIR__ . "/DistributedClient.php"; $server->on('Start', array(&$this, 'onStart')); $server->on('WorkerStart', array(&$this, 'onWorkerStart')); $server->on('Connect', array(&$this, 'onConnect')); $server->on('Receive', array(&$this, 'onReceive')); $server->on('Task', array(&$this, 'onTask')); $server->on('Finish', array(&$this, 'onFinish')); $server->on('Close', array(&$this, 'onClose')); $server->on('ManagerStop', array(&$this, 'onManagerStop')); $server->on('WorkerError', array(&$this, 'onWorkerError')); $server->start(); }
public function serverrun($queue) { try { $this->_setting($queue); } catch (Exception $e) { throw new \LogicException($e->getMessage()); } $serv = new swoole_server($this->host, $this->port, $this->mode); $serv->set($this->config); $serv->on('Start', array($this->objCallback, 'onStart')); $serv->on('Receive', array($this->objCallback, 'onReceive')); $serv->on('Connect', array($this->objCallback, 'onConnect')); $serv->on('Timer', array($this->objCallback, 'onTimer')); $serv->on('Task', array($this->objCallback, 'onTask')); $serv->on('Finish', array($this->objCallback, 'onFinish')); $serv->on('WorkerStart', array($this->objCallback, 'onWorkerStart')); $serv->on('WorkerStop', array($this->objCallback, 'onWorkerStop')); $onArray = array('onShutdown', 'onClose', 'onPipeMessage', 'onManagerStart', 'onManagerStop', 'onWorkerError'); foreach ($onArray as $on) { if (method_exists($this->objCallback, $on)) { $serv->on(str_replace('on', '', $on), array($this->objCallback, $on)); } } $serv->start(); }
function serve() { $serv = new \swoole_server(SERVERHOST, SERVERPORT); $serv->on('workerStart', [$this, 'onStart']); $serv->on('receive', [$this, 'onReceive']); $serv->set(array('worker_num' => 1, 'dispatch_mode' => 1, 'open_length_check' => true, 'package_max_length' => 8192000, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4)); $serv->start(); }
function run($setting = array()) { $this->runtimeSetting = array_merge($this->runtimeSetting, $setting); if (self::$pidFile) { $this->runtimeSetting['pid_file'] = self::$pidFile; } if (!empty(self::$options['daemon'])) { $this->runtimeSetting['daemonize'] = true; } if (!empty(self::$options['worker'])) { $this->runtimeSetting['worker_num'] = intval(self::$options['worker']); } if (!empty(self::$options['thread'])) { $this->runtimeSetting['reator_num'] = intval(self::$options['thread']); } if (!empty(self::$options['tasker'])) { $this->runtimeSetting['task_worker_num'] = intval(self::$options['tasker']); } $this->sw->set($this->runtimeSetting); $version = explode('.', SWOOLE_VERSION); //1.7.0 if ($version[1] >= 7) { $this->sw->on('ManagerStart', function ($serv) { Swoole\Console::setProcessName($this->getProcessName() . ': manager'); }); } $this->sw->on('Start', array($this, 'onMasterStart')); $this->sw->on('Shutdown', array($this, 'onMasterStop')); $this->sw->on('ManagerStop', array($this, 'onManagerStop')); $this->sw->on('WorkerStart', array($this, 'onWorkerStart')); if (is_callable(array($this->protocol, 'onTimer'))) { $this->sw->on('Connect', array($this->protocol, 'onConnect')); } if (is_callable(array($this->protocol, 'onTimer'))) { $this->sw->on('Close', array($this->protocol, 'onClose')); } if (self::$useSwooleHttpServer) { $this->sw->on('Request', array($this->protocol, 'onRequest')); } else { $this->sw->on('Receive', array($this->protocol, 'onReceive')); } if (is_callable(array($this->protocol, 'WorkerStop'))) { $this->sw->on('WorkerStop', array($this->protocol, 'WorkerStop')); } //swoole-1.8已经移除了onTimer回调函数 if ($version[1] < 8) { if (is_callable(array($this->protocol, 'onTimer'))) { $this->sw->on('Timer', array($this->protocol, 'onTimer')); } } if (is_callable(array($this->protocol, 'onTask'))) { $this->sw->on('Task', array($this->protocol, 'onTask')); $this->sw->on('Finish', array($this->protocol, 'onFinish')); } $this->sw->start(); }
function run() { $serv = new swoole_server("127.0.0.1", 8002); $serv->set(array('timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 1, 'backlog' => 128, 'max_conn' => 10000, 'dispatch_mode' => 2)); $serv->on('Receive', array($this, 'onReceive')); $serv->on('Close', array($this, 'onClose')); //swoole_server_addtimer($serv, 2); #swoole_server_addtimer($serv, 10); $serv->start(); }
function run() { $serv = new swoole_server("", 9509); $serv->set(array('worker_num' => 1, 'max_request' => 0)); $serv->on('WorkerStart', array($this, 'onStart')); //$serv->on('Connect', array($this, 'onConnect')); $serv->on('Receive', array($this, 'onReceive')); //$serv->on('Close', array($this, 'onClose')); $serv->start(); }
public function run() { $serv = new swoole_server('0.0.0.0', 9501); $serv->on('connect', array($this, 'onConnect')); $serv->on('receive', array($this, 'onReceive')); $serv->on('close', array($this, 'onClose')); $serv->on('workerstart', array($this, 'onWorkerStart')); $serv->set($this->setting); $serv->start(); }
function run() { $serv = new swoole_server("127.0.0.1", 55151); $serv->set(array('worker_num' => 1)); $serv->on('WorkerStart', array($this, 'onStart')); //$serv->on('Connect', array($this, 'onConnect')); $serv->on('Receive', array($this, 'onReceive')); //$serv->on('Close', array($this, 'onClose')); $serv->start(); }
public function __construct() { //初始化应用 $this->initializationOfApp(); // 创建swoole_http_server对象 $server = new swoole_server("0.0.0.0", 9500); $server->on('connect', array($this, 'onConnect')); $server->on('receive', array($this, 'onReceive')); $server->on('close', array($this, 'onClose')); $server->start(); }
/** * server run */ function run() { $serv = new swoole_server('0.0.0.0', 9501, SWOOLE_BASE); $serv->set(array('worker_num' => 3, 'daemonize' => true)); $this->serv = $serv; $serv->on('receive', array($this, 'onReceive')); $serv->on('close', array($this, 'onClose')); $serv->on('start', array($this, 'onStart')); $serv->on('WorkerStart', array($this, 'onWorkerStart')); $serv->start(); }
public function __construct() { define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini"); $this->application->bootstrap(); $server = new swoole_server("0.0.0.0", 9503); $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8)); $server->on('Receive', array($this, 'onReceive')); $server->on('Task', array($this, 'onTask')); $server->on('Finish', array($this, 'onFinish')); $server->start(); }
/** * @param $handel */ public function run() { $this->server = new \swoole_server($this->host, $this->port, SWOOLE_BASE, SWOOLE_SOCK_TCP); $this->server->set($this->config); $this->server->on('Start', [$this, 'onMasterStart']); $this->server->on('Shutdown', [$this, 'onMasterStop']); $this->server->on('ManagerStop', [$this, 'onManagerStop']); $this->server->on('WorkerStart', [$this, 'onWorkerStart']); $this->server->on('Connect', [$this->handel, 'onConnect']); $this->server->on('Receive', [$this->handel, 'onReceive']); $this->server->on('Close', [$this->handel, 'onClose']); $this->server->on('WorkerStop', [$this->handel, 'onStop']); if (is_callable([$this->handel, "onTimer"])) { $this->server->on("Timer", [$this->handel, "onTimer"]); } if (is_callable([$this->handel, 'onTask'])) { $this->server->on("Timer", [$this->handel, "onTimer"]); $this->server->on("Finish", [$this->handel, "onFinish"]); } $this->server->start(); }
public function start($setting = array()) { Trace::info(str_repeat('-', 100)); Trace::info(str_pad('[ ShadowSocks Swoole PHP ]', 100, ' ', STR_PAD_BOTH)); Trace::info(str_pad("IP:" . self::$conf['ip'] . " PORT:" . self::$conf['port'], 100, ' ', STR_PAD_BOTH)); Trace::info(str_pad("PassWord : "******"Encode :" . self::$conf['method'], 100, ' ', STR_PAD_BOTH)); Trace::info(str_repeat('-', 100)); $default = ['timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 4, 'backlog' => 128, 'dispatch_mode' => 2]; $setting = array_merge($default, $setting); $this->serv->set($setting); $this->serv->start(); }
function run() { $serv = new swoole_server("0.0.0.0", 10000); $serv->set(array('timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 2, 'backlog' => 128, 'max_conn' => 10000, 'dispatch_mode' => 2)); $serv->on('WorkerStart', array($this, 'onStart')); $serv->on('Connect', array($this, 'onConnect')); $serv->on('Receive', array($this, 'onReceive')); $serv->on('Close', array($this, 'onClose')); $serv->on('WorkerStop', array($this, 'onShutdown')); //swoole_server_addtimer($serv, 2); #swoole_server_addtimer($serv, 10); $serv->start(); }
public function __construct() { //初始化应用 $this->initializationOfApp(); // 创建swoole_http_server对象 $server = new swoole_server("0.0.0.0", 9504, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); // 设置参数 $server->set(array('worker_num' => 4, 'dispatch_mode' => 2, 'task_worker_num' => 4)); $server->on('receive', array($this, 'onReceive')); $server->on('task', array($this, 'onTask')); $server->on('finish', array($this, 'onFinish')); $server->start(); }
public function index() { $server = new \swoole_server(C('SERVICE_IP'), C('SERVICE_PORT')); $server->set(array('task_worker_num' => 4, 'daemonize' => false, 'heartbeat_check_interval' => 30, 'heartbeat_idle_time' => 60)); $server->on('WorkerStart', array($this, 'onWorkerStart')); $server->on('Receive', array($this, 'onReceive')); $server->on('Task', array($this, 'onTask')); $server->on('Finish', array($this, 'onFinish')); $server->on('pipeMessage', array($this, 'onPipeMessage')); $server->on('WorkerError', array($this, 'onWorkerError')); $this->conn = array(); $server->start(); }
/** * start a swoole server in cli * * @return mixed */ public function start() { $serv = new \swoole_server(self::HOST, self::PORT, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); // init config $serv->set(['max_conn' => 256, 'worker_num' => 4, 'dispatch_mode' => 3, 'task_worker_num' => 4, 'daemonize' => TRUE, 'open_eof_check' => TRUE, 'open_eof_split' => TRUE, 'package_eof' => self::EOFF, 'log_file' => APPPATH . 'logs/swoole.log', 'heartbeat_check_interval' => 30]); // listen on $serv->on('connect', [$this, 'on_connect']); $serv->on('receive', [$this, 'on_receive']); $serv->on('finish', [$this, 'on_finish']); $serv->on('close', [$this, 'on_close']); $serv->on('task', [$this, 'on_task']); // start server $serv->start(); }
public function __construct() { $serv = new swoole_server("127.0.0.1", 9502); $serv->set(array('worker_num' => 16, 'daemonize' => true)); $serv->on('connect', function ($serv, $fd) { echo "Client:Connect.\n"; }); $serv->on('receive', function ($serv, $fd, $from_id, $data) { $serv->send($fd, $data); $serv->close($fd); }); $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); $serv->start(); }
/** * 启动server 只能运行在cli模式下 * php index.php cli/server/startServer * * @access public * @param void * @return void * @author wangleiming **/ public function startServer() { $serv = new swoole_server('127.0.0.1', 9501); $serv->set(array('worker_num' => 2, 'task_worker_num' => 4, 'dispatch_mode' => 2, 'daemonize' => 0, 'log_file' => '')); $serv->on('Start', array($this, 'onStart')); $serv->on('Connect', array($this, 'onConnect')); $serv->on('Receive', array($this, 'onReceive')); $serv->on('Close', array($this, 'onClose')); $serv->on('Shutdown', array($this, 'onShutdown')); $serv->on('Timer', array($this, 'onTimer')); $serv->on('Task', array($this, 'onTask')); $serv->on('Finish', array($this, 'onFinish')); $serv->on('WorkerStart', array($this, 'onWorkerStart')); $serv->on('WorkerStop', array($this, 'onWorkerStop')); $serv->on('WorkerError', array($this, 'onWorkerError')); $serv->start(); }
public function __construct() { define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); define('MYPATH', dirname(APPLICATION_PATH)); $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini"); $this->application->bootstrap(); $config_obj = Yaf_Registry::get("config"); $task_config = $config_obj->task->toArray(); $server = new swoole_server($task_config['ServerIp'], $task_config['port']); if (isset($task_config['logfile'])) { $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8, 'log_file' => $task_config['logfile'])); } else { $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8)); } $server->on('Receive', array($this, 'onReceive')); $server->on('Task', array($this, 'onTask')); $server->on('Finish', array($this, 'onFinish')); $server->start(); }
public function serverAction() { $server = new swoole_server("192.168.80.140", 9021, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); $setting = array('daemonize' => 0, 'reactor_num' => 2, 'worker_num' => 4, 'max_request' => 2000, 'backlog' => 128, 'open_cpu_affinity' => 1, 'open_tcp_nodelay' => 1, 'log_file' => '../log/swoole_test.log', 'heartbeat_check_interval' => 30, 'heartbeat_idle_time' => 60, 'dispatch_mode' => 2); $server->set($setting); var_dump($server->setting); $server->on("connect", function ($server, $fd) { #echo "connect\n"; }); $server->on("receive", function ($server, $fd, $from_id, $data) { $server->send($fd, 'Swoole: ' . $data); $server->close($fd); }); $server->on("close", function ($server, $fd) { #echo "close\n"; #$server->shutdown(); }); $server->start(); return false; }
public function run($ip, $port = 9501, $model = SWOOLE_PROCESS, $type = SWOOLE_SOCK_UDP) { $serv = new \swoole_server($ip, $port, $model, $type); $serv->config = Config::get_swoole_config(); $serv->set($serv->config); $serv->on('Start', array($this, 'onStart')); $serv->on('WorkerStart', array($this, 'onWorkerStart')); $serv->on('Connect', array($this, 'onConnect')); $serv->on('Receive', array($this, 'onReceive')); $serv->on('Task', array($this, 'onTask')); $serv->on('Finish', array($this, 'onFinish')); $serv->on('WorkerError', array($this, 'onWorkerError')); $serv->on('Close', array($this, 'onClose')); $serv->on('WorkerStop', array($this, 'onWorkerStop')); $serv->on('Shutdown', array($this, 'onShutdown')); $serv->on('ManagerStart', function ($serv) { global $argv; swoole_set_process_name("php {$argv[0]}: manager"); }); $serv->start(); }