How do I use the thinkphp framework in a server-side callback function when using Swoole extensions in PHP?

Source: Internet
Author: User

  serv = new Swoole_server ("0.0.0.0", 9501); $this->serv->set (' worker_num ' = 8, ' daemonize ' and false, ' max_request        ' = = 10000, ' dispatch_mode ' = 3, ' Debug_mode ' + 1, ' task_worker_num ' and 8        ));        $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 '));        Bind callback $this->serv->on (' Task ', Array ($this, ' ontask '));        $this->serv->on (' Finish ', Array ($this, ' onfinish '));    $this->serv->start ();        } Public Function Onworkerstart ($serv, $worker _id) {echo "onworkerstart\n";     Determines whether the task worker process if ($worker _id >= $serv->setting[' Worker_num ') {$this->pdo = new PDO (           "Mysql:host=localhost;port=3306;dbname=test", "root", "123456", Array (Pdo::mysql_attr_init_command = "SET NAMES ' UTF8 ';", Pdo::attr_errmode =& Gt        Pdo::errmode_exception, pdo::attr_persistent = true));    }} Public Function OnConnect ($serv, $FD, $from _id) {echo "Client {$FD} connect\n"; Public Function OnReceive (Swoole_server $serv, $FD, $from _id, $data) {$sql = array (' sql ' = = ' I            Nsert into Test values (PID =?, name =?) ', ' param ' = = Array (0, "' Name '")        ), ' fd ' = $FD);    $serv->task (Json_encode ($sql));    Public Function OnClose ($serv, $FD, $from _id) {echo "Client {$FD} close connection\n"; The Public Function Ontask ($serv, $task _id, $from _id, $data) {try{$sql =Json_decode ($data, true);            $statement = $this->pdo->prepare ($sql [' SQL ']);                 $statement->execute ($sql [' param ']);            $serv->send ($sql [' FD '], "Insert");        return true;            } catch (Pdoexception $e) {var_dump ($e);        return false; }} Public Function OnFinish ($serv, $task _id, $data) {}}new mysqlpool ();

What about MySQL link here or what if I use the data in thinkphp?

How to quote? include_once 'core/ThinkPHP.php';can it be used in such a way?

The other point is that the server must use the command line mode when it goes down, how do I open it in Windows?

Reply content:


  serv = new Swoole_server ("0.0.0.0", 9501); $this->serv->set (' worker_num ' = 8, ' daemonize ' and false, ' max_request        ' = = 10000, ' dispatch_mode ' = 3, ' Debug_mode ' + 1, ' task_worker_num ' and 8        ));        $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 '));        Bind callback $this->serv->on (' Task ', Array ($this, ' ontask '));        $this->serv->on (' Finish ', Array ($this, ' onfinish '));    $this->serv->start ();        } Public Function Onworkerstart ($serv, $worker _id) {echo "onworkerstart\n";     Determines whether the task worker process if ($worker _id >= $serv->setting[' Worker_num ') {$this->pdo = new PDO (           "Mysql:host=localhost;port=3306;dbname=test", "root", "123456", Array (Pdo::mysql_attr_init_command = "SET NAMES ' UTF8 ';", Pdo::attr_errmode =& Gt        Pdo::errmode_exception, pdo::attr_persistent = true));    }} Public Function OnConnect ($serv, $FD, $from _id) {echo "Client {$FD} connect\n"; Public Function OnReceive (Swoole_server $serv, $FD, $from _id, $data) {$sql = array (' sql ' = = ' I            Nsert into Test values (PID =?, name =?) ', ' param ' = = Array (0, "' Name '")        ), ' fd ' = $FD);    $serv->task (Json_encode ($sql));    Public Function OnClose ($serv, $FD, $from _id) {echo "Client {$FD} close connection\n"; The Public Function Ontask ($serv, $task _id, $from _id, $data) {try{$sql =Json_decode ($data, true);            $statement = $this->pdo->prepare ($sql [' SQL ']);                 $statement->execute ($sql [' param ']);            $serv->send ($sql [' FD '], "Insert");        return true;            } catch (Pdoexception $e) {var_dump ($e);        return false; }} Public Function OnFinish ($serv, $task _id, $data) {}}new mysqlpool ();

What about MySQL link here or what if I use the data in thinkphp?

How to quote? include_once 'core/ThinkPHP.php';can it be used in such a way?

The other point is that the server must use the command line mode when it goes down, how do I open it in Windows?

You can use thinkphp. Swoole is a standard PHP extension that can be executed as long as the PHP syntax is met.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.