Skip to Content
学习 API 风格
book

学习 API 风格

by Lukasz Dynowski, Marcin Dulak
July 2025
Intermediate to advanced
414 pages
5h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 学习 API 风格

第 10 章 WebSocket WebSocket

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

基于 HTTP 的API 遵循请求-响应模式运行,即客户端向服务器发送请求并等待相应的响应。在HTTP 中,如果客户端没有事先发出请求,服务器就无法启动连接或向客户端发送响应。 服务器可以使用服务器端事件HTTP 流等方法多次向客户端 "推送 "信息,但这些方法仍然需要客户端的初始请求。或者,客户端可以轮询服务器(发送请求)以获取新信息,,使用短轮询(为每个请求建立新的 TCP 连接)或长轮询(为多个请求重复使用单个长期 TCP 连接)。

WebSocket 的目标是提供同步双向通信。在 WebSocket 中,客户端和服务器都可以相互独立地发送信息,而无需遵守请求-响应模式。 这是通过将 HTTP 转换为基于 TCP 的自定义协议(WebSocket)来实现的,该协议支持全双工传输模式。

在讨论 WebSocket 时,人们经常提到异步通信 。1 WebSocket 本身并不是异步的。2 WebSocket 客户端和服务器的同步实现是存在的、3但异步使用 WebSocket 有很多好处,如有效管理多个连接和提高响应速度。4

在本章中,您将了解 WebSocket 协议、其开放握手过程以及如何在后台和前台实现 WebSocket API。 您将以安全的方式使用 WebSocket,通过天气通知警报扩展我们的 WFS。 在本章结束时,您将掌握记录 WebSocket API 的技能,并了解其优缺点。

WebSocket 和 WebSocket API

WebSocket 是 的一种通信协议,它的创建是为了实现双向通信,而无需依赖发送多个 HTTP 请求(如 XMLHttpRequest长时间轮询的方法进行双向通信。5 WebSocket 协议于 2011 年由RFC 6455标准化,通过单个长期 TCP 连接提供双向通信通道。

图 10-1显示了使用 HTTP 和 WebSocket 协议时客户端和服务器之间消息数据流的不同。 在HTTP/1 中,消息每次只能从一个方向从发送者流向接收者(从客户端流向服务器,反之亦然)。而在 WebSocket 中,消息可以同时在客户端和服务器之间双向流动。

Comparison of client-server message exchange using HTTP and WebSocket
图 10-1. HTTP 和 WebSocket 数据流的比较

值得一提的是,虽然 HTTP 用于最初的开局握手,但 WebSocket 协议之后并不使用 HTTP。此外,WebSocket 协议的设计还允许非 HTTP 握手。

要在网络浏览器中使用 WebSocket 协议,网络浏览器要根据WebSocket 网络接口描述语言(WebSockets Web IDL实现 WebSocket API。6

WebSocket 开启握手

要在上使用 WebSocket 协议在客户端和服务器之间建立双向通信 ,客户端和服务器需要经过三步协议变更过程即图 10-2 所示的 WebSocket 开启握手过程

图 10-2. WebSocket 连接概述

打开握手过程如下:

  1. 客户端发送包含UpgradeConnectionSec-WebSocket-Key 标头的 HTTP ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

Aurélien Géron
学习 React,第二版

学习 React,第二版

Alex Banks, Eve Porcello
CSS:权威指南,第 5 版

CSS:权威指南,第 5 版

Eric Meyer, Estelle Weyl

Publisher Resources

ISBN: 9798341664036