Periodically sending messages to clients in Ratchet

放肆的年华 提交于 2019-12-01 14:29:56

$server isn't defined in the function scope and variables from the parent scope don't get inherited to the child scope by default. Closures can inherit variables from the parent scope by using the use language construct.

$server->loop->addPeriodicTimer(5, function () use ($server) {        
    foreach ($server->app->clients as $client) {                  
            $client->send("hello client");          
    }
});

More information about anonymous functions (closures): https://secure.php.net/manual/en/functions.anonymous.php
More information about variables scope: https://secure.php.net/manual/en/language.variables.scope.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!