PHP 套接字将信息加载到浏览器中
PHP Sockets loading information into browser
大家好,我正在尝试将一些信息加载到浏览器中。我有这个代码:
<?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");
}
?>
在服务器端,但我不知道如何让它在浏览器中可见。你能帮助我吗?
我正在尝试制作一个简单的聊天系统,只是为了了解基础知识,我希望转化为一些更复杂的游戏数据处理,实时统计等等......
此致!
您可以使用 JavaScript 和 WebSockets 向 PHP 服务器程序发送请求并接收响应。 This 是使用 PHP 和 WebSockets 聊天的好教程。
大家好,我正在尝试将一些信息加载到浏览器中。我有这个代码:
<?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");
}
?>
在服务器端,但我不知道如何让它在浏览器中可见。你能帮助我吗? 我正在尝试制作一个简单的聊天系统,只是为了了解基础知识,我希望转化为一些更复杂的游戏数据处理,实时统计等等......
此致!
您可以使用 JavaScript 和 WebSockets 向 PHP 服务器程序发送请求并接收响应。 This 是使用 PHP 和 WebSockets 聊天的好教程。