Using PHP message queue to implement Android and Web communication_PHP tutorial

WBOY
Release: 2016-07-13 09:53:59
Original
979 people have browsed it

Use PHP message queue to communicate between Android and Web

The requirement description is simple: Android sends data to the Web page.

System: Ubuntu 14.04 apache2 php5 Android 4.4

The idea is that the socket message queue server sends events. The following steps are Android side, server side, and front end. The focus is on PHP inter-process communication.

The Android side is relatively straightforward, it is a socket program. It should be noted that if you create a socket directly in the active main thread, an android.os.NetworkOnMainThreadException will be reported. Therefore, the best way is to open a sub-thread to create the socket. The code is as follows

    
private Socket socket = null;
private boolean connected = false;
private PrintWriter out;
private BufferedReader br;

private void buildSocket(){
        if(socket != null)
            return;
        try {
            socket = new Socket("223.3.68.101",54311); //IP地址与端口号
            out = new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    socket.getOutputStream())), true);
            br = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        connected = true;
  }
Copy after login

Then send the message

    public void sendMsg(String data){
        if(!connected || socket == null) return;
        synchronized (socket) {
            try {
                out.println(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Copy after login

You also need to close the socket after completion

    private void closeSocket(){
        if(  socket == null) return;
        try {
            socket.close();
            out.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket = null;
        connected = false;
    }
Copy after login

Note that these methods should not be executed on the main thread.

The following is the server PHP side.

First you need 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";
		die;
	}
	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";
		die;
	}

	if (socket_listen($sock, 4) == false){
		echo "socket_listen() failed:" . socket_strerror(socket_last_error()) . "\n";
		die;
	}
	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); //见后文
		$buf = socket_read($msgsock, 8192);  
		
		//看情况 break 掉
	}
	socket_close($msgsock);  
	
}
Copy after login

It's also relatively simple. This process runs independently, so to open a web page to request data, you need to access it from another script. Next, you need to use inter-process communication. I choose the message queue, which is the $msg_queue variable above.

The main program of the script is written like this.

$msg_queue_key = ftok(__FILE__,'socket'); //__FILE__ 指当前文件名字
$msg_queue = msg_get_queue($msg_queue_key); //获取已有的或者新建一个消息队列
buildSocket($msg_queue);
socket_close($sock);
Copy after login
The ftok() function is to generate a queue key to distinguish.

Then the task of handleData() is to put the received message into the queue

function handleData($dataStr, $msg_queue){
	msg_send($msg_queue,1,$dataStr);
}
Copy after login
Socket process script skeleton
<!--?php
//socket.php 服务器进程
 function buildSocket($msg_queue){
}

function handleData($dataStr, $msg_queue){
}

set_time_limit(0);
$msg_queue_key = ftok(__FILE__,&#39;socket&#39;);
$msg_queue = msg_get_queue($msg_queue_key);

buildSocket($msg_queue);
socket_close($sock);

?-->
Copy after login

In this way, other processes can find this queue through key and read messages from it. Use this to make it readable

function redFromQueue($message_queue){
	msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
	echo $message."\n\n";
}

$msg_queue_key = ftok("socket.php", &#39;socket&#39;); //第一个变量为上方socket进程的文件名。
$msg_queue = msg_get_queue($msg_queue_key, 0666);

while(true){
	$msg_queue_status = msg_stat_queue($msg_queue); //获取消息队列的状态
	if($msg_queue_status["msg_qnum"] == 0) //如果此时消息队列为空,那么跳过,否则会读取空行。
		continue;
	redFromQueue($msg_queue);
}
Copy after login


Now the last step is left, how to actively send data to the front end? This requires the use of a new feature of HTML5: server-sent events (to use newer non-IE browsers, see here for details). Look directly at the JS code

var source = new EventSource("php/getData.php"); //Web 服务器路径
source.onmessage = function(event){ //消息事件回调
	var resData = event.data;	
	document.getElementById("res").innerHTML=resData;
};
Copy after login

Then this getData.php is the script above that gets data from the message queue. Just to make it recognized as a server event, you need to add some formatting instructions, as follows.

<!--?php
//getData.php,提供给 Web 请求使用。
//声明文档类型
header(&#39;Content-Type: text/event-stream&#39;);
header(&#39;Cache-Control: no-cache&#39;);

function redFromQueue($message_queue){
	msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
	echo "data:".$message."\n\n"; //注意一定要在数据前面加上 “data:”
	flush(); //立刻 flush 一下
}

$msg_queue_key = ftok("socket.php", &#39;socket&#39;);
$msg_queue = msg_get_queue($msg_queue_key, 0666);

echo "data:connected\n\n";
flush();

while(true){
	$msg_queue_status = msg_stat_queue($msg_queue);
	if($msg_queue_status["msg_qnum"] == 0)
		continue;

	redFromQueue($msg_queue);
}

?-->
Copy after login

Now you can start running, first run the server

php socket.php

After printing listening, you can connect using an Android device.

Then use JS on the Web to request the getData script. After the request, the frontend can continuously obtain new data. It should be noted that the message queue may be blocked (the message volume reaches the upper limit), and there are limitations of the JS message mechanism itself, so losses, delays and other phenomena occur frequently.

The old problem of Web communication is stability. In the past, I always resented Web QQ for packet dropping. In fact, the entire Web revolution has not yet succeeded.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998740.htmlTechArticleUsing PHP message queue to implement Android and Web communication requirements description is very simple: Android sends data to the Web page. System: Ubuntu 14.04 apache2 php5 Android 4.4 The idea is...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template