我在测试onConnect回调函数的时候遇到如下问题.
1.服务器端代码:
<?php
use Workerman\Worker;
require_once './Workerman/Autoloader.php';
// 创建一个Worker监听2345端口,使用websocket协议通讯
$ws_worker = new Worker("websocket://0.0.0.0:2345");
$ws_worker->onConnect = function($connection,$ws_worker){
echo $connection->id."\n";
echo $ws_worker->id."\n";
};
2.成功开启worker服务;
遇到报错
PHP Warning: Missing argument 2 for {closure}() in /test.php on line 7
Warning: Missing argument 2 for {closure}() in /test.php on line 7
1
PHP Notice: Undefined variable: ws_worker in /test.php on line 9
Notice: Undefined variable: ws_worker in /test.php on line 9
PHP Notice: Trying to get property of non-object in /test.php on line 9
Notice: Trying to get property of non-object in /test.php on line 9
问题: 在哪定义的onConnect的function()只能传$connection这一个参数?手册上有说明吗?
workerman框架就是这么设计的,手册中onConnect部分有说明,参数是$connection。
你可以利用闭包在onConnect中传递外部定义的变量。类似下面这样
谢谢
不客气