Recently, there is a demand: the message may want to implement a proactive trigger message push functionality, this can be achieved to the template messages that give all members to send a custom message, without the need to send messages through the client, message on the server in listening transferred It is made with respect to the business logic.
Unsolicited messages pushing to achieve
common swoole we use to write WebSocket services may be the most used is open, message, close the three listening state, but never look onRequest use the following callbacks, yes, the solution to this initiative push message is the need for a callback with onRequest.
The official document: Because swoole_websocket_server inherited from swoole_http_server, so there onRequest callbacks websocket in.
Detailed implementation:
1 # Here is a laravel in the Commands 2 # running php artisan swoole start to run . 3 <? PHP . 4 . 5 namespace the App \ Console \ the Commands; . 6 . 7 use Illuminate \ Console \ the Command; . 8 use swoole_websocket_server; . 9 10 class Swoole the extends the Command . 11 { 12 is public $ WS ; 13 is / * * 14 . * and Signature of The name of The Command Console 15 * 16 * @var String . 17 */ 18 protected $signature = 'swoole {action}'; 19 20 /** 21 * The console command description. 22 * 23 * @var string 24 */ 25 protected $description = 'Active Push Message'; 26 27 /** 28 * Create a new command instance. 29 * 30 * @return void 31 */ 32 public function __construct() 33 { 34 parent::__construct(); 35 } 36 37 /** 38 * Execute the console command. 39 * 40 * @return mixed 41 */ 42 public function handle() 43 { 44 $arg = $this->argument('action'); 45 switch ($arg) { 46 case 'start': 47 $this->info('swoole server started'); 48 $this->start(); 49 break; 50 case 'stop': 51 $this->info('swoole server stoped'); 52 break; 53 case 'restart': 54 $this->info('swoole server restarted'); 55 break; 56 } 57 } 58 59 /** 60 * 启动Swoole 61 */ 62 private function start() 63 { 64 $this->ws = newswoole_websocket_server ( "0.0.0.0", 9502 ); 65 // listening event WebSocket connection opening 66 $ the this -> WS-> ON ( 'Open', function ( $ WS , $ Request ) { 67 }); 68 // Listener WebSocket event message 69 $ the this -> WS-> ON ( 'message', function ( $ WS , $ Frame ) { 70 $ the this -> info ( "Client IS the SendMessage \ n-" ); 71 is }); 72 // listener WebSocket active push event message 73 is $ the this -> WS-> ON ( 'Request',function ($request, $response) { 74 $scene = $request->post['scene']; // 获取值 75 $this->info("client is PushMessage\n".$scene); 76 }); 77 //监听WebSocket连接关闭事件 78 $this->ws->on('close', function ($ws, $fd) { 79 $this->info("client is close\n"); 80 }); 81 $this->ws->start(); 82 } 83 }
I said earlier that the implementation onRequest of swoole, under the following onRequest achieve active trigger a callback in the controller. Implementation is the familiar curl request.
1 # subsequent calls activepush method will be printed out in cmd 2 # Client IS PushMessage active push message word . 3 / * * . 4 * the CURL request . 5 * @param $ Data . 6 * / . 7 public function curl ( $ Data ) . 8 { . 9 curl $ = curl_init (); 10 curl_setopt ( $ curl , CURLOPT_URL to, "http://127.0.0.1:9502" ); . 11 curl_setopt ( $ curl , CURLOPT_RETURNTRANSFER,. 1 ); 12 is curl_setopt ( $ curl, CURLOPT_HEADER,. 1 ); 13 is curl_setopt ( $ curl , CURLOPT_POST,. 1 ); 14 curl_setopt ( $ curl , CURLOPT_POSTFIELDS, $ Data ); 15 the curl_exec ( $ curl ); 16 curl_close ( $ curl ); . 17 } 18 is . 19 / * * 20 * active trigger 21 is * / 22 is public function activepush () 23 is { 24 $ param [ 'SCENE'] = 'active push message' ; 25 $ the this-> curl ( $ param ); // actively push messages
Use
onRequest callback is especially suitable for push messages need to call in the controller, such as message templates and the like, call in the controller.
Recommended reading:
Swoole concurrent queue and processing system implemented Redis
PHP laravel + thrift + swoole create micro-Services Framework
PHP Swoole-Demo TCP server simple implementation
PHP Swoole long connection Frequently Asked Questions
PHP + Swoole concurrent programming charm
php Swoole regular tasks in milliseconds
Swoole developed in conjunction with thinkphp5 WebSocket Chat Communication System