php 在 Apache 服务器上使用 WebSocket

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11334320/
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-08-25 00:18:19  来源:igfitidea点击:

Using WebSocket on Apache server

phphtmlapachewebsocketphpwebsocket

提问by Edouard Reinach

With all the buzz around WebSockets, it's pretty hard to find a good walkthrough on how to use them with an Apache server on Google.

随着围绕 WebSockets 的所有嗡嗡声,很难找到关于如何在 Google 上的 Apache 服务器上使用它们的好的演练。

We're developing a plugin, in PHP (symfony2), which will run from time to time kind of a chat instance. And we find WebSockets more interesting, standard and quick than AJAX for this matter. The thing is, we don't have much sysadmin ressources in our group and we find hard to gather good informations on the following matters:

我们正在用 PHP (symfony2) 开发一个插件,它会不时运行一个聊天实例。在这方面,我们发现 WebSockets 比 AJAX 更有趣、更标准、更快速。问题是,我们组中没有太多系统管理员资源,我们发现很难收集有关以下事项的好信息:

  • Can we run a WebSocket instance on a traditional Apache, dedicated server, and if yes, do you have useful links for us?
  • If we need to mod the server, what kind of tools would you recommend knowing that we are not too skilled in sysadmin so we can't afford to have a high maintenance b*** on this.
  • 我们能否在传统的 Apache 专用服务器上运行 WebSocket 实例,如果是,您是否有对我们有用的链接?
  • 如果我们需要修改服务器,你会推荐什么样的工具,因为我们在系统管理员方面不太熟练,所以我们负担不起在这方面的高维护费用。

Thank you very much,

非常感谢,

ps: we'll link back to your blog/site as we'll make a technical/informational post on our devblog about this part of our app.

ps:我们将链接回您的博客/网站,因为我们将在我们的开发博客上发布关于我们应用程序这一部分的技术/信息帖子。

Thank you again!

再次感谢你!

采纳答案by zaf

One path is to use an independent installed web sockets server.

一种方法是使用独立安装的 Web 套接字服务器。

For PHP you can try: http://code.google.com/p/phpwebsocket/or http://github.com/Devristo/phpws/

对于 PHP,您可以尝试:http: //code.google.com/p/phpwebsocket/http://github.com/Devristo/phpws/

There are some other projects which you can try as well.

您也可以尝试其他一些项目。

Basically, you need to upload, unpack and start running the process.

基本上,您需要上传、解压缩并开始运行该过程。

On the frontend, you'll have javascript connecting to the server on the specific port.

在前端,您将使用 javascript 连接到特定端口上的服务器。

Most websocket servers have a demo which echoes back whatever it hears, so this is a good place to write some test code. You may even find a rudimentary chat implementation.

大多数 websocket 服务器都有一个演示,可以回显它听到的任何内容,因此这是编写一些测试代码的好地方。您甚至可能会找到一个基本的聊天实现。

The tricky part is to monitor the web socket server and to make sure it runs smoothly and continuously.

棘手的部分是监视 Web 套接字服务器并确保它平稳、连续地运行。

Try to test on as many browsers/devices as possible as this will decide on which websocket server implementation you choose. There are old and new protocols you have to watch out for.

尝试在尽可能多的浏览器/设备上进行测试,因为这将决定您选择哪种 websocket 服务器实现。您必须注意新旧协议。

回答by leggetter

As @zaf states you are more likely to find a standalone PHP solution - not something that runs within Apache. That said there is a apache WebSocket module.

正如@zaf 所说,您更有可能找到独立的 PHP 解决方案 - 而不是在 Apache 中运行的解决方案。也就是说有一个apache WebSocket 模块

However, the fundamental problem is that Apache wasn't built with maintaining many persistent connections in mind. It, along with PHP, is built on the idea that requests are made and responses are quickly sent back. This means that resources can very quickly be used up if you are holding requests open and you're going to need to look into horizontal scaling pretty quickly.

然而,根本的问题是 Apache 的构建并没有考虑到维护许多持久连接。它与 PHP 一起建立在发出请求并快速发回响应的思想之上。这意味着如果您保持请求处于打开状态,并且您将需要很快研究水平扩展,那么资源可能会很快用完。

Personally I think you have two options:

我个人认为你有两种选择:

  1. Use an alternative realtime web technology solution and communicate between your web application and realtime web infrastructure using queues or short-lived requests (web services).
  2. Off load the handling of persistent connections and scaling of the realtime web infrastructure to a realtime web hosted service. I work for Pusherand we fall into this category.
  1. 使用替代的实时 Web 技术解决方案,并使用队列或短期请求(Web 服务)在您的 Web 应用程序和实时 Web 基础设施之间进行通信。
  2. 将持久连接的处理和实时 Web 基础设施的扩展卸载到实时 Web 托管服务。我为Pusher工作,我们属于这一类。

For both self-hosted and hosted options you can check out my realtime web tech guide.

对于自托管和托管选项,您可以查看我的实时网络技术指南

回答by ShahRokh

I introduced another websocket server: PHP Ratchet(Github).

我介绍了另一个 websocket 服务器:PHP Ratchet( Github)。

This is better and complete list of client & server side codes and browser support.

这是更好和完整的客户端和服务器端代码和浏览器支持列表。

Please check this link.

请检查此链接

回答by AlexC_JEng

Another Path is to use a dedicated websocket server.

另一个路径是使用专用的 websocket 服务器。

Try Achex Websocket Serverat www.achex.caand checkout the tutorials.

www.achex.ca试用Achex Websocket 服务器查看教程。

OR

或者

If you really want Apache, check out Apache Camel. (but you have to set it up and its a bit more complicated than achex server) http://camel.apache.org/websocket.html

如果您真的想要 Apache,请查看Apache Camel。(但你必须设置它,它比 achex 服务器复杂一点) http://camel.apache.org/websocket.html