Laravel combined with swoole to implement websocket active message push

Posted by robindean on Wed, 16 Oct 2019 11:09:43 +0200

Laravel combined with swoole to implement websocket active message push
Recently, I am studying a project of laravel+swoole chat room. I want to realize a function that can actively trigger message push, which can 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 is monitored and transmitted 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.
Many friends here want to learn more about swoole, laravel, thinkphp, and many difficulties in using swoft microservices. In order for everyone to be an early PHP architect, you can refer to this PHP architect roadmap and architecture tutorial. Please poke here.)

Topics: PHP curl Laravel