私人领地

swoole推送信息一对一,一对多

服务端

$ws->on('message', function ($ws, $frame) {
    //echo "接收到的信息: {$frame->data}\n";
    //$ws->push($frame->fd, "server: {$frame->data}");
    //echo "服务器已接收:【".$frame->fd."】";
    //$ws->push($frame->fd, json_encode(['hello','world'.$frame->data]));


    // 1.客户端发送过来的信息
    $content = $frame->data;
    echo "服务器接收到信息:".$content."\n";
    // 2.讲消息发送个所有客户端
    $arr = json_decode($content);
    $id = $arr[0];
    $str= $arr[1];

    //一对一推送
    $ws->push($id,$str);
    // 一对多,推送
    /*foreach ($ws->connections as $fd){
        //echo "FD:".$fd."\n";
        $arr = json_decode($content);
        $id = $arr[0];
        $str= $arr[1];
        $ws->push($fd,$str);
    }*/
});

 

posted @ 2019-03-13 18:21  狂奔的蜗牛Snails  阅读(777)  评论(0编辑  收藏  举报