socket.io 如何根据id判断用户是否下线了?

伊伦哲罗
public static $online = [];
    public function index()
    {
        $context = array(
            'ssl' => array(
                'local_cert'  => $_SERVER['DOCUMENT_ROOT'].'\lx.diguadou.com.cer',
                'local_pk'    => $_SERVER['DOCUMENT_ROOT'].'\lx.diguadou.com.key',
                'verify_peer' => false,
            )
        );
      // $io = new SocketIO(8282,$context);//socket的端口
        $io = new SocketIO(8282);//socket的端口
        $io->on('workerStart', function () use ($io) {
            $inner_http_worker = new Worker('http://0.0.0.0:5880');
            $inner_http_worker->onMessage = function ($http_connection, $data) use ($io) {
                // isset(static::$online[$data->post('uid')]);
                $adc["code"]=200;
                $adc["message"]='成功';
                $adc['uid']=$data->post('uid');//要推送的UID 但是不知道怎么送出去
                $adc['data']=json_decode($data->post('msg'),true);
                //$io->to($data->post('uid'))->emit('pusha', $adc);//这个试了不起效果
                $io->emit('pusha',$adc);//只能这样群发了 前端根据用户的ID 从推送的数据里捡起属于自己的
                if(!$data->post('msg')) {
                    $rb=input('param.');
                    $rc=input('');
                    $http_connection->send('fail, $_GET["msg"] not found!!rb:');
                }
                $http_connection->send('ok');
            };
            $inner_http_worker->listen();
        });
        // 当有客户端连接时
         $io->on('connection', function($socket) {
            // 定义chat message事件回调函数
            $socket->on('init', function ($data) use($socket) {
                //这里判断用户是否在数组里结果也没起效 控制台里依旧显示多了一个ID
                //前端页面刷新就多一个
                $data = json_decode($data, true);
                $socket->uid = $data['uid'];
                static::$online[$data['uid']] =$socket;
                $socket->emit('init', 'login listen success', function ($res){});
            });
            $socket->on('push', function ($data) use($socket) {
                $socket->emit('push', '这是push7788', function ($res){});
                // $io->emit('find message from server',$data);
            });
            $socket->on('disconnect', function($socket){
                static::$online[$socket->uid]
             });
        });
        Worker::runAll();
    }

链接的时候 记录了用户的id

用户前端 执行了

this.$socket.close() //这是JS

断开了链接

如何在PHP里把用户的ID消除掉呢? 如何判断$online 有没有这个 ID呢?

里面的注解的问题 折腾一星期好像也没有多大效果

3145 2 0
2个回答

six
$socket->on('disconnect', function($socket){
   static::$online[$socket->id]
}); 

连接断开的时候消除id。

用isset(static::$online[$id]) 来判断是否在线

  • 暂无评论
伊伦哲罗
    public static $online = [];
    public function index()
    {
        $context = array(
            'ssl' => array(
                'local_cert'  => $_SERVER['DOCUMENT_ROOT'].'\lx.diguadou.com.cer',
                'local_pk'    => $_SERVER['DOCUMENT_ROOT'].'\lx.diguadou.com.key',
                'verify_peer' => false,
            )
        );
      // $io = new SocketIO(8282,$context);//socket的端口
        $io = new SocketIO(8282);//socket的端口
        $io->on('workerStart', function () use ($io) {
            $inner_http_worker = new Worker('http://0.0.0.0:5880');
            $inner_http_worker->onMessage = function ($http_connection, $data) use ($io) {

                // isset(static::$online[$data->post('uid')]);

                $adc["code"]=200;
                $adc["message"]='成功';
                $adc['uid']=$data->post('uid');//要推送的UID 但是不知道怎么送出去
                $adc['data']=json_decode($data->post('msg'),true);
                //$io->to($data->post('uid'))->emit('pusha', $adc);//这个试了不起效果
                $io->emit('pusha',$adc);//只能这样群发了 前端根据用户的ID 从推送的数据里捡起属于自己的
                if(!$data->post('msg')) {
                    $rb=input('param.');
                    $rc=input('');
                    $http_connection->send('fail, $_GET["msg"] not found!!rb:');
                }
                $http_connection->send('ok');
            };
            $inner_http_worker->listen();
        });
        // 当有客户端连接时
         $io->on('connection', function($socket) {
            // 定义chat message事件回调函数
            $socket->on('init', function ($data) use($socket) {
                //这里判断用户是否在数组里结果也没起效 控制台里依旧显示多了一个ID
                //前端页面刷新就多一个
                $data = json_decode($data, true);
                $socket->uid = $data['uid'];
                static::$online[$data['uid']] =$socket;
                $socket->emit('init', 'login listen success', function ($res){});
            });
            $socket->on('push', function ($data) use($socket) {
                $socket->emit('push', '这是push7788', function ($res){});
                // $io->emit('find message from server',$data);
            });
            $socket->on('disconnect', function($socket){
                static::$online[$socket->uid]
             });
        });
        Worker::runAll();
    }

这是我写的大部分代码 感觉问题特别多

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