使用 PHP 消息队列实现 Android 与 Web 通信
使用 PHP 消息队列实现 Android 与 Web 通信
需求描述很简单:Android 发送数据到 Web 网页上。
系统: Ubuntu 14.04 + apache2 + php5 + Android 4.4
思路是 socket + 消息队列 + 服务器发送事件,下面的讲解步骤为 Android 端,服务器端,前端。重点是在于 PHP 进程间通信。
Android 端比较直接,就是一个 socket 程序。需要注意的是,如果直接在活动主线程里面创建 socket 会报一个 android.os.NetworkOnMainThreadException, 因此最好的方法是开个子线程来创建 socket,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
然后是发送消息
1 2 3 4 5 6 7 8 9 10 |
|
完成后还需要关闭 socket
1 2 3 4 5 6 7 8 9 10 11 12 |
|
注意这些方法都不要在主线程执行。
下面是服务器 PHP 端。
首先要运行一个进程来接收信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
也比较简单。这个进程是独立运行的,那么打开网页请求数据,需要从另一段脚本接入,下面就需要用到进程间通信,我选择消息队列,也就是上面的 $msg_queue 变量。
脚本主程序这么写。
1 2 3 4 |
|
那么handleData() 的任务就是把收到的消息放到队列里面去
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
这样一来,其他进程就可以通过 key 找到这个队列,从里面读取消息了。使用这样可读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
现在就差最后一步,如何主动把数据发往前端?这要用到 HTML5 的新特性:服务器发送事件(要使用较新的非 IE 浏览器,具体查看这里)。直接看JS代码
1 2 3 4 5 |
|
那么这个 getData.php 就是上面那个从消息队列获取数据的脚本。只是为了让它被识别为服务器事件,需要加一点格式上的说明,具体如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
下面就可以开始运行,首先运行服务器
php socket.php
打印了 listening 就可以使用 Android 设备连接了。
然后再用 Web 上 JS 请求 getData 脚本,请求后前台可以不断地获得新的数据。需要注意的是消息队列可能会阻塞(消息量达到上限),再有就是 JS 本身消息机制的限制,因此丢失,延迟等现象频发。
Web 通信的老问题就是稳定性。以前老是怨恨 Web QQ 掉包,其实整个 Web 革命尚未成功。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

The message has been sent but rejected by the other party. This means that the sent information has been successfully sent from the device, but for some reason, the other party did not receive the message. More specifically, this is usually because the other party has set certain permissions or taken certain actions, which prevents your information from being received normally.

In iOS17, Apple has added several new features to its Messages app to make communicating with other Apple users more creative and fun. One of the features is the ability to use emojis as stickers. Stickers have been around in the Messages app for years, but so far, they haven't changed much. This is because in iOS17, Apple treats all standard emojis as stickers, allowing them to be used in the same way as actual stickers. This essentially means you're no longer limited to inserting them into conversations. Now you can also drag them anywhere on the message bubble. You can even stack them on top of each other to create little emoji scenes. The following steps show you how it works in iOS17

1. Being added to the blacklist: The message has been sent but rejected by the other party. Generally, you have been blacklisted. At this time, you will not be able to send messages to the other party, and the other party will not be able to receive your messages. 2. Network problems: If the recipient's network condition is poor or there is a network failure, the message may not be successfully received. At this point, you can try to wait for the network to return to normal before sending the message again. 3. The other party has set up Do Not Disturb: If the recipient has set up Do Not Disturb in WeChat, the sender’s messages will not be reminded or displayed within a certain period of time.

Xiaomi 14Pro is a flagship model with excellent performance and configuration. It has achieved high sales since its official release. Many small functions of Xiaomi 14Pro will be ignored by everyone. For example, it can be set to light up the screen for messages. Although the function is small, , but it is very practical. Everyone will encounter various problems when using the mobile phone. So how to set up the Xiaomi 14Pro to light up the screen for messages? How to set up Xiaomi Mi 14 Pro to light up the screen for messages? Step 1: Open your phone’s Settings app. Step 2: Swipe down until you find the "Lock screen and password" option and click to enter. Step 3: In the "Lock screen & passcode" menu, find and click the "Turn on screen for notifications" option. Step 4: On the "Turn on screen when receiving notifications" page, turn on the switch to enable

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

With the release of iOS 17, Apple has added a wealth of new features, features, and enhancements to its mobile operating system. One of them is that you can now leave FaceTime iPhone video messages and audio if someone misses your call. After you leave a message, your friends and family can even play your message on their Apple Watch, making it easier for you to stay connected. The first step to leaving a video message during a FaceTime call is to initiate the call. If the person on the other end doesn't answer the call, an option to record a video will appear on the screen. After clicking this option, the countdown starts from 5 to 1, after which you can start recording the message. The interface is user-friendly with a "Call Again" button and a "Record Video" button
