1

下面是我遍历发送消息,总觉得这样做是不对的,但是看手册又找不到发送所有成员的函数,求助

$server = new swoole_websocket_server("0.0.0.0", 9502);
  $server->set(array(
    //'worker_num' => 4,   
    'daemonize'=>1
));  
$server->on('open', function (swoole_websocket_server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
    $_SESSION['group'][$request->fd]=$request->fd;
    
});

$server->on('message', function (swoole_websocket_server $server, $frame) {
    
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $arr=array_unique($_SESSION['group']); 
    $data=json_decode($frame->data,true);
   
    if (is_array($arr)){
        foreach ($arr as $key=>$row){
              
              $server->push($row, json_encode(array('name'=>$data['name'],'type'=>$data['type'],'info'=>$data['info']))); 
        }                  
    }else{   
        $server->push($frame->fd, "this is server");
    }
   
                 
});
     
$server->on('close', function ($ser, $fd) {
    unset($_SESSION['group'][$fd]);
    echo "client {$fd} closed\n";
});

$server->start();
tang_cc 299
2016-07-02 提问
1 个回答
0

已采纳
foreach($serv->connections as $fd)
{
    $serv->push($fd, "hello\n");
}

撰写答案

推广链接