在 Apache 上设置 websocket? [英] Setting up a websocket on Apache?

查看:31
本文介绍了在 Apache 上设置 websocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在对 websockets 进行一些研究,但我有几个问题似乎找不到明确的答案:

So I'm doing some research on websockets, and I have a few questions I can't seem to find a definitive answer for:

  • 如何在我的 Linux 服务器上设置网络套接字?有 Apache 模块吗?我必须使用第 3 方 PHP 代码或类似代码吗?

  • How can I set up a web socket on my Linux server? Is there an Apache module? Would I have to use 3rd-party PHP code or similar?

除了浏览器兼容性之外,我应该注意问题 1 中描述的方法有哪些缺点吗?

Are there any kinds of drawbacks to the method described in question 1 that I should be aware of other than browser compatibility?

如何将我的 websocket 安装升级"到安全的 websocket 安装(ws://到 wss://)?如果我的 Apache 服务器上已经设置了 SSL,这会变得更容易还是更困难?

How could I "upgrade" my websocket installation to a secure websocket installation (ws:// to wss://)? Would this be made easier or more difficult if SSL was already set up on my Apache server?

除了 JavaScript 之外,还有其他语言可以用来连接到我的网络套接字吗?

Is there any language I could use to connect to my web socket other than JavaScript?

Web 套接字的默认请求方法是什么?

What is the default request method for a web socket?

推荐答案

我无法回答所有问题,但我会尽力而为.

I can't answer all questions, but I will do my best.

如您所知,WS 只是一个持久的全双工 TCP 连接,带有帧消息,其中初始握手类似于 HTTP.您需要一些服务器来侦听传入的 WS 请求并将处理程序绑定到它们.

As you already know, WS is only a persistent full-duplex TCP connection with framed messages where the initial handshaking is HTTP-like. You need some server that's listening for incoming WS requests and that binds a handler to them.

现在使用 Apache HTTP Server 可能是可能的,我已经看到了一些示例,但没有官方支持,而且它变得复杂.阿帕奇会做什么?你的处理程序在哪里?有一个模块可以将传入的 WS 请求转发到外部共享库,但这对于使用 WS 的其他出色工具来说不是必需的.

Now it might be possible with Apache HTTP Server, and I've seen some examples, but there's no official support and it gets complicated. What would Apache do? Where would be your handler? There's a module that forwards incoming WS requests to an external shared library, but this is not necessary with the other great tools to work with WS.

WS 服务器趋势现在包括:Autobahn (Python) 和 Socket.IO(Node.js = 服务器上的 JavaScript).后者还支持其他黑客持久"连接,如长轮询和所有 COMET 内容.还有其他鲜为人知的 WS 服务器框架,例如 Ratchet(如果您只熟悉 PHP).

WS server trends now include: Autobahn (Python) and Socket.IO (Node.js = JavaScript on the server). The latter also supports other hackish "persistent" connections like long polling and all the COMET stuff. There are other little known WS server frameworks like Ratchet (PHP, if you're only familiar with that).

在任何情况下,您都需要监听一个端口,当然该端口不能与您机器上已经运行的 Apache HTTP Server 相同(默认值 = 80).您可以使用 8080 之类的东西,但即使这个特定的选择很受欢迎,某些防火墙可能仍会阻止它,因为它不应该是 Web 流量.这就是为什么许多人选择 443,这是HTTP Secure 端口,出于显而易见的原因,防火墙不会阻止该端口.如果您不使用 SSL,则可以对 HTTP 使用 80,对 WS 使用 443.WS 服务器不需要是安全的;我们只是在使用端口.

In any case, you will need to listen on a port, and of course that port cannot be the same as the Apache HTTP Server already running on your machine (default = 80). You could use something like 8080, but even if this particular one is a popular choice, some firewalls might still block it since it's not supposed to be Web traffic. This is why many people choose 443, which is the HTTP Secure port that, for obvious reasons, firewalls do not block. If you're not using SSL, you can use 80 for HTTP and 443 for WS. The WS server doesn't need to be secure; we're just using the port.

编辑:根据 Iharob Al Asimi 的说法,上一段是错误的.我没有时间调查这个,所以请看他的作品了解更多细节.

Edit: According to Iharob Al Asimi, the previous paragraph is wrong. I have no time to investigate this, so please see his work for more details.

关于协议,如维基百科所示,它看起来像这样:

About the protocol, as Wikipedia shows, it looks like this:

客户端发送:

GET /mychat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
Origin: http://example.com

服务器回复:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

并保持连接有效.如果您可以实现这种握手和基本的消息框架(用一个描述它的小标题封装每条消息),那么您就可以使用任何您想要的客户端语言.JavaScript 仅用于 Web 浏览器,因为它是内置的.

and keeps the connection alive. If you can implement this handshaking and the basic message framing (encapsulating each message with a small header describing it), then you can use any client-side language you want. JavaScript is only used in Web browsers because it's built-in.

如您所见,默认的请求方法"是初始 HTTP GET,尽管这并不是真正的 HTTP,并且在此握手后丢失了与 HTTP 的所有共同点.我猜服务器不支持

As you can see, the default "request method" is an initial HTTP GET, although this is not really HTTP and looses everything in common with HTTP after this handshaking. I guess servers that do not support

Upgrade: websocket
Connection: Upgrade

将回复错误或页面内容.

will reply with an error or with a page content.

这篇关于在 Apache 上设置 websocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆