April work summary--Swoole high-performance php framework (project: WeChat applet chat program)

I have learned before, nodejs, powerful performance.

This year's goal is to go deep into workman, and use swool. Before using workman to deal with the chat system, the work needs to be run on the window. Therefore, in terms of performance, the advantages of workman are not used.

In the middle of this month, just as the project needs, we used swoole to develop the interface of the customer service chat system of the WeChat applet mall.

Through the interface provided by WeChat official, the chat between customers and customer service is realized.

write picture description here

Use swoole to easily get websocket and chat online.

Prerequisites for sending a message to a specified user

When a client connects to a socket, the system assigns an fd, which identifies the individual client.
When logging in, the customer service terminal can send the user's information to swoole, including the customer service id.

In order to send a message for a specified customer or group, store the customer service id and the corresponding fd in redis. When opening the socket and sending it to a customer service, it only needs to read the corresponding key of the redis customer service to send it to the specified user.

asynchronous

Although the current volume of inquiries is not large, for the longevity of the code, it can be used for a longer time. When the customer service sends a message, it adds a task asynchronous when receiving the message. When the volume of consultation is large, swoole can also do it well.

Asynchronous storage log

Chat, not like writing articles. There may be tens of thousands of inquiries per second, so in order to make the system smooth, swoole provides asynchronous reading and writing to help us read and write logs in unnecessary time-consuming operations.

function log_text($data){
    swoole_async_writefile(__DIR__ . '/log.txt', $data.$data."\n\n", function($filename) {
    }, FILE_APPEND);
}

Basic async template

object-oriented class

<?php

class WebsocketTest {
    public $server;
    public function __construct() {
        $this->server = new swoole_websocket_server("0.0.0.0", 7272);
        $this->server->on('open',[$this,'open']);
        $this->server->on('message', [$this,'message']);
        $this->server->on('close', [$this,'close']);
        $this->server->on('request', [$this,'request']);
        $this->server->on('task',[$this,'task']);
        $this->server->on('finish',[$this,'finish']);
        $this->server->set([
//            'daemonize'=>true,
        'task_worker_num'=>10,
        ]);
        $this->server->start();
    }
    //@异步接受数据, 发送数据
    function finish($server, $task_id,$data){
        $server->push($data['fd'],$data['data']);
    }
    //@异步处理数据
    function task($server, $task_id, $from_id, $data){
        //接受到数据之后处理复杂逻辑处理
        sleep('5');
        $data['data'] = '服务器发送'.$data['data'];
        $server->finish($data);
    }
    function request($request, $response) {
        foreach ($this->server->connections as $fd) {
            $this->server->push($fd, $request->get['message']);
        }
    }
    function close($ser, $fd) {
        echo "client {$fd} closed\n";
    }
    function message(swoole_websocket_server $server, $frame) {
        $data = [
            'fd'=>$frame->fd,
            'data'=>$frame->data,
        ];
        $server->task($data);
    }
    function open( $server, $request) {

    }
}
new WebsocketTest();


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326158015&siteId=291194637