简体   繁体   English

Web套接字问题

[英]Web Sockets questions

I have a couple of questions concerning Web Sockets . 我有几个关于Web套接字的问题。

The latest Firefox 4.0 nightlies support Web Sockets. 最新的Firefox 4.0 nightlies支持Web套接字。 So does Webkit (Chrome 4 + Safari 4/5). Webkit也是如此(Chrome 4 + Safari 4/5)。 Internet Explorer 9 is supposed to feature Web Sockets at some point according to Microsoft (before the stable release). 根据Microsoft(在稳定版本之前),Internet Explorer 9应该在某些时候具有Web套接字。

Anyway, my questions are: 无论如何,我的问题是:

  • I am building a JavaScript admin interface to manage a website. 我正在构建一个JavaScript管理界面来管理网站。 Should I use Web Sockets for the client-server communication instead of XMLHttpRequest if I told you that I do not need to care about browser compatibility? 我应该使用网络套接字的客户端服务器通信,而不是XMLHttpRequest的,如果我告诉你,我不需要在意浏览器的兼容性?

  • Would Web Sockets result in faster save, deletion and update calls compared to the usual situation with XMLHttpRequest? 与XMLHttpRequest的常见情况相比,Web套接字是否会导致更快的保存,删除和更新调用? Would the requests be more instant ? 请求会更加即时吗?

  • I am aware of the HTML5's navigator.online and window.addEventListener('offline', ...) , but with Web Sockets (upon connection loss), am I able to detect connection issues more accurately and faster? 我知道HTML5的navigator.onlinewindow.addEventListener('offline', ...) ,但是使用Web套接字(连接丢失时),我能更准确,更快地检测连接问题吗? I mean, when I turn off my Internet connection, or block it with my firewall, Firefox still seems to state that navigator.online is true . 我的意思是,当我关闭我的互联网连接,或用我的防火墙阻止它时,Firefox似乎仍然表明navigator.online真的 With Web Sockets, it seems, the connection to the server will be lost instantly, thus, I can detect connection problems more accurately? 使用Web套接字,似乎与服务器的连接将立即丢失,因此,我可以更准确地检测连接问题?

  • Can I support Web Sockets server-side with pure PHP, so, that the code is portable with other web servers ( no need to install any Apache modules or do other customization). 我可以支持Web套接字服务器端采用 PHP,因此,该代码是便携与其他Web服务器( 无需安装任何Apache模块或做其他定制)。 I would like to distribute the software to a couple of places without having to ask people to install all sorts of modules to their HTTPD or so. 我想将软件分发到几个地方,而不必要求人们将各种模块安装到他们的HTTPD左右。

I wish you can answer to as many of those questions as possible. 我希望你能尽可能多地回答这些问题。 I am really interested in answers. 我对答案很感兴趣。

I am building a JavaScript admin interface to manage a website. 我正在构建一个JavaScript管理界面来管理网站。 Should I use Web Sockets for the client-server communication instead of XMLHttpRequest if I told you that I do not need to care about browser compatibility? 如果我告诉您我不需要关心浏览器兼容性,我应该使用Web套接字进行客户端 - 服务器通信而不是XMLHttpRequest吗?

It appears to me that you want to use WebSockets just for the sake of it. 在我看来,你只是为了它而想要使用WebSockets。 The main reason to use WebSockets is when you want to push data from the server to the client. 使用WebSockets的主要原因是您希望将数据从服务器推送到客户端。 If your application does not need this, you should probably not use WebSockets. 如果您的应用程序不需要这个,您可能不应该使用WebSockets。

Would Web Sockets result in faster save, deletion and update calls compared to the usual situation with XMLHttpRequest? 与XMLHttpRequest的常见情况相比,Web套接字是否会导致更快的保存,删除和更新调用? Would the requests be more instant? 请求会更加即时吗?

You could probably save some time on both ends (client and server) due to the absence of headers. 由于没有标题,您可以在两端(客户端和服务器)节省一些时间。 But the gain is probably pretty small. 但收益可能很小。

with Web Sockets (upon connection loss), am I able to detect connection issues more accurately and faster? 使用Web套接字(在连接丢失时),我能够更准确,更快地检测连接问题吗?

Yes, an event will fire instantly when the WebSocket closes. 是的,当WebSocket关闭时,事件将立即触发。 Alternatives would be long-polling or periodic XHRs. 替代方案是长轮询或定期XHR。 Or event client-side storage. 或事件客户端存储。

Can I support Web Sockets server-side with pure PHP, so, that the code is portable with other web servers 我是否可以使用纯PHP支持服务器端的Web套接字,因此代码可以与其他Web服务器一起移植

First I suggest you read through this . 首先,我建议你通读 WebSockets don't work very well in a synchronous way. WebSockets在同步方式下不能很好地工作。 PHP and apache don't work very well in an asynchronous way. PHP和apache以异步方式不能很好地工作。 Although there are some implementations , many of them are outdated. 尽管存在 一些 实现 ,很多都是过时的。 I personally would use an other language for this, such as ruby , python , java or server-side javascript . 我个人会使用其他语言,例如rubypythonjava服务器端javascript Simply because the languages have better support for the asynchronous model and the WebSocket implementations are more sophisticated. 仅仅因为语言对异步模型有更好的支持,而WebSocket实现更复杂。

The WebSocket protocol is currently still a draft, it can change. WebSocket协议目前仍然是草案,它可以改变。 Just like it did a few weeks ago . 就像几周前那样 So your code may very well break. 所以你的代码可能会破坏。

My advice is: Do not use WebSockets just for the sake of it. 我的建议是:不要仅仅为了它而使用WebSockets。 If you have a real-time event-driven application, then it is probably the right choice. 如果您有一个实时的事件驱动应用程序,那么它可能是正确的选择。 Make sure you understand what WebSockets are for and what it takes on the server side, also in terms of event-driven applications. 确保您了解WebSockets的用途以及服务器端所需的内容,以及事件驱动的应用程序。 Don't use it for anything production, it's way too fragile. 不要将它用于任何生产,它太脆弱了。

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

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