Business background

It’s a game matching App, and PHP uses Swoole to create websockets to provide game matching services

The matching process is as follows

  • Authentication of the match (handshake event handling)
  • Matching business logic (for example, male can only match female, this also needs hot update, open event processing)
  • Data is returned and the connection is closed
  • After that, nodeJS provides the service

What you want to achieve

Automatically hot update code when matching business logic code is changed without restarting the service

Swoole official documentation on hot updates

The core is that the code you want to hot update must be introduced in the onWorkerStart event

Install swoole and inotify

My own “blueprints”

If you start the Swoole service using Artisan, the hot update may fail because too many classes have been loaded before onWorkerStart



index

Set constants and instantiate MatchServer to start the service

require 'MatchServer.php'; if (php_sapi_name() ! = 'cli') die(' please start in CLI mode '); define('ROOT_PATH',dirname(dirname(dirname(__DIR__))).'/'); define('PORT',20005); $server = new MatchServer();Copy the code

MatchServer

class MatchServer{ private $server; protected $application; $table = new swoole_table(1024); $table = new swoole_table(1024); $table->column('fd', swoole_table::TYPE_INT); $table->column('uid', swoole_table::TYPE_INT); $table->column('gameType', swoole_table::TYPE_STRING, 256); $table->column('data', swoole_table::TYPE_STRING, 256); $table->create(); $this->server = new swoole_websocket_server("0.0.0.0", PORT); $this->server->table = $table; $this->server->on('handShake', $this, 'onHandShake')); $this->server->on('workerStart', array($this, 'onWorkerStart')); $this->server->on('open', array($this, 'onOpen')); $this->server->on('message', array($this, 'onMessage')); $this->server->on('close', array($this, 'onClose')); $this->server->start(); } /** * Handle handshake ** @param swoole_http_request $request * @param swoole_http_response $response ** @return bool */ public Function onHandShake (\swoole_http_request $request, \swoole_http_response $response) {if($response->end(); return false; } //swoole handshake, because my match is handled in the open event, when I handle the handshake, I will not automatically call the open event, I have to call the // handshake code myself.. Too much.. Considering the length of the issue, not posted.. $this->onOpen($this->server, $request); return true; } @param $server * @param $worker_id */ public function onWorkerStart ($server, $worker_id) {// require root_path. 'public/index.php'; $this->application = new MatchApplication(); If ($worker_id == 0) {// Set hot update directory $dir = app_path('Game/Match'); $list[] = $dir; foreach (array_diff(scandir($dir), array('.', '.. ')) as $item) { $list[] = $dir.'/'.$item; } $notify = inotify_init(); foreach ($list as $item) { inotify_add_watch($notify, $item, IN_CREATE | IN_DELETE | IN_MODIFY); } swoole_event_add($notify, function () use ($notify,$server) { $events = inotify_read($notify); if (! Empty ($events)) {// Execute swolle reload $server->reload(); }}); }} @param $server @param $request */ public function onOpen ($server, $server) $this->application->onOpen($server,$request); } public function onMessage ($server, $frame){} @param $server * @param $fd */ public function onClose ($server, @param $fd) $fd) {if ($server-> exist($fd)) {$server->table->del($fd); }}}Copy the code

MatchApplication

Public function onOpen ($server, $request) {$fd = $request->fd; // Process business logic...... $server->push($fd,$data); $server->close($fd); }Copy the code

Start the service

$ php Index.php
Copy the code

Make sure onWorkerStart doesn’t load the code you want to hotupdate before it starts

public function onWorkerStart ($server, $worker_id)
{
    print_r(get_included_files());
    return;
}
Copy the code

I use Supervisor for guarding

As long as you can guarantee your salary to rise a step (constantly updated)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx and many other knowledge points can be shared free of charge to everyone, you can join my PHP technology exchange group 953224940

>>> Architect growth path