Call to a member function emit() on null??是版本的问题么?

zjbcd

在thinkphp 5 里如何使用 SocketIO 呢? Workerman version:4.0.6
主要想实现电脑端客服在线长连接,手机端用户发表问题,存入数据库后会分配一个客服ID,这个客服ID,在客服登录的时候已经和后端握手了,发送的时候发送的时候会提示 Call to a member function emit() on null,不知道如何记录客服的ID,判断客服是不是在线对指定客服进行消息推送,可是推送会报错 Call to a member function emit() on null?

$inner_http_worker->onMessage = function ($http_connection, $data) use ( $sender_io) {
                global $uidConnectionMap;
                global $socket;
                global $sender_io;
                $to = $data->post('to');
                $res=json_decode($data->post('content'),true);
                // 有指定uid则向uid所在socket组发送数据
                if($to){
                    $sender_io->to($to)->emit('kline', $res);
                // 否则向所有uid推送数据
                }else{
                    $sender_io->emit('kline', $res);
                }
                if(!$data->post('msg')) {
                    $rb=input('param.');
                    $rc=input('');
                    $http_connection->send('fail, $_GET["msg"] not found!!rb:');
                }
                $http_connection->send('ok'); 
            };

我是按照文档来的,部分参考了一下论坛的其他帖子,但是报错!Call to a member function emit() on null ?? chuxian出现问题的地方在 $sender_io->to($to)->emit('kline', $res);附近

1713 1 0
1个回答

six

use ( $sender_io) 了就不用 写 global $sender_io;了,把global $sender_io;去掉试下,估计你全局变量里没有 $sender_io 这个变量

  • zjbcd 2020-08-09

    好的,谢谢,已经解决

年代过于久远,无法发表回答
🔝