无法向连接到服务器的所有客户端发送消息(PHP Socket)

I am trying to send message to all client that connected to the server. i keep checking the session code in the loop and when the session code changed, the loop wont stop. Appreciated if there is any solutions on this. Thanks a lot.

here is server.php

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Server</title>
<link rel="stylesheet" href="../bootstrap-3.3.7/docs/dist/css/bootstrap.min.css">
<script src="../bootstrap-3.3.7/docs/assets/js/vendor/jquery.min.js"></script>
<script src="../bootstrap-3.3.7/docs/dist/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Server</h1>

<button id="start_button" class="btn btn-primary" >Start Quiz</button>

<script type="text/javascript">

document.getElementById("start_button").addEventListener("click", write_to_all);

function write_to_all(){
$.ajax({
            type: "GET",
            url: "change_session.php" ,

        });
}
</script>

<?php
$_SESSION['pass_start_quiz'] = "no";

$i=0;
$spawn = array();

error_reporting(0);
set_time_limit(0);
ob_implicit_flush(true);
$host = "127.0.0.1";
$port = 25011;
$start="no";

echo "Waiting for connections... 
";
ob_flush();
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");

$result = socket_bind($socket, $host, $port) or die("Could not bind to socket
");

$result = socket_listen($socket) or die("Could not set up socket listener
");

while(true){

$spawn[++$i] = socket_accept($socket) or die("Could not accept incoming connection
");
echo "
";
$input = socket_read($spawn[$i], 1024) or die("Could not read input
");
if($input=='q'){break;}
$input = trim($input);
echo $input ."
";
ob_flush();
$output = "hello client";
socket_write($spawn[$i], $output, strlen ($output)) or die("Could not write output
");
ob_flush();
$start_quiz= $_SESSION["pass_start_quiz"];
echo $start_quiz;

if($start_quiz == "yes"){
    echo $start_quiz;
    $output = "start";
    $arrlength = count($spawn);
    echo $arrlength;
    ob_flush();
    for($x = 0; $x < $arrlength; $x++) {
    socket_write($spawn[$x], $output, strlen ($output)) or die("Could not write output
");
}
    break;
}

}
socket_close($spawn);
socket_close($socket);
?>




</body>
</html>

here is client.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Client</title>
<link rel="stylesheet" href="../bootstrap-3.3.7/docs/dist/css/bootstrap.min.css">
<script src="../bootstrap-3.3.7/docs/assets/js/vendor/jquery.min.js"></script>
<script src="../bootstrap-3.3.7/docs/dist/js/bootstrap.min.js"></script>
</head>
<body>
<form method="post" action="client.php">
<p><h4><label>Type Your Message Here:<input name = "message" size = "25" maxlength = "30" required></label></h4></p>
<input type="submit" name="sendmsg" class="btn btn-primary" value="send message"/>
</form>
<?php
ob_implicit_flush(true);
$user="abc";
if(empty($_POST)){

}
elseif(isset($_POST['sendmsg'])) {
$message =$_POST["message"];


$host    = "127.0.0.1";
$port    = 25011;

echo "Message To server :".$message;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket
");

$result = socket_connect($socket, $host, $port) or die("Could not connect to server
");  

socket_write($socket, $message, strlen($message)) or die("Could not send data to server
");

while(true){
$result = socket_read ($socket, 1024) or die("Could not read server response
");
echo "Reply From Server  :".$result;
ob_flush();
if($result == "start"){
echo "Quiz Started";
ob_flush();
break;
}
}


socket_close($socket);


}


?>

</body>

</html>

This is change_session.php

<?php
session_start();
$_SESSION['pass_start_quiz'] = "yes";
?>

It looks like you've designed your loop to never stop. I see there is a conditional execution statement within the loop however.

It seems like you are setting up a socket server, connecting to it and then writing to it, but all within client.php. So this would be executed on-submit, and then discarded.

My thinking on designing something where you want socket communication to take place would be to have a server which sets up the socket, and listens persistently for any new client messages, and then performs some action.

It also appears you are using a stateless web based AJAX pattern. Perhaps a re-think to standardize on only one form of communication would be worthwhile. AJAX is a stateless HTTP based method for browsers to communicate with servers, but servers can also talk to your endpoints using HTTP requests. If you don't need to be streaming information or maintaining a persistent connection, TCP sockets might not be the best protocol, and might be confusing matters.

Hope this helps!