html — PHP-сокеты загружают информацию в браузер

Привет, ребята, я пытаюсь загрузить некоторую информацию в браузер. У меня есть этот код:

<?php
// allow the daemon to run without being timed out
set_time_limit(0);

$ip = "192.168.0.101";
$port = 123;

// set the protocols
if( !$socket = socket_create(AF_INET,SOCK_STREAM,0) ){
showError();
}

echo "The socket's protocol info was set \n";

// bind the socket
if( !socket_bind($socket,$ip,$port) ){
showError();
}
echo "The socket has been bound to a specific port now ! \n";

// start listening on this port
if( !socket_listen($socket) ){
showError();
}
echo "Now listening for connections @ @ @ \n";

$client = socket_accept($socket);
echo "new connection with client established !! \n";
do {
// welcome the user
if(!$clientMsg = socket_read($client, 2048, PHP_NORMAL_READ)) {
echo "Error occured while receiving message!\n";
}

if(!$clientMsg = trim($clientMsg)) {
continue;
}

$msg = "Thank you for your message!\n";
socket_write($client, $msg, strlen($msg));

echo "====================================================\n";
echo "Message was received successfully!\n";
echo $clientMsg."\n";
echo "====================================================\n";

if ($clientMsg == 'close') {
//socket_close($client);
break 2;
}
} while (true);
/*
// check for any message sent by the user
do{
if( ! $clientMssg = socket_read($client,2048,PHP_NORMAL_READ) ){
showError();
}

// say something back
$messageForUser = "Thanks for your input. Will think about it.";
socket_write($client,$messageForUser,strlen($messageForUser));

// was it actually words?
if( !$clientMssg = trim($clientMssg) ){
continue;
}
if( $clientMssg == 'close' ){
// close their connection as requested
socket_close($client);
echo "\n\n-------------------------------- \n" .
"The user has left the connection\n";
// break out of the loop
break 2;
}

}while(true); */
// end the socket
echo "Ending the socket \n";
socket_close($socket);

// show error details
function showError( $theSocket = null ){
$errorcode = socket_last_error($theSocket);
$errormsg = socket_strerror($errorcode);

die("Couldn't create socket: [$errorcode] $errormsg");
}

?>

На стороне сервера, но я не знаю, как я могу сделать это видимым в браузере. Вы можете мне помочь?
Я пытаюсь сделать простую систему чата только для понимания основ и надеюсь перевести на более сложную манипуляцию данными для игр, живую статистику и так далее …

С уважением!

0

Решение

Вы можете использовать JavaScript и WebSockets для отправки запросов к вашей программе PHP-сервера и получения ответов. это хороший учебник, использующий PHP и WebSockets для чата.

2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector