PHP套接字将信息加载到浏览器中

时间:2015-01-07 17:09:11

标签: php html sockets browser visualization

大家好我试图将一些信息加载到浏览器中。我有这段代码:

<?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");
    }

?>

在服务器端,但我不知道如何在浏览器中显示它。你能帮助我吗? 我试图制作一个简单的聊天系统只是为了理解基础知识,我希望转化为一些更复杂的数据操作,用于游戏,实时统计等等......

问候!

1 个答案:

答案 0 :(得分:2)

您可以使用JavaScript和WebSockets将请求发送到PHP服务器程序并接收响应。 This是一个使用PHP和WebSockets进行聊天的好教程。