您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman常用组件-FileMonitor文件监控组件
发布时间:2021-12-19 20:50:30编辑:雪饮阅读()
首先我們將依賴下載:
https://github.com/walkor/workerman-filemonitor
依賴下載后是一個代碼包。
放置與我們的項目中,放好之後咱們的項目結構如:
[root@localhost www.fpm.com]# tree
.
├── 404.html
├── allServer.php
├── Applications
│ └── FileMonitor
│ └── start.php
├── ca.crt
├── ca.key
├── channelServer.php
├── client.php
├── composer.json
├── composer.lock
├── config.php
├── httpServer.php
├── index2.html
├── index.html
├── Protocols
│ └── MyTextProtocol.php
├── README.md
├── server.crt
├── server.csr
├── server.key
├── telnet.php
├── textclient.php
├── udpClient.php
├── vendor
│ ├── autoload.php
│ ├── composer
│ │ ├── autoload_classmap.php
│ │ ├── autoload_files.php
│ │ ├── autoload_namespaces.php
│ │ ├── autoload_psr4.php
│ │ ├── autoload_real.php
│ │ ├── autoload_static.php
│ │ ├── ClassLoader.php
│ │ ├── installed.json
│ │ ├── installed.php
│ │ ├── InstalledVersions.php
│ │ ├── LICENSE
│ │ └── platform_check.php
│ ├── psr
│ │ └── http-message
│ │ ├── CHANGELOG.md
│ │ ├── composer.json
│ │ ├── LICENSE
│ │ ├── README.md
│ │ └── src
│ │ ├── MessageInterface.php
│ │ ├── RequestInterface.php
│ │ ├── ResponseInterface.php
│ │ ├── ServerRequestInterface.php
│ │ ├── StreamInterface.php
│ │ ├── UploadedFileInterface.php
│ │ └── UriInterface.php
│ └── workerman
│ ├── channel
│ │ ├── composer.json
│ │ ├── README.md
│ │ ├── src
│ │ │ ├── Client.php
│ │ │ ├── Queue.php
│ │ │ └── Server.php
│ │ └── test
│ │ ├── queue.php
│ │ └── server.php
│ ├── globaldata
│ │ ├── composer.json
│ │ ├── README.md
│ │ ├── src
│ │ │ ├── Client.php
│ │ │ └── Server.php
│ │ └── test
│ │ ├── bench.php
│ │ ├── start.php
│ │ └── test.php
│ ├── http-client
│ │ ├── composer.json
│ │ ├── README.md
│ │ └── src
│ │ ├── Client.php
│ │ ├── ConnectionPool.php
│ │ ├── Emitter.php
│ │ ├── Request.php
│ │ └── Response.php
│ ├── psr7
│ │ ├── CHANGELOG.md
│ │ ├── composer.json
│ │ ├── LICENSE
│ │ ├── README.md
│ │ └── src
│ │ ├── AppendStream.php
│ │ ├── BufferStream.php
│ │ ├── CachingStream.php
│ │ ├── DroppingStream.php
│ │ ├── FnStream.php
│ │ ├── functions_include.php
│ │ ├── functions.php
│ │ ├── InflateStream.php
│ │ ├── LazyOpenStream.php
│ │ ├── LimitStream.php
│ │ ├── MessageTrait.php
│ │ ├── MultipartStream.php
│ │ ├── NoSeekStream.php
│ │ ├── PumpStream.php
│ │ ├── Request.php
│ │ ├── Response.php
│ │ ├── Rfc7230.php
│ │ ├── ServerRequest.php
│ │ ├── StreamDecoratorTrait.php
│ │ ├── Stream.php
│ │ ├── StreamWrapper.php
│ │ ├── UploadedFile.php
│ │ ├── UriNormalizer.php
│ │ ├── Uri.php
│ │ └── UriResolver.php
│ ├── workerman
│ │ ├── Autoloader.php
│ │ ├── composer.json
│ │ ├── Connection
│ │ │ ├── AsyncTcpConnection.php
│ │ │ ├── AsyncUdpConnection.php
│ │ │ ├── ConnectionInterface.php
│ │ │ ├── TcpConnection.php
│ │ │ └── UdpConnection.php
│ │ ├── Events
│ │ │ ├── EventInterface.php
│ │ │ ├── Event.php
│ │ │ ├── Ev.php
│ │ │ ├── Libevent.php
│ │ │ ├── React
│ │ │ │ ├── Base.php
│ │ │ │ ├── ExtEventLoop.php
│ │ │ │ ├── ExtLibEventLoop.php
│ │ │ │ └── StreamSelectLoop.php
│ │ │ ├── Select.php
│ │ │ └── Swoole.php
│ │ ├── Lib
│ │ │ ├── Constants.php
│ │ │ └── Timer.php
│ │ ├── MIT-LICENSE.txt
│ │ ├── Protocols
│ │ │ ├── BinaryTransfer.php
│ │ │ ├── Frame.php
│ │ │ ├── Http
│ │ │ │ ├── Chunk.php
│ │ │ │ ├── mime.types
│ │ │ │ ├── Request.php
│ │ │ │ ├── Response.php
│ │ │ │ ├── ServerSentEvents.php
│ │ │ │ ├── Session
│ │ │ │ │ ├── FileSessionHandler.php
│ │ │ │ │ └── RedisSessionHandler.php
│ │ │ │ └── Session.php
│ │ │ ├── Http.php
│ │ │ ├── JsonInt.php
│ │ │ ├── JsonNL.php
│ │ │ ├── ProtocolInterface.php
│ │ │ ├── Text.php
│ │ │ ├── TextTransfer.php
│ │ │ ├── Websocket.php
│ │ │ ├── Ws.php
│ │ │ └── XmlProtocol.php
│ │ ├── README.md
│ │ ├── Timer.php
│ │ └── Worker.php
│ ├── workerman.log
│ └── _www_wwwroot_www.fpm.com_allServer.php.pid
├── websocket.php
└── wsServer.php
27 directories, 141 files
其中這裏的Applications就是它代碼包中的文件夾
然後我們在allServer.php中引用這個FileMonitor文件監控組件的start.php:
allServer.php:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Applications/FileMonitor/start.php';
$channel_server = new Channel\Server('0.0.0.0', 2206);
$worker = new Worker('websocket://0.0.0.0:1234');
$worker->count = 8;
// 全局群组到连接的映射数组
$group_con_map = array();
$worker->onWorkerStart = function(){
// Channel客户端连接到Channel服务端
Channel\Client::connect('127.0.0.1', 2206);
// 监听全局分组发送消息事件
Channel\Client::on('send_to_group', function($event_data){
$group_id = $event_data['group_id'];
$message = $event_data['message'];
global $group_con_map;
var_dump(array_keys($group_con_map));
if (isset($group_con_map[$group_id])) {
foreach ($group_con_map[$group_id] as $con) {
$con->send($message);
}
}
});
};
$worker->onMessage = function(TcpConnection $con, $data){
$config=require_once __DIR__ . '/config.php';
var_dump($config);
// 加入群组消息{"cmd":"add_group", "group_id":"123"}
// 或者 群发消息{"cmd":"send_to_group", "group_id":"123", "message":"这个是消息"}
$data = json_decode($data, true);
var_dump($data);
$cmd = $data['cmd'];
$group_id = $data['group_id'];
switch($cmd) {
// 连接加入群组
case "add_group":
global $group_con_map;
// 将连接加入到对应的群组数组里
$group_con_map[$group_id][$con->id] = $con;
// 记录这个连接加入了哪些群组,方便在onclose的时候清理group_con_map对应群组的数据
$con->group_id = isset($con->group_id) ? $con->group_id : array();
$con->group_id[$group_id] = $group_id;
break;
// 群发消息给群组
case "send_to_group":
// Channel\Client给所有服务器的所有进程广播分组发送消息事件
Channel\Client::publish('send_to_group', array(
'group_id'=>$group_id,
'message'=>$data['message']
));
break;
}
};
// 这里很重要,连接关闭时把连接从全局群组数据中删除,避免内存泄漏
$worker->onClose = function(TcpConnection $con){
global $group_con_map;
// 遍历连接加入的所有群组,从group_con_map删除对应的数据
if (isset($con->group_id)) {
foreach ($con->group_id as $group_id) {
unset($group_con_map[$group_id][$con->id]);
}
if (empty($group_con_map[$group_id])) {
unset($group_con_map[$group_id]);
}
}
};
Worker::runAll();
由於文件workerman這裏是對onxxx之類回調中的include的php文件支持熱重載,那麽這個文件健康組件也是同樣的道理。
所以這裏就以onMessage回調中require一個當前項目根目錄的config.php爲例:
config.php中的内容:
<?php
return ["config_key1"=>"val12366"];
這裏的内容是我最後一次修改之後的。
那麽接下來就是啓動這個allServer.php並在前端每次訪問后我修改下config.php中的内容,直到最後一次修改后的效果如:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php/php.ini allServer.php start
Workerman[allServer.php] start in DEBUG mode
-------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
--------------------------------------------- WORKERS ---------------------------------------------
proto user worker listen processes status
tcp root FileMonitor none 1 [OK]
tcp root ChannelServer frame://0.0.0.0:2206 1 [OK]
tcp root none websocket://0.0.0.0:1234 8 [OK]
---------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
array(1) {
["config_key1"]=>
string(5) "val12"
}
array(2) {
["cmd"]=>
string(9) "add_group"
["group_id"]=>
string(3) "123"
}
bool(true)
array(3) {
["cmd"]=>
string(13) "send_to_group"
["group_id"]=>
string(3) "123"
["message"]=>
string(15) "这个是消息"
}
array(1) {
[0]=>
int(123)
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(1) {
["config_key1"]=>
string(6) "val123"
}
array(2) {
["cmd"]=>
string(9) "add_group"
["group_id"]=>
string(3) "123"
}
bool(true)
array(3) {
["cmd"]=>
string(13) "send_to_group"
["group_id"]=>
string(3) "123"
["message"]=>
string(15) "这个是消息"
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(1) {
[0]=>
int(123)
}
array(0) {
}
array(1) {
["config_key1"]=>
string(8) "val12366"
}
array(2) {
["cmd"]=>
string(9) "add_group"
["group_id"]=>
string(3) "123"
}
bool(true)
array(3) {
["cmd"]=>
string(13) "send_to_group"
["group_id"]=>
string(3) "123"
["message"]=>
string(15) "这个是消息"
}
array(0) {
}
array(1) {
[0]=>
int(123)
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
可以看到文件監控組件已生效。
php語法檢查
有时候在进行网页开发的时候,后台文件的语法错误比较难检查出来,这时候使用php -l filename可对文件的语法进行检查。
實例如:這裏先對上面那個config.php進行語法檢查:
[root@localhost www.fpm.com]# php -l config.php
No syntax errors detected in config.php
自然是沒有問題咯。
儅我將上面config.php文件内容修改如:
<?php
return ["config_key1"=>"val12366"]
然後再次進行語法檢查:
[root@localhost www.fpm.com]# php -l config.php
PHP Parse error: syntax error, unexpected end of file, expecting ';' in config.php on line 2
Parse error: syntax error, unexpected end of file, expecting ';' in config.php on line 2
Errors parsing config.php
這下問題就暴露出來了,是我少寫了一個分號在數組末尾位置。
本文原文參考:https://www.workerman.net/doc/workerman/components/file-monitor.html
关键字词:workerman,FileMonitor,文件監控
相关文章
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelCli
- workerman常用组件-channel分佈式通訊組件-channelSer
- workerman常用组件-channel分佈式通訊組件
- workerman常用组件-GlobalData变量共享组件-GlobalDat
- workerman常用组件-GlobalData变量共享组件-GlobalDat
- workerman常用组件-GlobalData变量共享组件-GlobalDat