简体   繁体   English

Websocket与AJAX相比,对于使用PHP的简单应用程序的优势

[英]Websocket advantages over AJAX for simple applications with PHP

I've used a bit of AJAX with PHP for things like submitting forms and I've recently started looking into websockets. 我在PHP上使用了一些AJAX和诸如提交表单之类的东西,最近我开始研究websocket。 I followed this tutorial to understand the basics. 我按照本教程学习了基础知识。 From what I gather, websockets keep the connection open whereas AJAX opens and closes a request. 根据我的收集,websocket使连接保持打开状态,而AJAX打开和关闭请求。

My question is do websockets provide any advantage over AJAX if you're just submitting forms or simple tasks like auto_complete (which there's a jQuery plugin for anyway)? 我的问题是,如果您只是提交表单或诸如auto_complete之类的简单任务(无论如何都有jQuery插件),websocket是否比AJAX有任何优势? Maybe the tutorial isn't the greatest, but it seems like there's a heck of a lot more code involved to get websockets to work(at least with PHP) than just a simple AJAX call (or using jQuery which bundles it). 也许本教程并不是最出色的,但是似乎要花更多的代码才能使websockets起作用(至少与PHP兼容),而不仅仅是一个简单的AJAX调用(或使用jQuery捆绑它)。 I've read in a few places that it's a bit quicker, but if I'm working on something that isn't receiving tons of requests, will it really make a difference? 我在一些地方读到它的速度要快一些,但是如果我正在处理没有收到大量请求的内容,这真的会有所作为吗? Correct me if I'm wrong, but not all browsers support websockets either, right? 如果我错了,请纠正我,但并非所有浏览器都支持websocket,对吗?

Websockets have two advantages. Websocket具有两个优点。

  1. they have much less overhead, which results in a better network performance 它们的开销要少得多,从而带来更好的网络性能

  2. they allow the server to send data which the client hasn't explicitely requested. 它们允许服务器发送客户端未明确请求的数据。

The second one is the most important advantage. 第二个是最重要的优势。

In AJAX, everything the server sends must be the response to a previous request by the client, and every request can only be answered once. 在AJAX中,服务器发送的所有内容都必须是客户端对先前请求的响应,并且每个请求只能被回答一次。 But in many applications, especially multi-user applications, events happen on the server and these events must be pushed to the clients immediately. 但是在许多应用程序中,特别是在多用户应用程序中,事件在服务器上发生,并且这些事件必须立即推送到客户端。 There are workarounds for that in AJAX, like delaying the answer to a request until there is something to report (long-polling), but these are quite dirty. AJAX中有一些解决方法,例如将对请求的回答延迟到要报告的内容为止(长轮询),但这些方法很脏。 That's why there are Websockets. 这就是为什么有Websockets的原因。 With a websocket connection, the server can send messages to the clients when it wants and as often as it wants, without having to wait for a request from the client. 借助websocket连接,服务器可以在需要时根据需要发送消息到客户端,而不必等待客户端的请求。

But unfortunately WebSockets also have disadvantages: 但是不幸的是,WebSockets也有缺点:

  1. They aren't as well-supported by web development frameworks (yet!) 它们还没有得到Web开发框架的大力支持(但!)。
  2. Not all web browsers support it ( but most desktop browsers already do ) 并非所有的网络浏览器都支持它( 但是大多数桌面浏览器已经支持)
  3. Many proxies and reverse-proxies can't relay websocket traffic (yet!) 许多代理和反向代理无法中继websocket通信(尚未!)

Actually, AJAX and websockets are two different categories. 实际上,AJAX和websocket是两个不同的类别。 AJAX is a concept, a technique. AJAX是一种概念,一种技术。 With AJAX you can perform (as the acronym stands for) asynchronous requests, so the browser doesn't need to reload/refresh the whole page. 使用AJAX,您可以执行(缩写)代表异步请求,因此浏览器不需要重新加载/刷新整个页面。 This is good for different things, eg checking form input. 这对于不同的事情很有用,例如检查表单输入。 Websockets are a protocol, technically quite the same as http, unless the connection will not be closed after transmition. Websocket是一个协议,在技术上与http完全相同,除非传输后不会关闭连接。 This is good for things, where the webserver may need to contact the client (http can't do that), like a push service fore example (chat or mail client where you want to update the user interface even when the user does not refresh the page, or games). 这对网络服务器可能需要联系客户端(http无法执行此操作)的事情很有用,例如推送服务示例(聊天或邮件客户端,即使用户不刷新也要在其中更新用户界面)页面或游戏)。 And it kills the http overhead as the whole thing has only to be done once in the beginning. 而且它消除了HTTP开销,因为整个事情一开始只需要做一次即可。

So, their for different purposes, even if they overlap. 因此,即使它们重叠,它们也有不同的用途。 For your auto-completion I think it won't make a real difference in performance. 对于您的自动完成功能,我认为它不会对性能产生真正的影响。 And it even is a action/reaction thing, so the user types or submits (whatever) what can cause a request and the server responds. 它甚至是一个动作/反应,因此用户键入或提交(无论如何)会导致请求的内容,服务器就会做出响应。

Websockets is a powerful technology and could certainly cater for the limited use cases you;ve mentioned but there can be compatibility problems with older browsers and network intermediaries. Websockets是一项强大的技术,当然可以满足您已经提到的有限用例,但是较旧的浏览器和网络中介可能会存在兼容性问题。 In fact some folks even recommend having an HTTP fallback in case Websockets isn't supported. 实际上,有些人甚至建议使用HTTP后备,以防不支持Websockets。

Unless you have requirements that necessitate websockets, such as full duplex bi-directional communications for example, you might be better off using existing AJAX based solutions. 除非您有需要Web套接字的要求,例如全双工双向通信,否则最好使用现有的基于AJAX的解决方案。

If you have requirements to push notifications to your user interface, Websockets can be a good idea, but if you are literally looking for form submission and auto-complete, then these problems have already been solved using Ajax. 如果您有将通知推送到用户界面的要求,Websockets是个好主意,但是如果您实际上是在寻求表单提交和自动完成功能,那么使用Ajax已经解决了这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM