Beispiel #1
0
 /**
  * 绑定数据
  * @param string $event 事件
  * @param object $cls 处理类
  * @return void
  */
 public function bind($event, $cls)
 {
     if ($this->isStart) {
         return;
     }
     $this->server->on($event, [$cls, 'handle']);
 }
 public function __construct()
 {
     $ip = self::$conf['ip'] = empty(self::$conf['ip']) ? '0.0.0.0' : self::$conf['ip'];
     $this->serv = new swoole_server($ip, self::$conf['port'], SWOOLE_PROCESS, SWOOLE_TCP);
     $this->serv->on('connect', [$this, 'onConnect']);
     $this->serv->on('receive', [$this, 'onReceive']);
     $this->serv->on('close', [$this, 'onClose']);
 }
Beispiel #3
0
 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();
 }
Beispiel #4
0
 public function __construct($host, $ip, $config, $bufferDriverClass)
 {
     $server = new \swoole_server($host, $ip, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
     $server->set($config);
     $server->on('receive', array($this, 'onReceive'));
     $server->on('close', array($this, 'onClose'));
     $this->server =& $server;
     $this->bufferDriverClass = $bufferDriverClass;
 }
 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();
 }
Beispiel #6
0
 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($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();
 }
Beispiel #8
0
 function __construct($base = false)
 {
     $mode = $base ? SWOOLE_BASE : SWOOLE_PROCESS;
     $serv = new swoole_server("127.0.0.1", 9501, $mode);
     $serv->on('Connect', [$this, 'onConnect']);
     $serv->on('receive', [$this, '_receive']);
     $serv->on('workerStart', [$this, 'onWorkerStart']);
     $serv->on('Close', [$this, 'onClose']);
     $this->serv = $serv;
 }
Beispiel #9
0
 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();
 }
Beispiel #11
0
 /**
  * 创建server 对象
  *
  * @return \swoole_server
  */
 private function createServer()
 {
     $server = new \swoole_server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
     $server->set($this->normalizedConfig());
     $server->on('workerStart', [$this, 'onWorkerStart']);
     $server->on('connect', [$this, 'onConnect']);
     $server->on("receive", [$this, "onReceive"]);
     $server->on('close', [$this, 'onClose']);
     return $server;
 }
 function run($setting = array())
 {
     $this->swooleSetting = array_merge($this->swooleSetting, $setting);
     if (!empty($this->swooleSetting['pid_file'])) {
         $this->pid_file = $this->swooleSetting['pid_file'];
     }
     $this->sw->set($this->swooleSetting);
     $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('ManagerStop', array($this, 'onManagerStop'));
     $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();
 }
Beispiel #13
0
 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();
 }
Beispiel #14
0
 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();
 }
Beispiel #15
0
 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();
 }
Beispiel #16
0
 /**
  * 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();
 }
Beispiel #17
0
 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();
 }
Beispiel #18
0
 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();
 }
Beispiel #19
0
 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();
 }
Beispiel #20
0
 /**
  * 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();
 }
 function run()
 {
     $serv = new swoole_server("0.0.0.0", 9507);
     $serv->set(array('worker_num' => 1));
     $serv->on('Start', function ($serv) {
         echo "Swoole Upload Server running\n";
     });
     $this->root_path = rtrim($this->root_path, ' /');
     $serv->on('connect', array($this, 'onConnect'));
     $serv->on('receive', array($this, 'onreceive'));
     $serv->on('close', array($this, 'onclose'));
     $this->serv = $serv;
     $serv->start();
 }
Beispiel #22
0
 function listen($host = '0.0.0.0', $port = 9510)
 {
     $swoole_setting = (require dirname(__DIR__) . '/configs/swoole.php');
     $server = new swoole_server($host, $port, SWOOLE_BASE);
     $swoole_setting['open_eof_check'] = true;
     $swoole_setting['open_eof_split'] = true;
     $swoole_setting['package_eof'] = self::EOF;
     $server->set($swoole_setting);
     $server->on('WorkerStart', [$this, 'onStart']);
     $server->on('WorkerStop', [$this, 'onStop']);
     $server->on('receive', [$this, 'onReceive']);
     $this->server = $server;
     $this->server->start();
 }
Beispiel #23
0
 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();
 }
Beispiel #24
0
 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();
 }
Beispiel #25
0
 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();
 }
Beispiel #26
0
 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();
 }
Beispiel #27
0
 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();
 }
Beispiel #28
0
 function run($_setting = array())
 {
     $default_setting = array('dispatch_mode' => 3, 'max_request' => 0, 'open_eof_split' => true, 'package_eof' => self::EOF);
     define('SWOOLE_SERVER', true);
     $setting = array_merge($default_setting, $_setting);
     $serv = new \swoole_server('0.0.0.0', self::PORT, SWOOLE_PROCESS);
     $serv->set($setting);
     $serv->on('receive', array($this, 'onReceive'));
     $this->serv = $serv;
     $this->serv->start();
 }
Beispiel #29
0
 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;
 }
Beispiel #30
0
 public function start()
 {
     $this->setTitle('proxy');
     $type = $this->option['type'] == 'UDP' ? SWOOLE_SOCK_UDP : SWOOLE_SOCK_TCP;
     $server = new \swoole_server($this->option['host'], $this->option['port'], SWOOLE_PROCESS, $type);
     $server->set($this->option['setting']);
     $event = ['Start', 'Receive', 'Close', 'Shutdown', 'Timer', 'WorkerStart', 'WorkerStop', 'Task', 'Finish', 'WorkerError', 'MasterConnect', 'MasterClose', 'Connect'];
     foreach ($event as $event) {
         $server->on($event, [$this, sprintf('on%s', $event)]);
     }
     $this->server = $server;
     $this->server->start();
 }