有没有什么好的理由在浏览器外使用Websockets?

编程入门 行业动态 更新时间:2024-10-14 06:19:20
本文介绍了有没有什么好的理由在浏览器外使用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?

本文发布于:2023-11-22 01:06:45,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有没有什么   浏览器   理由   Websockets

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!