php - 在 PHP 中创建用于聊天的客户端

标签 php html ajax json

我正在尝试创建一个 PHP 聊天,所以我有 server.php 在终端上启动服务器,它是 listen to client 连接:

<?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.phpclient.php 时,我能够发送和接收消息,但是我想使用 main.php,将该回复消息转换为 JSON 对象并发送回 html 页面,在那里它将附加到 textarea框。 我的问题是:如何获得 client.php 收到的回复并将其发送回 html 页面? 当我在终端上执行它时,我有:

Enter a message to send : hello
Reply : hello

我使用 AJAX 在聊天中发送用户输入,所以我希望能够获取该消息,并将其发送到服务器,我在终端上启动并接收回复来回网页并将其附加到文本框区域。 我怎样才能做到这一点?我应该通过 main.phpclient.php 作为服务启动吗?或者我应该使用 client($input) 函数发送消息然后返回它发送的内容? 但是,我希望 client 一直运行直到使用注销,因为其他客户端可能会连接到聊天。我怎样才能做到这一点对我来说有点模糊。 client( $input ) 中的代码与 client.php 中的代码相同。

最佳答案

抱歉跑题了,但如果可以的话,最好使用 XMPP 就绪解决方案,例如带有 http-bind 模块的 ejabberd 服务器。当然,这种解决方案有一些缺点,但缺点更大。看看这个解决方案,也许它会以低成本解决您的问题。

参见 related answer with brief desc on XMPP solution

关于php - 在 PHP 中创建用于聊天的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902684/

相关文章:

php - 选择多个选项中的很少条目在数据库中为空

php - 当我在 Eclipse 中调试 PHP 脚本时,它不会加载 mysql 扩展

html - 将一个元素设置在另一个元素旁边

javascript - 动态构建服务器返回的复杂 HTML 元素的最佳方式

ajax - HttpPost上的ASP MVC3 HandleError

php - 如何按 ASC 或 DESC 日期对表格进行排序?

php - 自动下载并安装一个exe文件

Ruby 中的 HTML 解析器到 DOM

jquery - 使用光标移动背景图像不起作用

javascript - echo $_POST[valuename] 未被读取