WebSocket的优势AJAX与PHP的简单应用 [英] Websocket advantages over AJAX for simple applications with PHP

查看:141
本文介绍了WebSocket的优势AJAX与PHP的简单应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用了一下AJAX与PHP对于像提交表单和我最近开始寻找到的WebSockets。我跟着本教程,以了解的基础知识。从我收集,的WebSockets保持连接打开,而AJAX打开和关闭的请求。

I've used a bit of AJAX with PHP for things like submitting forms and I've recently started looking into websockets. I followed this tutorial to understand the basics. From what I gather, websockets keep the connection open whereas AJAX opens and closes a request.

我的问题是这样做的WebSockets提供任何优势AJAX如果你只是提交表单或类似auto_complete简单的任务(其中有一个jQuery插件呢)?也许本教程是不是最大的,但它好像有一个更多的code参与让WebSockets的工作(至少在PHP)赫克不仅仅是一个简单的AJAX调用(或使用jQuery捆绑了它) 。我读过的一些地方,这是一个有点快,但如果我工作的东西,是不是收到请求吨,将它真正发挥作用?纠正我,如果我错了,但不是所有的浏览器都支持WebSockets的两种,对吧?

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)? 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). 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?

推荐答案

的WebSockets有两方面的优势。

Websockets have two advantages.

  1. 它们具有少得多的开销,这导致了更好的网络性能

  1. they have much less overhead, which results in a better network performance

他们让服务器发送该客户端还没有明确地请求的数据。

they allow the server to send data which the client hasn't explicitely requested.

第二个是最重要的优势。

The second one is the most important advantage.

在AJAX,一切服务器发送必须响应客户端一个previous请求,每次请求只能使用一次回答。但在许多应用中,尤其是多用户应用,事件发生在服务器上与这些事件必须立即推送到客户端。有在AJAX变通办法,如延迟回答一个请求,直到有一些报道(长轮询),但这些都是非常脏。这就是为什么有WebSockets的。用的websocket连接时,服务器可以发送消息给当它要和它希望作为经常的客户端,而无需等待来自客户端的请求。

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. 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. That's why there are 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.

不过遗憾的是WebSockets的也有缺点:

But unfortunately WebSockets also have disadvantages:

  1. 在他们没有很好地支持Web开发框架(尚未!)
  2. 不是所有的浏览器都支持它(但大多数桌面浏览器已经做
  3. 许多代理和反向代理服务器不能中继的WebSocket流量(尚未!)

这篇关于WebSocket的优势AJAX与PHP的简单应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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