나는 PHP
채팅을 만들려고 해요에서 채팅 클라이언트를 작성, 그래서 나는 client
연결을들을 수있는 터미널 서버, 시작 server.php
있습니다는 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();
?>
기능 client()
를 내가 client.php
파일에서 가지고있는 코드입니다. 터미널에서 실행할 때 server.php
에서 메시지를 보내고받을 수 있습니다. 이제 내 main.php
파일을 사용하고 싶습니다. 사용자가 로그인하면 서버에 메시지를 보내면 사용자가 보지 못한 메시지가 회신됩니다. 두 개의 다른 터미널에서 server.php
과 client.php
을 실행하면 메시지를 보내고받을 수 있지만 main.php
을 사용하여 해당 응답 메시지를 JSON
개체로 변환하고 html
페이지로 다시 전송합니다. textarea
상자에 추가하십시오. 내 문제는 무엇입니까 : client.php
이받은 답변을 html 페이지로 다시 보내려면 어떻게해야합니까? 내가 터미널에 그것을 실행하면 , 내가 가진 :
Enter a message to send : hello
Reply : hello
내가 채팅에서 사용자 입력을 보낼 AJAX
를 사용, 그래서 나는 그 메시지를 취할 수, 서버에 보내 싶어하는 터미널에서 시작하여 답장을 웹 페이지로 되돌리고 앞으로 가져 와서 텍스트 상자 영역에 추가합니다. 어떻게해야합니까? main.php
을 통해 서비스로 client.php
을 시작해야합니까? 또는 client($input)
함수를 사용하여 메시지를 보낸 다음 보낸 메시지를 반환해야합니까? 그러나 다른 클라이언트가 채팅에 연결할 수 있기 때문에 사용이 로그 아웃 될 때까지 client
이 실행되기를 원합니다. 어떻게 저를 위해 일종의 퍼지 일 수 있습니다 달성 할 수 있습니다. client($input)
의 코드는 client.php
과 동일합니다.
http://stackoverflow.com/questions/2055020/php-chat-client – sdolgy
또한 PHP는 한 요청보다 오래 실행되도록 설계되지 않았습니다. 오래 걸릴 수 있습니다 (채팅 봇을 오래 전에 했었습니다).하지만 매우주의하지 않으면 메모리 누수와 충돌 위험이 높습니다. 다른 언어는 상주 앱에 더 적합 할 수 있습니다. – ToBe