Sto cercando di creare una chat PHP
, quindi devo server.php
che avvia il server sul terminale, che è ascoltare client
connessioni:Creazione di un client per una chiacchierata in PHP
<?php
function chat_leave($sock, $chat_id = 0)
{
if($chat_room_id[ $chat_id ])
{
unset($chat_room_id[ $chat_id ]);
return true;
}
socket_close($sock);
return false;
}
function client($input)
{
/*
Simple php udp socket client
*/
//Reduce errors
error_reporting(~E_WARNING);
$server = '127.0.0.1';
$port = 9999;
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
//Communication loop
while(1)
{
//Send the message to the server
if(! socket_sendto($sock, $input , strlen($input) , 0 , $server , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
//Now receive reply from server and print it
if(socket_recv ($sock , $reply , 2045 , MSG_WAITALL) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
return $reply;
}
}
/*
* chat_join
* a new user joins the chat
* @username: String
* @password: String
*
* add a new listener to the server
*
*/
function chat_join($username = "", $password = "")
{
$users = array(
"batman" => "batman123",
"robin" => "robin123",
"joe" => "joe123"
);
if($users[$username] == $password)
{
return true;
}
return false;
}
function main()
{
$chat_room_id = array();
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
$action = stripslashes($_POST['action']);
$port = intval($_POST['port']);
$domain = stripslashes($_POST['domain']);
$chat_id = intval($_POST['chat_room_id']);
if(strcmp($action, "login") == 0)
{
$status = chat_join($username, $password);
if($status)
{
$chat_room_id[] = $chat_id;
echo json_encode($status);
}
}
else if(strcmp($action, "chat") == 0)
{
$msg = stripslashes($_POST['message']);
// take the message, send through the client
$reply = client($msg);
echo json_encode($reply);
}
else if(strcmp($action, "logout") == 0)
{
}
else
{
echo json_encode(false);
}
return;
}
main();
?>
La funzione client()
è il codice che ho da un file client.php
, che quando eseguo sul terminale, è in grado di inviare e ricevere messaggi dallo server.php
. Ora vorrei usare il mio file main.php
, quindi una volta che l'utente ha effettuato l'accesso invierà messaggi al server, che risponderà ai messaggi che l'utente non ha visto. Quando eseguo server.php
e client.php
da due terminali diversi, sono in grado di inviare e ricevere messaggi, tuttavia vorrei farlo utilizzando main.php
, trasformare il messaggio di risposta in un oggetto JSON
e inviarlo alla pagina html
dove verrà viene aggiunto a una casella textarea
. Il mio problema è: come posso ottenere la risposta che ha ricevuto client.php
e rispedirla alla pagina html? Quando eseguire sul terminale, ho:
Enter a message to send : hello
Reply : hello
Io uso AJAX
per inviare l'input utente nella chat, così ho voluto essere in grado di prendere quel messaggio, e inviarlo al server, che Ho iniziato sul terminale e ho risposto e inoltrato alla pagina web e l'ho aggiunta all'area della casella di testo. Come posso farlo? Devo avviare client.php
come servizio tramite main.php
? O dovrei usare la funzione client($input)
per inviare un messaggio e quindi restituire ciò che invia, indietro? Tuttavia, volevo che client
fosse in esecuzione fino a quando l'utilizzo si disconnette, poiché altri client potrebbero connettersi alla chat. Come posso ottenere questo è un po 'sfocato per me. Il codice in client($input)
è lo stesso di client.php
.
http://stackoverflow.com/questions/2055020/php-chat-client – sdolgy
Inoltre, tenere presente che PHP non è progettato per essere eseguito più di una richiesta. È possibile eseguire più a lungo (ha fatto un bot di chat qualche tempo fa) ma si ha un alto rischio di perdite di memoria e crash se non si è molto molto attenti. Altre lingue potrebbero essere più adatte per le app residenti. – ToBe