在PHP中为聊天创建客户端
| 
                         
 我正在尝试创建一个 
 PHP聊天,所以我有server.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),$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文件,因此一旦用户登录,他将向服务器发送消息,服务器将回复用户未看到的消息. Enter a message to send : hello Reply : hello 我使用AJAX在聊天中发送用户输入,所以我希望能够接收该消息,并将其发送到服务器,我在终端上启动并回复并转发到网页并将其附加到文本框区域. 
 对于偏离主题感到抱歉,但如果可以的话,最好使用带有http-bind模块的ejabberd服务器等XMPP就绪解决方案.当然,有一些缺点是这样的解决方案,但缺点是更大.只需看看这个解决方案,也许它会以低成本解决您的问题. 
  
                          见related answer with brief desc on XMPP solution (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
