How does laravel use swoole to develop websocket active message push

Posted by nedpwolf on Mon, 21 Oct 2019 11:04:35 +0200

Requirement: to achieve a function that can actively trigger message push, this function can be used to send customized messages to all members of the template message, instead of sending messages through the client. The message in the message on the service end listens for the messages to be sent for relative business logic.

Active message push implementation
In general, we use swoole to write WebSocket services, which may use the most open, message, and close monitoring states. However, we haven't seen the use of the following onRequest callback. Yes, we need to use onRequest callback to solve this active message push.
Official document: because swoole ﹣ websocket ﹣ server inherits from swoole ﹣ http ﹣ server, there is an onRequest callback in websocket.

Detailed implementation:

# Here is a command in laravel
# Run PHP artican swoole start to run
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use swoole_websocket_server;

class Swoole extends Command
{
    public $ws;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'swoole {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Active Push Message';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $arg = $this->argument('action');
        switch ($arg) {
            case 'start':
                $this->info('swoole server started');
                $this->start();
                break;
            case 'stop':
                $this->info('swoole server stoped');
                break;
            case 'restart':
                $this->info('swoole server restarted');
                break;
        }
    }

    /**
     * Start Swoole
     */
    private function start()
    {
        $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
        //Listen for WebSocket connection open events
        $this->ws->on('open', function ($ws, $request) {
        });
        //Listen to WebSocket message events
        $this->ws->on('message', function ($ws, $frame) {
            $this->info("client is SendMessage\n");
        });
        //Listen to WebSocket active push message events
        $this->ws->on('request', function ($request, $response) {
            $scene = $request->post['scene'];       // Get value
            $this->info("client is PushMessage\n".$scene);
        });
        //Listen for WebSocket connection closing events
        $this->ws->on('close', function ($ws, $fd) {
            $this->info("client is close\n");
        });
        $this->ws->start();
    }
}

The above is the implementation of onRequest in swoole, and the following implementation actively triggers the onRequest callback in the controller. The implementation method is the familiar curl request.

# After the activepush method is called, it will be printed out in cmd. 
# client is PushMessage
    /**
     * CURL request
     * @param $data
     */
    public function curl($data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://127.0.0.1:9502");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_exec($curl);
        curl_close($curl);
    }
    
    /**
     * Active trigger
     */
    public function activepush()
    {
        $param['scene'] = 'Active push message';
        $this->curl($param);            // Active push message

purpose
The onRequest callback is especially applicable to push messages that need to be invoked in the controller, such as template messages, and so on, and are invoked in the controller.
Maybe a lot of PHP programmers who have been developing for 1-5 years have come here and many friends have met with bottlenecks, want to advance to middle and senior level and architect!! To learn more about swoole, laravel, thinkphp6, and swoft microservices, you will encounter many difficulties in using them. In order to advance PHP architects as soon as possible, you can refer to this PHP architect roadmap and architecture tutorial. Please poke here.

Topics: PHP curl Laravel