【问题标题】:How to receive updates from Server without requesting every nth seconds [closed]如何在不每隔 n 秒请求一次的情况下从服务器接收更新[关闭]
【发布时间】:2014-10-10 21:18:39
【问题描述】:

我正在构建一个移动应用程序,只要服务器有可用的新数据,它就会从服务器接收数据。我正在为我的移动应用程序使用 cordova 和 ionic 框架,并为移动应用程序获取数据的 API 和服务器使用 PHP/MySQL/Apache。

有什么方法可以让我从我的服务器中检索数据(JSON 格式),而无需在我的移动应用程序中使用 http.get 每隔 n 秒不断地请求数据?因为我只需要在它有新数据时获取,而不是一直获取新数据,但有时,在峰值时每秒都有新数据。 Apache/PHP 是否可以处理这个问题,或者我是否需要切换到例如 nodejs 之类的?提前致谢。

顺便说一句,我希望我的移动应用程序在一秒钟内收到数据。

我的问题与Receive update from server with mobile frameworkhttps://softwareengineering.stackexchange.com/questions/225589/how-to-refresh-keep-up-to-date-content-in-the-browser-without-overloading-the-se 非常相似,但我现在还在犹豫。

【问题讨论】:

  • 服务器端推送机制需要使用websockets。你可以看看socket.io
  • 这适用于 PHP 和 Apache 吗?还是我真的需要切换到nodejs?
  • 服务器推送不需要 websockets,但它是最有效的传输方式。例如,长轮询也可以满足要求。
  • @frank 根据您是否需要全双工通信,您可以使用server-sent events 而不是 websockets。这仅允许单向通信,因此如果您希望与服务器进行完整的来回对话,您仍然需要 websockets。

标签: php mysql node.js angularjs cordova


【解决方案1】:

Node.js 和 Socket.io 使类似的事情变得微不足道,但是您几乎可以使用任何 Web 后端来做到这一点。有几个选项可以做到这一点,但我尽可能倾向于使用 websockets。我从未使用过它,但Ratchet 似乎可以为 PHP 做你想做的事。看看他们的Hello World tutorial 看看如何设置它。

更新

由于您使用的是 Cordova,因此 websockets 是有意义的。这是使用 socket.io 在 Node.js 中的示例实现。

var app = require('http').createServer(handler);
var io = require('socket.io')(app);

app.listen(80);

function handler (req, res) {
  // We aren't serving any http requests outside of socket.io. 
  // Return a 404 error for all other requests.
  res.status(404).send('Not found');
}

io.on('connection', function (socket) {
  //  This is either a new client connection, or a client reconnecting. 
  //  You can use cookies to establish identity and handle reconnects 
  //  differently if necessary.

  socket.on('new-content', function(content) {
    //  persist the file in the database if necessary and resend it to all 
    //  connected clients
    socket.broadcast.emit('new-content', content);
  });
});

这里我们只是创建一个简单的中继服务器。您可以发送一个“文件”,在这种情况下它只是一个内容字符串,它将被发送给所有其他连接的用户。在这种情况下,您不必一直查询数据库来查找新内容,您可以从来自客户端的内容中触发它。如果您希望离线用户能够在他们在线时接收该内容,您将需要某种持久性和机制来跟踪和处理它。

客户端脚本也很简单。

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://myserver.com');
  socket.on('new-file function (content) {
    // A new file was sent from the server, do something with the content here.
  });

  function sendFile(content) {
    socket.emit('new-file', content);  
  }
</script>

【讨论】:

  • 您能否介绍一下 websockets(socket.io) 的替代方案,它们可用于解决 OPs 要求。
  • 是的,有没有其他网络插座的替代品?
  • 首先,socket.io 不仅仅是 websockets。它是对多种服务器推送技术的抽象。这使得开发者能够以一种方式编写“服务器推送”代码,socket.io 将升级到服务器和客户端都支持的最有效的方法。
  • @Port8080 Websockets 实际上是最轻量级的服务器推送技术实现。不幸的是,似乎需要很多样板代码才能让它在 PHP 中工作。使用 node / socket.io 的一大优势是它需要更少的代码行来让 websockets 与故障转移工作。查看文档并与上面的 Ratchet 进行比较。 socket.io/docs
  • 抱歉这个菜鸟问题。由于我正在使用 Cordova 开发移动应用程序,这是否意味着 socket.io 的代码需要驻留在我的移动应用程序中,或者它应该位于我的 Web 服务器上?这只是基于socket.io/docs 中的示例,因为我还不熟悉 socket.io
猜你喜欢
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
相关资源
最近更新 更多