workman官网地址:高性能PHP应用容器 workerman
1:把workman项目放到linux服务器后,需要启动你的php文件,才可以使用
定位到项目根目录:例:cd /mnt/workman
启动代码:php outin.php start -d
停止代码:php outin.php stop
以debug(调试)方式启动: php outin.php start
2:workman服务端写法
uidConnetions = [];
$worker->onConnect = function($connection) use ($worker)
{
$connection->onWebSocketConnect = function($connection , $httpBuffer) use ($worker)
{
// 可以在这里判断连接来源是否合法,不合法就关掉连接
// $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket连接
// if($_SERVER['HTTP_ORIGIN'] != 'https://www.workerman.net')
// {
// $connection->close();
// }
// onWebSocketConnect 里面$_GET $_SERVER是可用的
// var_dump($_GET, $_SERVER);
//前端传入的参数,可以根据type,company_uuid判断需要显示的链接有哪些
$worker->uidConnetions[$connection->id]["connection"] = $connection;
$worker->uidConnetions[$connection->id]["type"] = $_GET["type"];
$worker->uidConnetions[$connection->id]["company_uuid"] = $_GET["company_uuid"];
};
};
$worker->onClose = function ($connection) use ($worker)
{
if (isset($connection->id)) {
unset($worker->uidConnetions[$connection->id]);
}
};
// 进程启动后定时推送数据给客户端
$worker->onWorkerStart = function($worker){
//查表,有需要的,
// file_put_contents("/mnt/workman/log/" .date("Ymd"). ".txt", date("Y-m-d H:i:s") . ":jin" .PHP_EOL, FILE_APPEND);
$db = new Connection('mysql地址','3306', 'root', 'password', '表名'); //mysql链接信息
$attend = new Attendance();
Timer::add(1, function()use($worker, $db, $attend){
//10s之内的数据
$time = time() - 10;
$time = date("Y-m-d H:i:s",$time);
//$time = date("Y-m-d 00:00:00",$time);
$infos = $db->query("SELECT student_userid,class_name,student_name,sign_time,id,company_uuid,status,photo_url FROM sign WHERE tui = '1' AND is_delete = '0' AND sign_time > '$time' GROUP BY student_userid ORDER BY id desc");
//获取信息
$attend_data = $attend->getAttendanceData($db);
foreach($worker->connections as $connection) {
$res = $worker->uidConnetions[$connection->id];
file_put_contents("/mnt/workman/log/" .date("Ymd"). ".txt", date("Y-m-d H:i:s") . "type1:".$res["type"] .PHP_EOL, FILE_APPEND);
//outin类型的,使用本回调
if ($res["type"] == 'outin') {
foreach ($infos as $v) {
$company_uuid = $v["company_uuid"];
$student_userid = $v["student_userid"];
$imgurl = $db->single("SELECT imgurl FROM stu_face WHERE company_uuid = '$company_uuid' AND student_userid = '$student_userid'");
if ($v["company_uuid"] == $res["company_uuid"]) {
$arr = [
"class_name"=>$v["class_name"],
"student_name"=>$v["student_name"],
"status"=>$v["status"],
"photo_url"=>$imgurl,
"sign_time"=>$v["sign_time"]
];
//打印输出查询的数据
file_put_contents("/mnt/workman/log/" .date("Ymd"). ".txt", date("Y-m-d H:i:s") . "xinxi:".json_encode($arr, true) .PHP_EOL, FILE_APPEND);
//根据数据为已推送
$db->query("UPDATE sign SET tui = '2' WHERE tui = '1' AND company_uuid = '$company_uuid' AND student_userid = '$student_userid'");
$connection->send(json_encode($arr));
}
}
}
if ($res["type"] == 'banji') {
$attend->banji($res, $attend_data["ban_infos"],$db,$connection);
}
}
});
};
Worker::runAll();
2:自动加载问题,参考:自动加载-workerman手册
注:1:文件名是小写的,在使用命名空间的时候,也是使用小写
2:在需要使用的时候,引入是这样的: