Android 和 PHP 之间最快的通信

发布于 2024-10-29 08:05:57 字数 398 浏览 1 评论 0原文

大家好 我正在构建一个 Android 应用程序,需要来自 PHP 服务器的数据。

假设我的网络应用程序上有一个文本输入。每当用户触发“onkeydown”时,我想将字符发送到 Android 应用程序,而我的 Android 应用程序将只输出当前字符。 Web 应用程序将不断向 myAandroid 应用程序发送小“命令”。我想知道接收这些发送/接收这些命令的最快方式是什么。

我看到了一些关于如何实现这一点的选择。

1)我的android应用程序不断轮询服务器以查看是否已写入消息。

2) 我的 Android 应用程序将设置一个 PHP 脚本将连接并向其推送消息的套接字服务器。

3)我想这非常慢且不准确,但我想我可以使用推送通知。

还有其他选择吗?最好的解决方案是什么?

Hi all
Im building a android application that requires data from a PHP server.

Lets say i have a text input on my web application. Whenever the users trigger a "onkeydown" i want to send the character to the Android application and my Android application will just output the current character. The web application will constantly send small "commands" to myAandroid application. I would like to know what is the fastest possible way to receive those send / receive those commands.

I see a few options on how this could be implemented.

1) My android application constantly polls the server to see if a message has been written.

2) My Android application would setup a socket server that the PHP script would connect and push the message to.

3) I guess this is extremely slow and inaccurate, but i guess i could use push notifications.

Is there any other alternatives ? and what would be the best solution?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-11-05 08:05:57

推送消息并不慢,只是不准确(即不保证一定会发送)。
尽管如此,接收推送消息可能比每隔五秒轮询一次服务器要快。

如果您有足够的时间,那么值得将这两种方法结合起来,即推送消息和常规池作为后备。

也就是说,您没有提供有关您正在做的事情的足够信息,因此很难提出任何建议。

最快的方法是连接到服务器,保持连接打开,然后让 PHP 缓慢地传输少量的乱码数据,直到有真正的消息要发送。这是最快的事情,但在电池使用等方面当然是禁忌。所以请解释一下你想做什么。

更新要远程命令手机,您需要开放的连接。有很多方法可以做到这一点,我将仅发布 php 的概念证明。

<?php
//to send a command to the phone just create a file 
//named command.txt in this directory
//from any other script
set_time_limit(0);

while (true)
{
    if (file_exists('command.txt'))
    {
        $command = file_get_contents('command.txt');
        rm( 'command.txt' );
        echo $command;
            flush();
        unset ($command);
    } else {
        echo asc(0); //prevent the connection to be dropped
            flush();
        usleep(200); //sleeps for 200 ms
    }
}

?>

这个示例缺少很多东西,它只是如何处理 PHP 的开放连接的概念证明。
客户端需要删除所有零字节并保留下载的其余数据。
这绝对是处理单向远程交互的最快方法,但另一方面,会很快消耗电池:)。

Push message is not slow, just inaccurate (ie. no guarantee that it will be sent).
Still, it's probably faster to receive a push message than to poll the server every - say - five seconds.

If you can afford the time it's worth combining the two approach, ie push messages and regular pooling as fall back.

That said, you don't provide enough info on what you're doing, so it's difficult to suggest anything.

The fastest way would be to connect to the server, leave the connection open, and have PHP slowly stream little quantities of gibberish data until it has a real message to send. That's the fastest thing, but of course is a no-no in terms of battery usage and so on. So please explain what you're trying to do.

UPDATE to remote command a phone you need an open connection. There are many ways to do it, I'll post just a proof of concept php.

<?php
//to send a command to the phone just create a file 
//named command.txt in this directory
//from any other script
set_time_limit(0);

while (true)
{
    if (file_exists('command.txt'))
    {
        $command = file_get_contents('command.txt');
        rm( 'command.txt' );
        echo $command;
            flush();
        unset ($command);
    } else {
        echo asc(0); //prevent the connection to be dropped
            flush();
        usleep(200); //sleeps for 200 ms
    }
}

?>

This example lacks many things, it's just a proof of concept on how to handle an open connection with PHP.
The client side would need to strip all zero bytes and keep the rest of data downloaded.
This is in absolute the fastest way to handle a 1 way remote interaction, on the other hand, will suck battery pretty fast :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文