有没有什么好的理由在浏览器外使用Websockets? [英] Are there any good reasons to use Websockets outside the browser?

查看:124
本文介绍了有没有什么好的理由在浏览器外使用Websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Websockets设计用于在浏览器中进行快速双向通信。假设您可以控制服务器和本地客户端(例如iOS或Mac应用程序),有没有什么好的理由或情况通过websocket而不是使用HTTP库?

解决方案

我要回答几个不同的问题,希望最终回答您的问题:




  • 有没有理由从客户端而不是HTTP使用WebSockets?




    • WebSockets是双向,全双工,相比HTTP的开销。


    • 与HTTP / AJAX / COMET相比,延迟和开销较低的部分是您不必为每个请求重新建立连接。 / p>




      • 初始WebSockets握手是HTTP服务器友好的安全机制)。


      • WebSockets的另一个好处是它是一个简单的WebSockets连接,框架协议,允许应用程序专注于有用的功能,而不必处理自己的框架和缓冲。


      • 它相当容易添加WebSockets支持现有的TCP套接字服务器或代理,例如 wsproxy 。从服务器的角度来看,WebSockets是一个胜利,因为他们可以通过独立的客户端或通过浏览器访问。这意味着将有越来越多的服务,目前基于TCP套接字,将通过WebSockets暴露。因此,为客户添加WebSockets支持现在将在未来得到回报。




    Websockets were designed for fast bi-directional communication in the browser. Assuming you have control over the server and a native client (say an iOS or Mac app), are there any good reasons or situations to communicate over a websocket instead of using an HTTP library?

    解决方案

    I'm going to answer a couple of different questions that hopefully ends up answering your question:

    • Is there a reason to use WebSockets from a client rather than HTTP? Yes.

      • WebSockets is bi-directional, full-duplex, low-latency and low-overhead compared to HTTP.

      • Part of the lower latency and overhead compared to HTTP/AJAX/COMET is that you don't have to re-establish the connection for each request.

    • Is there a reason to use WebSockets from a client rather than raw sockets? Yes.

      • The initial WebSockets handshake is HTTP server friendly (and has some origin and hash exchange safety mechanisms). It allows web servers to easily be updated to support redirect or proxy for WebSockets connections to the real WebSockets server application.

      • Another benefit of WebSockets is that it is a framed protocol which allows the application to focus on useful functionality without having to deal with its own framing and buffering.

      • Its fairly easy to add WebSockets support to existing TCP socket servers or proxy via something like wsproxy. Disclaimer: I made wsproxy.

      • From a server perspective WebSockets is a win because they can be accessed via standalone clients or via browsers. This means there are going to be more and more services that are currently TCP socket based that will be exposed via WebSockets. So adding WebSockets support to a client now will pay off in the future.

    这篇关于有没有什么好的理由在浏览器外使用Websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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