config/worker_server.php
<?php
return [
'worker_class' => 'app\home\websocket\Worker',
];
app/home/websocket/Worker.php
<?php
namespace app\home\websocket;
use Protocols\Http\Request;
use Protocols\Http\Response;
use think\worker\Server;
use Workerman\Connection\TcpConnection;
class Worker extends Server{
protected $socket = 'Httpd://0.0.0.0:2346';
protected $option = [
'count' => 4,
'name' => 'Worker',
];
public function onMessage(TcpConnection $connection,Request $request){
$connection->send(json_encode($request),true);
}
写在哪个位置呢?我尝试过写在app/home/websocket/Worker.php里,他提示报错。[think\exception\ErrorException]
Constant expression contains invalid operations 。它又不能加到config/worker_server.php里,tp手册上面写着:然后在worker_server.php中增加配置参数:
return [
'worker_class' => 'app\http\Worker',
];
定义该参数后,其它配置参数均不再有效。
去看云手册看看
自定义类一般都继承了他的 Server 类,在 init 函数里面写看看