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

时间:2023-03-08 20:19:45

服务端

$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[];
$str= $arr[]; //一对一推送
$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);
}*/
});