Skip to content

WebSocket

一句话解释

WebSocket 是 communication protocol 那个 provides persistent, bidirectional 连接之间浏览器 and 服务器.

用大白话说

WebSocket is a protocol that allows a browser and server to maintain an open, bidirectional connection. Unlike HTTP (where the client must request each time), WebSocket lets either side send messages at any time — ideal for real-time applications.

WebSocket starts as an HTTP request that "upgrades" to a WebSocket connection. Once established, the connection stays open, and both client and server can send messages whenever they want. This avoids the overhead of repeated HTTP requests and enables true real-time communication.

Common use cases include chat applications, live sports updates, collaborative editing, multiplayer games, and real-time dashboards. The browser API is simple: const ws = new WebSocket('wss://...'), then listen for onmessage events and call ws.send() to send data.

示例

javascript
const ws = new WebSocket("wss://chat.example.com");
ws.onmessage = (event) => {
  console.log("Received:", event.data);
};
ws.send("Hello server!");

WebSocket 连接那个接收消息 and 发送 one. 连接 stays 打开 for 真实-时间 communication.

相关术语

相关资源

相关课程

  • HTTP

    HTTP and related protocols.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表