javascript PHP中(跨平台)实时数据流的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4495749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:53:28  来源:igfitidea点击:

Best approach for (cross-platform) real-time data streaming in PHP?

phpjavascriptpush

提问by mindplay.dk

I've been wondering how to do "true" (semi) real-time data streaming with PHP.

我一直想知道如何使用 PHP 进行“真正的”(半)实时数据流。

Possible applications: chat rooms, auctions, games, etc.

可能的应用:聊天室、拍卖、游戏等。

By "true", I mean the data is not just written somewhere and polled continuously, but actually streaming to the client somehow.

通过“真实”,我的意思是数据不仅写在某处并连续轮询,而且实际上以某种方式流式传输到客户端。

By "semi", I mean it's okay if only the stream from the server to the client is real-time, and messages from the client to the server are not.

通过“半”,我的意思是,如果只有从服务器到客户端的流是实时的,而从客户端到服务器的消息不是实时的,那也没关系。

For the communication between the client and server, I'd like to stick with plain HTTP (AJAX) rather than some other protocol.

对于客户端和服务器之间的通信,我想坚持使用纯 HTTP (AJAX) 而不是其他一些协议。

Streaming to the client with HTTP is possible by manually flushing the output buffer.

通过手动刷新输出缓冲区,可以使用 HTTP 流式传输到客户端。

The question is what to connect that script to on the server-side?

问题是在服务器端将该脚本连接到什么?

And once it's connected, to do a blocking read, rather than polling for changes.

并且一旦连接,就进行阻塞读取,而不是轮询更改。

The shared memory (shmop) extension would work, but it's not cross-platform.

共享内存 (shmop) 扩展可以工作,但它不是跨平台的。

Perhaps memcached would work? But I'm not sure if there's a way to do a blocking read, so it comes down to polling again - although I'm sure memcached is pretty fast, I just don't like the idea of continuous polling.

也许 memcached 会起作用?但是我不确定是否有办法进行阻塞读取,所以它再次归结为轮询——虽然我确定 memcached 非常快,但我只是不喜欢连续轮询的想法。

Any ideas?

有任何想法吗?

采纳答案by Simian

PHP is not well suited for implementing realtime data streaming. PHP is very slow, and is not designed to build multi-threaded applications. You'd be better off implementing a full blown socket server in a language like Python or Java.

PHP 不太适合实现实时数据流。PHP 非常慢,并且不是为构建多线程应用程序而设计的。你最好用 Python 或 Java 之类的语言实现一个完整的套接字服务器。

That said, I would highly recommend checking out NodeJS: http://nodejs.org/

也就是说,我强烈建议您查看 NodeJS:http: //nodejs.org/

It uses an asynchronous event based model for I/O, instead of having threads block in an event loop. NodeJS servers are written in Javascript. NodeJS is fast, scales, and has a low learning curve.

它使用基于异步事件的 I/O 模型,而不是在事件循环中阻塞线程。NodeJS 服务器是用 Javascript 编写的。NodeJS 速度快、可扩展且学习曲线低。

Clients would connect to a NodeJS HTTP server using long polling Ajax requests. PHP could connect to NodeJS directly and push notifications. Or PHP could write to a message queue, or database, memcache etc, and NodeJS would poll those data stores for updates, and send new messages to clients.

客户端将使用长轮询 Ajax 请求连接到 NodeJS HTTP 服务器。PHP 可以直接连接到 NodeJS 并推送通知。或者 PHP 可以写入消息队列、数据库、内存缓存等,NodeJS 将轮询这些数据存储以获取更新,并向客户端发送新消息。

You would possibly need to write your own daemon to serve as a go between from NodeJS to MySQL, memcached, etc. when polling for updates. NodeJS would keep a socket open with a daemon process. The daemon process would poll data stores for updates, and send the updates to NodeJS. A NodeJS HTTP server would then send those updates to clients.

在轮询更新时,您可能需要编写自己的守护程序来充当从 NodeJS 到 MySQL、memcached 等之间的切换。NodeJS 将通过守护进程保持套接字打开。守护进程将轮询数据存储以获取更新,并将更新发送到 NodeJS。然后 NodeJS HTTP 服务器会将这些更新发送给客户端。

See this tutorial for implementing a realtime Twitter stream: http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

请参阅本教程以实现实时 Twitter 流:http: //net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

回答by Jonah

If you're using HTML and Javascript, then you want WebSockets. If it's Flash or anything else, then normal TCP sockets.

如果您使用 HTML 和 Javascript,那么您需要 WebSockets。如果是 Flash 或其他任何东西,则是正常的 TCP 套接字。

The idea for either is that you run a server file (written in PHP), that waits for connections. Once it's connected to one or more clients, data can be pushed both ways. There are a few PHP WebSocket projects out there. Check out this:

两者的想法是运行一个服务器文件(用 PHP 编写),等待连接。一旦连接到一个或多个客户端,数据就可以双向推送。有一些 PHP WebSocket 项目。看看这个:

http://code.google.com/p/phpwebsocket

http://code.google.com/p/phpwebsocket

There's also a framework called Skeleton that I've been contributing to that has a WebSocket server library built in. Still in the unstable stages, though.

还有一个叫做 Skeleton 的框架,我一直在为它做贡献,它内置了一个 WebSocket 服务器库。不过仍处于不稳定阶段。

http://code.google.com/p/skeleton

http://code.google.com/p/skeleton

Unfortunately, WebSockets are still a new technology, so they're not supported universally. As @Christian mentioned, you may want to use the Socket.IO library.

不幸的是,WebSockets 仍然是一项新技术,因此并未得到普遍支持。正如@Christian 所提到的,您可能想要使用 Socket.IO 库。

回答by Christian Joudrey

If you want to communicate between PHP and another language (for instance a C++ application) you might want to look into Apache Thrift (http://thrift.apache.org/). Apache Thrift is widely used at Facebook for "scalable cross-language services development".

如果您想在 PHP 和另一种语言(例如 C++ 应用程序)之间进行通信,您可能需要查看 Apache Thrift ( http://thrift.apache.org/)。Apache Thrift 在 Facebook 被广泛用于“可扩展的跨语言服务开发”。

Edit: I would probably use Apache Thrift to communicate with a Twistedapplication listening on port 80 and have the browsers connect to the Twisted application using either long polling or a websocket. You might want to also look into Socket.IO, it's a cross-browser web socket implementation which is made for realtime applications.

编辑:我可能会使用 Apache Thrift 与侦听端口 80的Twisted应用程序进行通信,并让浏览器使用长轮询或 websocket 连接到 Twisted 应用程序。您可能还想查看Socket.IO,它是一个跨浏览器的 Web 套接字实现,专为实时应用程序而设计。

Basically you would have your application push to your Twisted web server using Thrift and you would then pass the message to any open connections.

基本上,您可以使用 Thrift 将应用程序推送到 Twisted Web 服务器,然后将消息传递给任何打开的连接。

  • Christian
  • 基督教