The thinkphp framework does not say much, there is no doubt about the toughness. See the official manual for a question
The Workerman framework is also very powerful (the background of the development framework is not very clear), as the official introduction:
Workerman, let you see the unknown side of PHP.
Of course a detailed introduction to the WM official also gives a very detailed manual. It is not intended to be a lengthy introduction and boast of the powerful words of TP and WM. In the past, when using TP3.2.3, the use of the Workerman framework, but at the time TP officials did not integrate the WM, but also separate use of two frames, their respective roles. Now the WM has been integrated in the TP5.
The integration steps also look at TP5 's manual:
https://www.kancloud.cn/manual/thinkphp5/235128
Here is the example code given by the TP5 official:
<?phpnamespace app\index\controller;use think\worker\server;use workerman\lib\timer; // Introduction of the WM Framework class library, class worker extends server{// protected $ socket = ' http://0.0.0.0:2348 '; protected $socket = ' tcp:// 0.0.0.0:2349 '; /** * receive information * @param $connection * @param $data */ public function onmessage ($connection, $data) { // temporarily set a Lastmessagetime property for connection, Used to record the last time a message was received $connection->lastmessagetime = time ( ); $connection- >send (Json_encode ($data)); $connection->send (' I have received your message '); } /** * callback function triggered when the connection is established * @param $connection */ Public function onconnect ($connection) { echo $connection->getremoteip (); } /** * callback function triggered when the connection is broken * @ param $connection */ public function OnClose ($connection) { echo $ connection->id . ' disconnect \r\n '; } /** * trigger When an error occurs on a client connection * @param $connection * @param $code * @param $msg */ public function onerror ($connection, $code, $ msg) { echo "error $code $msg \ n "; } /** * Each process starts * @param $worker */ public function onworkerstart ($worker) { // set a timer to run once per second after the process starts timer::add (1, function () use ($worker) { $time _now = time (); foreach ($ worker->connections as $connection) { // It is possible that the connection has not received a message, Lastmessagetime is set to the current time if (Empty ($ Connection->lastmessagetime)) { $connection->lastmessagetime = $time _now; continue; } / / The last communication interval is greater than the heartbeat interval, the client is considered offline and the connection is closed if ($time _now - $connection->lastmessagetime > 10) { $connection->close (); } } } ); echo $worker->id . "\ r \ n"; }}
This example is used as a TCP protocol development, see OnMessage, Onworkerstart two functions, is to add a timer, because it is a TCP server, must use heartbeat to detect whether the client due to extreme conditions (power loss, Abnormal shutdown), causing the disconnection (the server is not immediately aware of the client's disconnected state). The logical implementation is that the worker class inherits the server class, the worker class assigns a value to the parameter, and then the worker object is instantiated by the server constructor of the parent class.
------------------------------------------------------------
server.php
<?php// +----------------------------------------------------------------------// | thinkphp [ we can do it just think it ]// +------------------------- ---------------------------------------------// | copyright (c) 2006-2014 http:// thinkphp.cn all rights reserved.// +------------------------------------------------------ ----------------// | licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// + ----------------------------------------------------------------------// | author: liu21st <[email protected]>// +----------------------------------------------------------------- -----namespace think\worker;use workerman\worker;use workerman\lib\timer;/** * Worker controller extension Classes */abstract class Server{ protected $worker; protected&nbSP; $worker 2; protected $socket = '; protected $protocol = ' http '; protected $host = ' 0.0.0.0 '; protected $port = ' 2346 '; protected $processes = 1; /** * architecture functions * @access Public */ public function __construct () { // instancing Websocket Services $this->worker = new worker ($this->socket ? : $this->protocol . '://' . $this->host . ': ' . $this Port);//&NBSP;&NBSP;&NBSP;&Nbsp; $this->worker2 = new worker (' tcp://0.0.0.0:2349 '); // set the number of processes $this- >worker->count = $this->processes; // Set process name $this->worker->name = "Bluetooth"; // initialization $this- >init (); // set callback foreach ([' Onworkerstart ', ' onConnect ', ' onMessage ', ' onClose ', ' onError ' , ' onbufferfull ', ' onbufferdrain ', ' onworkerstop ', ' onworkerreload "] as $event) { if (Method_exists ($this, $event)) &NBSP;{&Nbsp; $this worker-> $event = [$this, $event]; } } // run worker worker::runall (); } protected function init () { }}
Although it is integrated, it is still flexible enough to customize any WM function you need in server.php.
This article from "for the Future" blog, declined reprint!
Using the Workerman framework in ThinkPHP5