Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a client for a chat in PHP

Tags:

json

html

ajax

php

I'm trying to create a PHP chat, so I have server.php that starts the server on the terminal, which is listen to client connections:

<?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();

?>

The function client() is the code I have from a client.php file, which when I execute on the terminal, is able to send and receive messages from the server.php. Now I would like to use my main.php file, so once the user is logged in he will send messages to the server, which will reply back the messages that user haven't seen. When I run server.php and client.php from two different terminals, I'm able to send and receive messages, however I would like to do that using main.php, transform that reply message into a JSON object and send back to the html page where it will get appended to a textarea box. My problem is: how can I get the reply that client.php received and send it back to the html page? When I execute it on the terminal, I have:

Enter a message to send : hello
Reply : hello

I use AJAX to send the user input in the chat, so I wanted to be able to take that message, and send it to the server, which I started on the terminal and take the reply back and forward to the webpage and append that to the text box area. How can I accomplish that? Should I start client.php as a service through main.php? Or should I use the client($input) function to send a message and then return what it sends, back? However, I wanted that client to be running until the use logs out, because other clients may connect to the chat. How can I accomplish that is kind of fuzzy for me. The code in client( $input ) is the same as in client.php.

like image 202
cybertextron Avatar asked Dec 16 '12 15:12

cybertextron


1 Answers

Sorry for off-topic, but if you can, it would be better to use XMPP ready solution like ejabberd server with http-bind module. Sure, there is some cons it such solution, but cons are greater. Just look in this solution, maybe it will solve your problem with low cost.

see related answer with brief desc on XMPP solution

like image 154
pinepain Avatar answered Sep 18 '22 04:09

pinepain