被动套接字 主动套接字_了解网络套接字及其可能性

被动套接字 主动套接字

介绍 (Introduction)

As users of web applications, we are accustomed to being able to quickly and seamlessly interact with others via chat, text messaging or some other form of digital communication method. What most consumers think is some form of arcane internet magic, we as programmers know there is a deeper and much richer explanation of what this web technology is, what it does, and how it works under the hood.

作为Web应用程序的用户,我们习惯于能够通过聊天,文本消息传递或某种其他形式的数字通信方法与他人快速无缝地进行交互。 大多数消费者认为这是一种神秘的互联网魔术,作为程序员,我们知道对这种Web技术是什么,它做什么以及它在幕后如何工作有更深入,更丰富的解释。

你好WebSockets (Hello WebSockets)

Historically, for programmers that were looking to build real-time web applications, it used to be the case that you would have built your application to send the messages from the client-side of the sending party to the server and then send this response from the server to the client-side of the receiving party. With the WebSocket API (WebSockets), we eliminate this requirement and introduce an endless world of possibilities for web developers to harness bi-directional (two way) communication between the client and the server. Now that we’ve introduced WebSockets, let’s understand how WebSockets actually work.

从历史上看,对于正在寻求构建实时Web应用程序的程序员来说,过去通常是先构建应用程序以将消息从发送方的客户端发送到服务器,然后从服务器到接收方的客户端。 借助WebSocket API(WebSockets),我们消除了这一要求,并为Web开发人员引入了无穷无尽的可能性,使Web开发人员可以利用客户端与服务器之间的双向(双向)通信。 现在,我们已经介绍了WebSockets,让我们了解WebSockets的实际工作原理。

From MDN: The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to hit the server for a reply (https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).

MDN:WebSocket API 是一种先进的技术,使得它可以打开用户的浏览器和服务器之间的双向交互通信会话。 使用此API,您可以将消息发送到服务器并接收事件驱动的响应,而不必单击服务器进行答复( https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API )。

In essence, Websockets allow a client and a server to maintain a persistent connection with each other as opposed to requiring the standing request and response event loop that comes out of HTTP protocol. Instead of having a client-side make a request to the server, and have the server respond to fulfill the request, WebSockets utilise HTTP initially and then keep the TCP connection alive so that it avoids the delays that typically plagues the HTTP request and response cycle and instead use TCP connections to get a real-time response with little delay.

从本质上讲,Websocket允许客户端和服务器之间保持持久连接,而不是需要来自HTTP协议的常规请求和响应事件循环。 WebSockets最初没有使用客户端向服务器发出请求,而是让服务器响应以完成请求,而是首先使用HTTP,然后保持TCP连接处于活动状态,从而避免了通常困扰HTTP请求和响应周期的延迟。而是使用TCP连接来获得几乎没有延迟的实时响应。

With TCP connections, Websockets create an initial handshake between the client who asks to open a WebSocket connection and the server who would then agree to this handshake. Once this handshake is created, the handshake persists (visually continues to be held between the server and the client — see image below) and this persistent TCP connection (handshake) acts as a communication bridge that then allows data (in the form of messages) to flow from the client to the server and back in a real-time fashion.

使用TCP连接,Websockets在请求打开WebSocket连接的客户端和随后同意该握手的服务器之间创建初始握手。 创建此握手后,握手将继续(视觉上继续保持在服务器和客户端之间,请参见下图),并且此持久的TCP连接(握手)充当通信桥,然后允许数据(以消息的形式)从客户端流向服务器并以实时方式返回。

让我们获取技术 (Let’s get Technical)

Without WebSockets, a user would use HTTP long-polling. With HTTP long-polling the client continually polls the server and requests for new information. The server will then send data to the client once new data is available and thus for the client to receive this new data it must continue to reach out to the server to see if the state of the database has changed, i.e. if there is new information to show. This continues ongoing and the constant checking of the server for new information requires the event loop to tax the server infrastructure heavily and with multiple clients from many users hitting the server, this will tax the server and require significant amounts of data to be sent to all the different clients making requests.

没有WebSockets,用户将使用HTTP长轮询。 使用HTTP长轮询,客户端可以连续轮询服务器并请求新信息。 然后,一旦有新数据可用,服务器将向客户端发送数据,因此对于客户端来说,要接收此新数据,它必须继续与服务器联系,以查看数据库状态是否已更改,即是否有新信息。显示。 这将继续进行,并且不断检查服务器是否有新信息,这需要事件循环来加重服务器基础结构的负担,并且有许多用户的多个客户端命中该服务器,这将使服务器负担很多,并需要将大量数据发送给所有服务器。不同的客户提出请求。

Image for post
https://medium.com/front-end-weekly/what-are-websockets-7bf0e2e1af2). https://medium.com/front-end-weekly/what-are-websockets-7bf0e2e1af2 )。

On the other hand, with WebSockets, the initial handshake that we described previously means that a persistent TCP connection is made from each client to the server and this will allow data to flow from the client to server and vice versa in a real-time fashion. The process starts with an initial GET HTTP request from the client to the server.

另一方面,对于WebSockets,我们先前描述的初始握手意味着从每个客户端到服务器建立持久的TCP连接,这将允许数据以实时的方式从客户端流向服务器,反之亦然。 该过程从客户端到服务器的初始GET HTTP请求开始。

Once the initial request is made and the handshake is accepted, the TCP connection remains open and unlike the HTTP long polling method, a new request to the server from the client does not need to be made each time, so the event loop does not need to process for each continual ongoing request from each client.

发出初始请求并接受握手后,TCP连接将保持打开状态,并且与HTTP长轮询方法不同,不需要每次都从客户端向服务器发出新请求,因此不需要事件循环处理来自每个客户端的每个连续正在进行的请求。

Rather, the server will just send any new information to the client and the client will render this new information as it needs to. It automatically defaults to passing new information from the server up to the client eliminating the need for a high latency, infrastructure taxing request that HTTP long polling would perform.

相反,服务器将仅向客户端发送任何新信息,客户端将根据需要呈现此新信息。 它默认自动将新信息从服务器传递到客户端,从而消除了HTTP长轮询将执行的高延迟,基础结构征税请求。

何时使用WebSockets (When to use WebSockets)

For those programmers keen to dive in and add WebSockets to their own applications. Let’s take a moment to assess what type of applications work best with WebSockets. As mentioned previously, the web applications that do best are real-time or near real-time applications but what exact characteristics does that look like?

对于那些热衷于学习并将WebSocket添加到自己的应用程序中的程序员。 让我们花一点时间来评估哪种类型的应用程序最适合WebSockets。 如前所述,最出色的Web应用程序是实时或接近实时的应用程序,但是它们的确切特征是什么?

Let’s take a look at the Windows Developer’s blog that explains when you may need to use WebSockets over traditional HTTP:

让我们看一下Windows Developer的博客,该博客解释了何时可能需要通过传统HTTP使用WebSocket:

快速React时间 (Fast Reaction Time)

When a client needs to react quickly to a change (especially one it cannot predict), a WebSocket may be best. Consider a chat application that allows multiple users to chat in real-time. If WebSockets are used, each user can both send and receive messages in real-time. WebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received.

当客户端需要对更改做出快速React(尤其是无法预测的更改)时,WebSocket可能是最好的。 考虑允许多个用户实时聊天的聊天应用程序。 如果使用WebSocket,则每个用户都可以实时发送和接收消息。 与REST相比,WebSocket允许更高的效率,因为它们不需要为发送和接收的每个消息分配HTTP请求/响应开销。

持续更新 (Ongoing Updates)

When a client wants ongoing updates about the state of the resource, WebSockets are generally a good fit. WebSockets are a particularly good fit when the client cannot anticipate when a change will occur and changes are likely to happen in the short term.

当客户端想要持续更新资源状态时,WebSocket通常很合适。 当客户端无法预测何时会发生更改并且短期内可能发生更改时,WebSockets特别适合。

临时消息 (Ad-hoc Messaging)

The WebSocket protocol is not designed around request-response. Messages may be sent from either end of the connection at any time, and there is no native support for one message to indicate it is related to another. This makes the protocol well suited to “fire and forget” messaging scenarios and poorly suited for transactional requirements that require a response from one party before a response from the other party can fire.

WebSocket协议不是围绕请求响应设计的。 可以随时从连接的任一端发送消息,并且对一条消息的本地支持不表示该消息与另一条消息有关。 这使得该协议非常适合“即发即弃”的消息传递场景,而不适用于需要在一方发出响应之前触发一方响应的事务需求。

小负载的高频消息传递 (High-Frequency Messaging with Small Payloads)

The WebSocket protocol offers a persistent connection to exchange messages. This means that individual messages don’t incur any additional tax to establish the transport. Taxes such as establishing SSL, content negotiation, and exchange of bulky headers are imposed only once when the connection is established.

WebSocket协议提供了持久的连接来交换消息。 这意味着单独的邮件无需为建立传输而支付任何额外的税。 建立连接时,仅建立一次税收,例如建立SSL,内容协商和交换大标题。

As WebSockets were designed specifically for long-lived connection scenarios, they avoid the overhead of establishing connections and sending HTTP request/response headers, resulting in a significant performance boost.

由于WebSocket是专门为长期连接方案设计的,因此它们避免了建立连接和发送HTTP请求/响应标头的开销,从而显着提高了性能。

Quoted from: (https://blogs.windows.com/windowsdeveloper/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0/)

引用自:( https://blogs.windows.com/windowsdeveloper/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0/ )

If the characteristics of your web application fall in line with the requirements listed above then consider some of these popular WebSocket libraries that can be integrated into your application.

如果您的Web应用程序的特性符合上述要求,那么请考虑可以将这些流行的WebSocket库集成到您的应用程序中。

自己的图书馆 (A Library of Your Own)

Image for post
https://www.traveller.com.au/content/dam/images/g/s/2/8/9/w/image.gallery.galleryLandscape.620x414.gs94jd.png/1477354626702.jpg) https://www.traveller.com.au/content/dam/images/g/s/2/8/9/w/image.gallery.galleryLandscape.620x414.gs94jd.png/1477354626702.jpg )

When looking to use the WebSocket API, we want to look for useful libraries that work with WebSockets and our Server Side language. For our example, we use Node.js, the popular Javascript runtime and will reference examples of JS libraries found in the Node.js ecosystem.

当寻求使用WebSocket API时,我们想寻找与WebSockets和我们的服务器端语言一起使用的有用的库。 对于我们的示例,我们使用流行的Javascript运行时Node.js,并将引用在Node.js生态系统中找到的JS库的示例。

Here are three popular WebSocket JS libraries that provide the functionalities that implement WebSockets that you may use for your real-time application:

以下是三个流行的WebSocket JS库,这些库提供实现可用于实时应用程序的WebSocket的功能:

1. socket.io (1. socket.io)

A widely popular and used WebSocket wrapper/API with over 43K stars. Consisting of a Node.js server and a Javascript client library, socket.io provides reliability for handling proxies and load balancers as well as personal firewall and antivirus software and even supports binary streaming. Here is the link.

具有超过43K个星的广泛流行和使用的WebSocket包装器/ API。 socket.io 由Node.js服务器和 Javascript客户端库组成 ,可为处理代理和负载平衡器以及个人防火墙和防病毒软件提供可靠性,甚至支持二进制流。 这是 链接

2. ws (2. ws)

With 9k stars WS is a popular WebSocket is a client/server library for Node.js that helps to manage things under the WebSocket protocol. As the WebSocket protocol is natively supported in most major browsers, some choose to work with WebSocket directly without using socket.io on top. Here is the link.

WS是9k星,它是流行的WebSocket,它是Node.js的客户端/服务器库,可帮助管理WebSocket协议下的内容。 由于大多数主流浏览器都原生支持WebSocket协议,因此某些浏览器选择直接使用WebSocket,而无需在顶部使用socket.io。 这是 链接

3. sockjs (3. sockjs)

At 6k stars sockjs is a browser JavaScript library that provides a WebSocket-like object with a cross-browser Javascript API which creates a low latency, full duplex, cross-domain communication between the browser and the web server. It follows the HTML5 WebSocket API and only when it fails it falls back to browser specific protocols. Here is the link.

sockjs是6k星,是一个浏览器JavaScript库,它为类似WebSocket的对象提供跨浏览器Javascript API,该API在浏览器和Web服务器之间创建低延迟,全双工,跨域通信。 它遵循HTML5 WebSocket API,并且只有在失败时才回退到浏览器特定的协议。 这是 链接

Quoted from: (https://blog.bitsrc.io/8-node-js-web-socket-libraries-for-2018-818e7e5b67cf)

引用自:( https://blog.bitsrc.io/8-node-js-web-socket-libraries-for-2018-818e7e5b67cf )

Now that we have three JS libraries that can help implement WebSockets for your application, let’s take a closer look at the managed services that can be used with these WebSocket libraries that can help supercharge your application in real-time and make building even easier. Pardon the pun.

现在,我们有了三个可以帮助您为应用程序实现WebSocket的JS库,下面让我们仔细研究可与这些WebSocket库一起使用的托管服务,这些服务可以帮助您实时增强应用程序的工作并使构建更加容易。 双关语。

更快的解决方案 (A quicker solution)

For those of us that are focussed on implementing the solution with WebSockets and are not keen to write the infrastructure code or deal with deployment of the WebSocket themselves, you may consider an alternative to a JS library like socket.io and perhaps utilise a quicker more efficient solution such as a managed solution like the popular Pusher API.

对于那些专注于通过WebSockets实现解决方案而又不希望自己编写基础结构代码或不愿自行处理WebSocket部署的人,您可以考虑使用诸如Socket.io之类的JS库的替代方法,并且也许可以使用更快的方法。高效的解决方案,例如流行的Pusher API之类的托管解决方案。

What is Pusher API? As their website states:

什么是Pusher API? 如他们的网站所述:

Pusher is a platform that allows developers to easily build an application with realtime features as quickly as possible. Pusher specialises in building realtime and scalable infrastructures for developers and is packaged with powerful features like client events, queryable API, Pub/Sub messaging and others (https://pusher.com/tutorials/react-websockets).

Pusher是一个平台,使开发人员可以轻松,快速地构建具有实时功能的应用程序。 Pusher专门为开发人员构建实时和可伸缩的基础结构,并打包有强大的功能,例如客户端事件,可查询的API,发布/订阅消息传递和其他功能( https://pusher.com/tutorials/react-websockets )。

With Pusher, programmers can do less work to implement real-time behaviour into their application and with a service like Pusher Channels, set up all the necessary infrastructure to get your real-time application working with Web Sockets quickly and simply. Additionally, Pusher ensures that your back infrastructure server code scales efficiently, giving programmers a lesser burden when it comes to dealing with a large number of client requests that would arise when your real-time application grows in popularity.

使用Pusher,程序员可以减少工作量,以将实时行为实施到他们的应用程序中,并使用Pusher Channels之类的服务来建立所有必要的基础架构,以使您的实时应用程序快速,简单地与Web Sockets一起使用。 此外,Pusher可确保您的后台基础结构服务器代码有效地扩展,从而在处理实时应用程序日益普及时可能产生的大量客户端请求时,减轻了程序员的负担。

结论 (Conclusion)

Image for post

Image from (https://images.unsplash.com/photo-1565049981953-379c9c2a5d48?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80)

图片来自( https://images.unsplash.com/photo-1565049981953-379c9c2a5d48?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80 )

Let’s summarize the benefits WebSockets give to programmers looking to build near real-time and real-time applications one last time!

让我们总结一下WebSockets给希望最后一次构建近实时和实时应用程序的程序员带来的好处!

WebSockets are a game changer in client/server web technology. By establishing a ‘handshake’, a persistent TCP connection is made between the client and server that allows for two way messaging to occur between the client and the server that has very low latency and much less technical overhead then the traditional HTTP request response cycle.

WebSockets是客户端/服务器Web技术中的游戏规则改变者。 通过建立“握手”,在客户端和服务器之间建立了持久的TCP连接,从而允许客户端和服务器之间以两种方式进行消息传递,与传统的HTTP请求响应周期相比,该方法具有极低的延迟和更少的技术开销。

This persistent TCP connection allows for real-time applications to be built and used by the end user in a code efficient and real-time manner all the while allowing programmers to not have to spend significant resources on writing infrastructure code to handle the real-time behaviour or with deployment.

这种持久的TCP连接允许最终用户以代码高效且实时的方式构建和使用实时应用程序,同时使程序员不必花费大量资源来编写基础结构代码来处理实时信息。行为或部署。

For those that are keen to understand more about WebSockets and dive a little deeper, please refer to the WebSocket API documentation (https://html.spec.whatwg.org/multipage/web-sockets.html) and check out the Mozilla documentation on WebSockets (https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).

对于那些渴望了解更多有关WebSockets并进一步深入研究的人,请参考WebSocket API文档( https://html.spec.whatwg.org/multipage/web-sockets.html )并查看Mozilla文档在WebSockets( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket )上。

Thanks for reading and have fun with WebSockets!

感谢您的阅读,并与WebSockets玩得开心!

https://sookocheff.com/post/networking/how-do-websockets-work/

https://sookocheff.com/post/networking/how-do-websockets-work/

https://blogs.windows.com/windowsdeveloper/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0/

https://blogs.windows.com/windowsdeveloper/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0/

https://pusher.com/websockets

https://pusher.com/websockets

翻译自: https://medium.com/@williamting/understanding-websockets-and-its-possibilities-ef4f5b48973f

被动套接字 主动套接字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值