服务器安装
先决条件
¥Prerequisites
请确保你的系统上安装了 Node.js。当前的长期支持 (LTS) 版本是一个理想的起点,请参阅 此处。
¥Please make sure that Node.js is installed on your system. The current Long Term Support (LTS) release is an ideal starting point, see here.
至少需要 Node.js 10,旧版本不再支持。
¥At least Node.js 10 is needed, older versions are not supported anymore.
安装
¥Installation
要安装最新版本:
¥To install the latest release:
- NPM
- Yarn
- pnpm
npm install socket.io
yarn add socket.io
pnpm add socket.io
要安装特定版本:
¥To install a specific version:
- NPM
- Yarn
- pnpm
npm install socket.io@version
yarn add socket.io@version
pnpm add socket.io@version
额外的包
¥Additional packages
默认情况下,Socket.IO 使用 ws 包提供的 WebSocket 服务器。
¥By default, Socket.IO use the WebSocket server provided by the ws package.
有 2 个可选软件包可以与此软件包一起安装。这些软件包是二进制附加组件,可以改进某些操作。预构建的二进制文件可用于最流行的平台,因此你不一定需要在计算机上安装 C++ 编译器。
¥There are 2 optional packages that can be installed alongside this package. These packages are binary add-ons which improve certain operations. Prebuilt binaries are available for the most popular platforms so you don't necessarily need to have a C++ compiler installed on your machine.
bufferutil:允许有效地执行诸如屏蔽和取消屏蔽 WebSocket 帧的数据有效负载等操作。
¥bufferutil: Allows to efficiently perform operations such as masking and unmasking the data payload of the WebSocket frames.
utf-8-validate:允许有效地检查消息是否包含规范所要求的有效 UTF-8。
¥utf-8-validate: Allows to efficiently check if a message contains valid UTF-8 as required by the spec.
要安装这些软件包:
¥To install those packages:
- NPM
- Yarn
- pnpm
npm install --save-optional bufferutil utf-8-validate
yarn add --optional bufferutil utf-8-validate
pnpm add -O bufferutil utf-8-validate
请注意,这些包是可选的,如果它们不可用,WebSocket 服务器将回退到 Javascript 实现。更多信息可参见 此处。
¥Please note that these packages are optional, the WebSocket server will fallback to the Javascript implementation if they are not available. More information can be found here.
其他 WebSocket 服务器实现
¥Other WebSocket server implementations
可以使用任何公开与 ws 相同的 API(特别是 handleUpgrade 方法)的 Websocket 服务器实现。
¥Any Websocket server implementation which exposes the same API as ws (notably the handleUpgrade method) can be used.
例如,你可以使用 eiows 包,它是 uws 包(现已弃用)的分支:
¥For example, you can use the eiows package, which is a fork of the (now deprecated) uws package:
- NPM
- Yarn
- pnpm
npm install eiows
yarn add eiows
pnpm add eiows
然后使用 wsEngine
选项:
¥And then use the wsEngine
option:
const { Server } = require("socket.io");
const eiows = require("eiows");
const io = new Server(3000, {
wsEngine: eiows.Server
});
与默认实现相比,此实现显着提高了性能和内存使用率。像往常一样,请根据你自己的使用情况对其进行基准测试。
¥This implementation "allows, but doesn't guarantee" significant performance and memory-usage improvements over the default implementation. As usual, please benchmark it against your own usage.
与 µWebSockets.js
一起使用
¥Usage with µWebSockets.js
从版本 4.4.0 开始,Socket.IO 服务器现在可以绑定到 µWebSockets.js
服务器。
¥Starting with version 4.4.0, a Socket.IO server can now bind to a µWebSockets.js
server.
安装:
¥Installation:
- NPM
- Yarn
- pnpm
npm install uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
yarn add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
pnpm add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
用法:
¥Usage:
const { App } = require("uWebSockets.js");
const { Server } = require("socket.io");
const app = App();
const io = new Server();
io.attachApp(app);
io.on("connection", (socket) => {
// ...
});
app.listen(3000, (token) => {
if (!token) {
console.warn("port already in use");
}
});
杂项
¥Miscellaneous
依赖树
¥Dependency tree
服务器的基本安装包括 21 个软件包,其中 6 个由我们团队维护:
¥A basic installation of the server includes 21 packages, of which 6 are maintained by our team:
└─┬ socket.io@4.7.2
├─┬ accepts@1.3.8
│ ├─┬ mime-types@2.1.35
│ │ └── mime-db@1.52.0
│ └── negotiator@0.6.3
├── base64id@2.0.0
├─┬ cors@2.8.5
│ ├── object-assign@4.1.1
│ └── vary@1.1.2
├─┬ debug@4.3.4
│ └── ms@2.1.2
├─┬ engine.io@6.5.2
│ ├── @types/cookie@0.4.1
│ ├─┬ @types/cors@2.8.13
│ │ └── @types/node@20.4.6 deduped
│ ├── @types/node@20.4.6
│ ├── accepts@1.3.8 deduped
│ ├── base64id@2.0.0 deduped
│ ├── cookie@0.4.2
│ ├── cors@2.8.5 deduped
│ ├── debug@4.3.4 deduped
│ ├── engine.io-parser@5.2.1
│ └─┬ ws@8.11.0
│ ├── UNMET OPTIONAL DEPENDENCY bufferutil@^4.0.1
│ └── UNMET OPTIONAL DEPENDENCY utf-8-validate@^5.0.2
├─┬ socket.io-adapter@2.5.2
│ └── ws@8.11.0 deduped
└─┬ socket.io-parser@4.2.4
├── @socket.io/component-emitter@3.1.0
└── debug@4.3.4 deduped
包含了第 3 方包的类型声明,以方便 TypeScript 用户使用该库(但代价是包稍大一些)。
¥The type declarations for 3rd party packages are included, in order to ease the use of the library for TypeScript users (but at the cost of a slightly-larger package).
也可以看看:https://github.com/microsoft/types-publisher/issues/81#issuecomment-234051345
¥See also: https://github.com/microsoft/types-publisher/issues/81#issuecomment-234051345
传递版本
¥Transitive versions
engine.io
包带来了负责管理底层连接(HTTP 长轮询或 WebSocket)的引擎。也可以看看:工作原理
¥The engine.io
package brings the engine that is responsible for managing the low-level connections (HTTP long-polling or WebSocket). See also: How it works
socket.io 版本 | engine.io 版本 | ws 版本 |
---|---|---|
4.7.x | 6.5.x | 8.17.x |
4.6.x | 6.4.x | 8.11.x |
4.5.x | 6.2.x | 8.2.x |
4.4.x | 6.1.x | 8.2.x |
4.3.x | 6.0.x | 8.2.x |
4.2.x | 5.2.x | 7.4.x |
4.1.x | 5.1.x | 7.4.x |
4.0.x | 5.0.x | 7.4.x |
3.1.x | 4.1.x | 7.4.x |
3.0.x | 4.0.x | 7.4.x |
2.5.x | 3.6.x | 7.5.x |
2.4.x | 3.5.x | 7.4.x |