簡體   English   中英

無法將消息發送到連接到服務器的所有客戶端(PHP套接字)

[英]Cannot send message to all client that connected to the server (PHP Socket)

我正在嘗試向連接到服務器的所有客戶端發送消息。 我一直在循環中檢查會話代碼,並且當會話代碼更改時,循環不會停止。 感謝對此有任何解決方案。 非常感謝。

這是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... \n";
ob_flush();
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");

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

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

while(true){

$spawn[++$i] = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "\n";
$input = socket_read($spawn[$i], 1024) or die("Could not read input\n");
if($input=='q'){break;}
$input = trim($input);
echo $input ."\n";
ob_flush();
$output = "hello client";
socket_write($spawn[$i], $output, strlen ($output)) or die("Could not write output\n");
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\n");
}
    break;
}

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




</body>
</html>

這是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\n");

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

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

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


socket_close($socket);


}


?>

</body>

</html>

這是change_session.php

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

看來您已將循環設計為永不停止。 我看到循環中有一個條件執行語句。

似乎您正在設置一個套接字服務器,先連接到該服務器,然后再寫入它,但這一切都在client.php中。 因此,這將在提交時執行,然后丟棄。

我在設計要進行套接字通信的地方的想法是擁有一台服務器,該服務器設置套接字,並持續偵聽任何新的客戶端消息,然后執行一些操作。

似乎您正在使用基於無狀態Web的AJAX模式。 重新考慮只采用一種通信形式的標准化也許是值得的。 AJAX是基於無狀態HTTP的瀏覽器與服務器通信的方法,但是服務器也可以使用HTTP請求與您的終結點通信。 如果您不需要流式傳輸信息或維持持久連接,則TCP套接字可能不是最佳協議,並且可能會令人困惑。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM