Using laravel+Swoole to implement websocket active message push

angryTom
Release: 2023-04-07 21:06:01
forward
3416 people have browsed it

Recently there is a demand: I want to implement a function that can actively trigger message push. This can be used to send custom messages to template messages to all members without sending messages through the client. In message, the transmitted message is monitored and the corresponding business logic is performed.

Using laravel+Swoole to implement websocket active message push

Active message push implementation

Usually we use swoole to writeWebSocket The service may use the three listening states of open, message, and close the most, but never look at the use of the onRequest callback below. Yes, this is what is needed to solve this active message push. Use onRequest callback.

Official document: Because swoole_websocket_server inherits from swoole_http_server, there is an onRequest callback in websocket.

Detailed implementation:

# 这里是一个laravel中Commands
# 运行php artisan swoole start 即可运行
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;
        }
    }

    /**
     * 启动Swoole
     */
    private function start()
    {
        $this->ws = new swoole_websocket_server("0.0.0.0", 9502);
        //监听WebSocket连接打开事件
        $this->ws->on('open', function ($ws, $request) {
        });
        //监听WebSocket消息事件
        $this->ws->on('message', function ($ws, $frame) {
            $this->info("client is SendMessage\n");
        });
        //监听WebSocket主动推送消息事件
        $this->ws->on('request', function ($request, $response) {
            $scene = $request->post['scene'];       // 获取值
            $this->info("client is PushMessage\n".$scene);
        });
        //监听WebSocket连接关闭事件
        $this->ws->on('close', function ($ws, $fd) {
            $this->info("client is close\n");
        });
        $this->ws->start();
    }
}
Copy after login

What I mentioned above is the implementation of onRequest in swoole. The following implementation actively triggers the onRequest callback in the controller. The implementation method is the curl request we are familiar with.

# 调用activepush方法以后,会在cmd中打印出 
# client is PushMessage 主动推送消息 字眼
    /**
     * CURL请求
     * @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);
    }
    
    /**
     * 主动触发
     */
    public function activepush()
    {
        $param['scene'] = '主动推送消息';
        $this->curl($param);            // 主动推送消息
Copy after login

Purpose
onRequest callback is especially suitable for push messages that need to be called in the controller, such as template messages, etc., which are called in the controller.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Using laravel+Swoole to implement websocket active message push. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!