Implement chat using websocket in android with php -
Implement chat using websocket in android with php -
i want implement chat functionality using websocket in android php.
i need php code same.
the scenerio this.
the android developer send message android device php application , php message sent users android phone meant for.
after need save chats in database well.
i have no thought how it.
please help , provide code fo this.
the code have written follows.
$address = "0.0.0.0"; $port = 5000; $max_clients = 10; if(!($sock = socket_create(af_inet, sock_stream, 0))) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("couldn't create socket: [$errorcode] $errormsg \n"); } echo "socket created \n"; // bind source address if( !socket_bind($sock, $address , 5000) ) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("could not bind socket : [$errorcode] $errormsg \n"); } echo "socket bind ok \n"; if(!socket_listen ($sock , 10)) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("could not hear on socket : [$errorcode] $errormsg \n"); } echo "socket hear ok \n"; echo "waiting incoming connections... \n"; //array of client sockets $client_socks = array(); //array of sockets read $read = array(); //start loop hear incoming connections , process existing connections while (true) { //prepare array of readable client sockets $read = array(); //first socket master socket $read[0] = $sock; //now add together existing client sockets ($i = 0; $i < $max_clients; $i++) { if($client_socks[$i] != null) { $read[$i+1] = $client_socks[$i]; } } //now phone call select - blocking phone call if(socket_select($read , $write , $except , null) === false) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("could not hear on socket : [$errorcode] $errormsg \n"); } //if ready contains master socket, new connection has come in if (in_array($sock, $read)) { ($i = 0; $i < $max_clients; $i++) { if ($client_socks[$i] == null) { $client_socks[$i] = socket_accept($sock); //display info client connected if(socket_getpeername($client_socks[$i], $address, $port)) { echo "client $address : $port connected us. \n"; } //send welcome message client $message = "welcome php socket server version 1.0 \n"; $message .= "enter message , press enter, , shall reply \n"; socket_write($client_socks[$i] , $message); break; } } } //check each client if send info ($i = 0; $i < $max_clients; $i++) { if (in_array($client_socks[$i] , $read)) { $input = socket_read($client_socks[$i] , 1024); if ($input == null) { //zero length string meaning disconnected, remove , close socket unset($client_socks[$i]); socket_close($client_socks[$i]); } $n = trim($input); $output = "ok ... $input"; //echo "sending output client \n"; //send response client //socket_write($client_socks[1] , $output); //send response client socket_write($client_socks[$i] , $output); } } }
check link http://socketo.me/docs/hello-world
ratchet loosely coupled php library providing developers tools create real time, bi-directional applications between clients , servers on websockets.
php android websocket chat
Comments
Post a Comment