阡陌博客 - workman
type: channel
link: https://www.kinmor.com/tag/workman/
items: thinkphp5.0安装workman

thinkphp5.0安装workman
type: item
date: 2022-08-23T15:21:00+08:00
description: composer安装composer require topthink/think-worker=1.0.* workman服务需要PHP开启某些函数//使用workerman需要解除以下函数的禁用 stream_socket_server stream_socket_client pcntl_signal_dispatch pcntl_signal pcntl_alarm pcntl_fork posix_getuid posix_getpwuid posix_kill posix_setsid posix_getpid posix_getpwnam posix_getgrnam posix_getgid posix_setgid posix_initgroups posix_setuid posix_isatty 在项目根目录创建server.php代码如下<?php define('APP_PATH', __DIR__ . '/application/'); define('BIND_MODULE','push/Worker'); require __DIR__ . '/thinkphp/start.php'; *创建模块应答application|push|controller|Worker.php代码如下<?php namespace app\push\controller; use think\worker\Server; use think\Log; use think\Controller; class Worker extends Server { protected $socket = 'websocket://0.0.0.0:2346'; /** * 收到信息 * @param $connection * @param $data */ public function onMessage($connection, $data) { $params =$data; $data = json_decode($data,true); if ($data['sign']){ $connection->send($data['sign']); }else{ $return_data ="我于".date("Y-m-d H:i:s")."收到您的信息:".$params; $connection->send($return_data); } } /** * 当连接建立时触发的回调函数 * @param $connection */ public function onConnect($connection) { } /** * 当连接断开时触发的回调函数 * @param $connection */ public function onClose($connection) { } /** * 当客户端的连接上发生错误时触发 * @param $connection * @param $code * @param $msg */ public function onError($connection, $code, $msg) { echo "error $code $msg\n"; } /** * 每个进程启动 * @param $worker */ public function onWorkerStart($worker) { Log::write('进程启动了:'.date('Y-m-d H:i:s'),'info'); } public function index(){ parent::start(); } } 启动php服务进程php server.php start #启动 |stop 停止|restart重启 Nginx配置反向代理在server内添加location /wss { proxy_pass http://127.0.0.1:2346/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 5000M; } 客户端访问代码我本地创建了一个8.html文件…