服務端json
$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); }*/ });