TypeError: Argument 2 passed to {closure}() must be an instance of Workerman\Protocols\Http\Request, string given, called in /www/wwwroot/tp61/vendor/workerman/workerman/Connection/TcpConnection.php on line 638 and defined in /www/wwwroot/tp61/workermanstudy/ws_test.php:35
第35行是下面这个调用 看了好久没看出有问题呀
$ws_worker->onMessage = function(TcpConnection $connection,Request $request)
一开始也use了
use Workerman\Protocols\Http\Request;
还是不行
bai tuo ni ba dai ma tie quan hao bu hao?
全部代码
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/../vendor/autoload.php';
// 注意:这里与上个例子不同,使用的是websocket协议
$ws_worker = new Worker("websocket://0.0.0.0:2000");
$ws_worker->onWorkerStart = function($ws_worker)
{
// 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
global $db;
$db = new \Workerman\MySQL\Connection('localhost', '3306', 'tp6', 'ERFYFsaLKSa62dKc', 'tp6');
};
// 启动4个进程对外提供服务
$ws_worker->count = 1;
$ws_worker->onConnect = function(TcpConnection $connection)
{
$connection->id='进程id'.rand(10,999).$connection->id;
$data = array(
'user'=>'链接成功',
'message'=>$connection->id
);
$connection->send(json_encode($data));
};
// 当收到客户端发来的数据后返回hello $data给客户端
$ws_worker->onMessage = function(TcpConnection $connection, Request $request)
{
global $db;
$all_tables = $db->query("select * from
think_message_list
");};
// 运行worker
Worker::runAll();
websocket协议onMessage第二个参数是字符串,不是Request对象。按照手册应该是这样
官网的例子就是这样写的https://www.workerman.net/doc/workerman/http/request.html
官网的是http协议,你的是websocket协议
恍然大明白 谢谢大佬