Prerequisites(先决条件)
Please make sure that Node.js is installed on your system. The current Long Term Support (LTS) release is an ideal starting point.
At least Node.js 8 is needed, older versions are not supported anymore.
请确保您的系统上已安装Node.js。 当前的长期支持(LTS)版本是理想的起点。
至少需要Node.js 8,不再支持旧版本。
Installation
The latest Socket.IO release is:
To install the latest release:
1 | npm install socket.io |
To install a specific version:
1 | npm install socket.io@<version> |
Additional packages(其他包)
By default, Socket.IO use the WebSocket server provided by the ws package.
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: Allows to efficiently perform operations such as masking and unmasking the data payload of the WebSocket frames.
- utf-8-validate: Allows to efficiently check if a message contains valid UTF-8 as required by the spec.
To install those packages:
默认情况下,Socket.IO使用ws软件包提供的WebSocket服务器。
可以在此软件包的旁边安装2个可选软件包。 这些软件包是二进制附件,可改善某些操作。 预编译的二进制文件适用于大多数流行的平台,因此您不一定需要在计算机上安装C ++编译器。
bufferutil:允许有效地执行操作,例如屏蔽和取消屏蔽WebSocket帧的数据有效负载。
utf-8-validate:可以有效地检查邮件是否包含规范要求的有效UTF-8。
要安装这些软件包:
1 | npm install --save-optional bufferutil utf-8-validate |
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服务器不可用,它们将回退到Javascript实现。 更多信息可以在这里找到。
Other WebSocket server implementations(其他WebSocket服务器实现)
Any Websocket server implementation which exposes the same API as ws (notably the handleUpgrade method) can be used.
For example, you can use the eiows package, which is a fork of the (now deprecated) uws package:
可以使用任何与ws公开相同API的Websocket服务器实现(特别是handleUpgrade方法)。
例如,您可以使用eiows软件包,它是uws软件包(现已弃用)的分支:
1 | npm install eiows |
And then use the wsEngine
option:
1 | const io = require('socket.io')(3000, { |
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.
与默认实现相比,此实现“允许但不能保证”性能和内存使用方面的重大改进。 与往常一样,请对照您自己的用法对其进行基准测试。
Caught a mistake? Edit this page on GitHub