Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a client for a chat in PHP
    text
    copied!<p>I'm trying to create a <code>PHP</code> chat, so I have <code>server.php</code> that starts the server on the terminal, which is listen to <code>client</code> connections:</p> <pre><code>&lt;?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" =&gt; "batman123", "robin" =&gt; "robin123", "joe" =&gt; "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(); ?&gt; </code></pre> <p>The function <code>client()</code> is the code I have from a <code>client.php</code> file, which when I execute on the terminal, is able to send and receive messages from the <code>server.php</code>. Now I would like to use my <code>main.php</code> 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 <code>server.php</code> and <code>client.php</code> from two different terminals, I'm able to send and receive messages, however I would like to do that using <code>main.php</code>, transform that reply message into a <code>JSON</code> object and send back to the <code>html</code> page where it will get appended to a <code>textarea</code> box. My problem is: how can I get the reply that <code>client.php</code> received and send it back to the html page? When I execute it on the terminal, I have:</p> <pre><code>Enter a message to send : hello Reply : hello </code></pre> <p>I use <code>AJAX</code> 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 <code>client.php</code> as a service through <code>main.php</code>? Or should I use the <code>client($input)</code> function to send a message and then return what it sends, back? However, I wanted that <code>client</code> 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 <code>client( $input )</code> is the same as in <code>client.php</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload