在哪规定了回调函数中的参数?

edgexie

我在测试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这一个参数?手册上有说明吗?

4088 3 0
3个回答

walkor 打赏

workerman框架就是这么设计的,手册中onConnect部分有说明,参数是$connection。
你可以利用闭包在onConnect中传递外部定义的变量。类似下面这样

$ws_worker->onConnect   =   function($connection)use($ws_worker) { ...
  • 暂无评论
edgexie

谢谢

  • 暂无评论
walkor 打赏

不客气

  • 暂无评论
年代过于久远,无法发表回答
×
🔝