Using PHP Message Queuing for Android and Web communication
The requirements description is simple: Android sends data to a Web page.
System: Ubuntu 14.04 + apache2 + php5 + Android 4.4
The idea is that the socket + message Queue + server sends events, the following tutorial steps for Android, server-side, front-end. The focus is on PHP inter-process communication.
The Android side is more direct, is a socket program. It is important to note that if you create a socket directly inside the active main thread it will report a android.os.NetworkOnMainThreadException, so the best way is to open a subroutine to create the socket, the code is as follows
Private Socket SOCKET = Null;private Boolean connected = false;private printwriter out;private bufferedreader br;private V OID Buildsocket () { if (socket! = NULL) return; try { socket = new Socket ("223.3.68.101", 54311),//IP address and port number out = new PrintWriter ( new BufferedWriter ( New OutputStreamWriter ( Socket.getoutputstream ())), true); br = new BufferedReader ( new InputStreamReader (Socket.getinputstream ())); } catch (IOException e) { E.printstacktrace (); } connected = true; }
Then send a message
public void sendmsg (String data) { if (!connected | | socket = = NULL) return; Synchronized (socket) { try { out.println (data) } catch (Exception e) { e.printstacktrace ()} } }
You need to close the socket when you're done
private void Closesocket () { if ( socket = = null) return; try { socket.close (); Out.close (); Br.close (); } catch (IOException e) { e.printstacktrace (); } socket = NULL; connected = false; }
Note that these methods are not executed on the main thread.
The following is the server PHP side.
The first step is to run a process to receive the information.
function buildsocket ($msg _queue) {$address = "223.3.68.101"; $port = 54321; if ($sock = Socket_create ( Af_inet, Sock_stream, sol_tcp) = = = False) {echo "Socket_create () failed:". Socket_strerror (Socket_last_error ()). "/n";d ie;} echo "Socket create\n", if (Socket_set_block ($sock) = = False) {echo "Socket_set_block () Faild:". Socket_strerror (Socket_last_error ()). "\ n"; Die;} if (Socket_bind ($sock, $address, $port) = = False) {echo "Socket_bind () failed:". Socket_strerror (Socket_last_error ()). "\ n";d ie;} if (Socket_listen ($sock, 4) = = False) {echo "Socket_listen () failed:". Socket_strerror (Socket_last_error ()). "\ n";d ie;} echo "listening\n"; if ($msgsock = socket_accept ($sock)) = = = False) {echo "socket_accept () Failed:reason:". Socket_strerror (Socket_last_error ()). "\ n"; Die } $buf = Socket_read ($msgsock, 8192); while (true) {if (strlen ($BUF) > 1) handledata ($buf, $msg _queue);//See Post $buf = Socket_read ($msgsock, 8192); See conditions break off}socket_close ($msgsock); }
is also relatively simple. This process is run independently, then open the Web request data, need to access from another script, the following need to use inter-process communication, I choose the message queue, that is, the above $msg _queue variable.
The script main program writes this.
$msg _queue_key = Ftok (__file__, ' socket '); __FILE__ refers to the current file name $msg_queue = Msg_get_queue ($msg _queue_key); Get existing or create a new message Queue Buildsocket ($msg _queue); Socket_close ($sock);
The Ftok () function is the key that generates a queue to differentiate.
Then the Handledata () task is to put the received messages into the queue.
function Handledata ($dataStr, $msg _queue) {msg_send ($msg _queue,1, $dataStr);}
Socket Process Script Skeleton
In this way, other processes can find the queue by key and read the message from the inside. Use this to read
function Redfromqueue ($message _queue) {msg_receive ($message _queue, 0, $message _type, 1024x768, $message, True, msg_ipc_ NOWAIT); Echo $message. " \ n ";} $msg _queue_key = Ftok ("socket.php", ' socket '); The first variable is the file name of the socket process above. $msg _queue = Msg_get_queue ($msg _queue_key, 0666), while (true) {$msg _queue_status = msg_stat_queue ($msg _queue);// Gets the status of the message queue if ($msg _queue_status["msg_qnum"] = = 0)//If the message queue is empty at this time, then skip, otherwise the blank line will be read. Continue;redfromqueue ($msg _queue);}
Now is the last step, how to proactively send data to the front end? This will use the new features of HTML5: The server sends the event (to use a newer non-IE browser, see here). Read the JS code directly
var Source = new EventSource ("php/getdata.php"); Web Server Path source.onmessage = function (event) {//Message event callback var resdata = Event.data;document.getelementbyid ("res"). Innerhtml=resdata;};
So this getdata.php is the one above the script that gets the data from the message queue. Just to let it be recognized as a server event, you need to add a bit of formatting instructions, as follows.
You can start running the server first
PHP socket.php
The listening is printed and can be connected using an Android device.
Then use the Web JS request GetData script, the front desk can continue to obtain new data after the request. It is important to note that Message Queuing may be blocked (the message volume reaches the upper limit), and then there is the limit of the JS itself message mechanism, so the phenomenon of loss, delay, etc. is frequent.
The old problem with Web communication is stability. Used to resent the web QQ swap, in fact, the entire Web revolution has not been successful.
http://www.bkjia.com/PHPjc/998740.html www.bkjia.com true http://www.bkjia.com/PHPjc/998740.html techarticle using PHP Message Queuing to implement Android and Web communication requirements description is simple: Android sends data to Web pages. System: Ubuntu 14.04 + apache2 + php5 + Android 4.4 idea is ...