客户端安装
版本兼容性
¥Version compatibility
下面是服务器端和 JS 客户端的兼容性表:
¥Here is the compatibility table between the server and the JS client:
JS 客户端版本 | Socket.IO 服务器版本 | |||
---|---|---|---|---|
1.x | 2.x | 3.x | 4.x | |
1.x | 是的 | 不 | 不 | 不 |
2.x | 不 | 是的 | 是的1 | 是的1 |
3.x | 不 | 不 | 是的 | 是的 |
4.x | 不 | 不 | 是的 | 是的 |
[1] 是的,与 允许 EIO3:true 一起
¥[1] Yes, with allowEIO3: true
请查看相关的迁移指南:
¥Please check the associated migration guides:
浏览器支持
¥Browser support
Socket.IO 确实支持 IE9 及以上版本。不再支持 IE 6/7/8。
¥Socket.IO does support IE9 and above. IE 6/7/8 are not supported anymore.
借助出色的 Sauce Labs 平台测试了浏览器兼容性:
¥Browser compatibility is tested thanks to the awesome Sauce Labs platform:
安装
¥Installation
独立构建
¥Standalone build
默认情况下,Socket.IO 服务器在 /socket.io/socket.io.js
处公开客户端包。
¥By default, the Socket.IO server exposes a client bundle at /socket.io/socket.io.js
.
io
将被注册为全局变量:
¥io
will be registered as a global variable:
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
</script>
如果你不需要此功能(请参阅下面的其他选项),你可以在服务器端禁用该功能:
¥If you don't need this (see other options below), you can disable the functionality on the server side:
const { Server } = require("socket.io");
const io = new Server({
serveClient: false
});
来自 CDN
¥From a CDN
你还可以包含来自 CDN 的客户端打包包:
¥You can also include the client bundle from a CDN:
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js" integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO" crossorigin="anonymous"></script>
Socket.IO 也可以从其他 CDN 获得:
¥Socket.IO is also available from other CDN:
cdnjs:https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.5/socket.io.min.js
jsDelivr:https://cdn.jsdelivr.net/npm/socket.io-client@4.7.5/dist/socket.io.min.js
拆包:https://unpkg.com/socket.io-client@4.7.5/dist/socket.io.min.js
¥unpkg: https://unpkg.com/socket.io-client@4.7.5/dist/socket.io.min.js
有多种打包包可供选择:
¥There are several bundles available:
名称 | 尺寸 | 描述 |
---|---|---|
套接字.io.js | 34.7 kB 压缩 | 未缩小版本,带有 debug |
socket.io.min.js | 最小 14.7 kB+gzip | 量产版,不含 debug |
socket.io.msgpack.min.js | 最小 15.3 kB+gzip | 量产版,不带 debug,带 消息包解析器 |
debug 包允许将调试信息打印到控制台。你可以找到更多信息 此处。
¥The debug package allows to print debug information to the console. You can find more information here.
在开发过程中,我们建议使用 socket.io.js
打包包。通过设置 localStorage.debug = 'socket.io-client:socket'
,客户端接收到的任何事件都将打印到控制台。
¥During development, we recommend using the socket.io.js
bundle. By setting localStorage.debug = 'socket.io-client:socket'
, any event received by the client will be printed to the console.
对于生产,请使用 socket.io.min.js
打包包,这是一个不包括调试包的优化构建。
¥For production, please use the socket.io.min.js
bundle, which is an optimized build excluding the debug package.
来自 NPM
¥From NPM
Socket.IO 客户端与 webpack 或 browserify 等打包器兼容。
¥The Socket.IO client is compatible with bundlers like webpack or browserify.
- NPM
- Yarn
- pnpm
npm install socket.io-client
yarn add socket.io-client
pnpm add socket.io-client
客户端也可以从 Node.js 运行。
¥The client can also be run from Node.js.
注意:由于上述原因,你可能希望从浏览器打包包中排除调试。通过 webpack,你可以使用 webpack-remove-debug。
¥Note: for the reasons cited above, you may want to exclude debug from your browser bundle. With webpack, you can use webpack-remove-debug.
TypeScript 用户注意事项:这些类型现在包含在 socket.io-client
包中,因此不再需要 @types/socket.io-client
中的类型,并且实际上可能会导致错误:
¥Note for TypeScript users: the types are now included in the socket.io-client
package and thus the types from @types/socket.io-client
are not needed anymore and may in fact cause errors:
Object literal may only specify known properties, and 'extraHeaders' does not exist in type 'ConnectOpts'
杂项
¥Miscellaneous
依赖树
¥Dependency tree
客户端的基本安装包括 9 个软件包,其中 5 个由我们团队维护:
¥A basic installation of the client includes 9 packages, of which 5 are maintained by our team:
└─┬ socket.io-client@4.7.2
├── @socket.io/component-emitter@3.1.0
├─┬ debug@4.3.4
│ └── ms@2.1.2
├─┬ engine.io-client@6.5.2
│ ├── @socket.io/component-emitter@3.1.0 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
│ └── xmlhttprequest-ssl@2.0.0
└─┬ socket.io-parser@4.2.4
├── @socket.io/component-emitter@3.1.0 deduped
└── debug@4.3.4 deduped
传递版本
¥Transitive versions
engine.io-client
包带来了负责管理底层连接(HTTP 长轮询或 WebSocket)的引擎。也可以看看:工作原理
¥The engine.io-client
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-client 版本 | engine.io-client 版本 | ws 版本1 |
---|---|---|
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.5.x | 7.5.x |
2.4.x | 3.5.x | 7.5.x |
[1] 仅适用于 Node.js 用户。在浏览器中,使用原生 WebSocket API。
¥[1] for Node.js users only. In the browser, the native WebSocket API is used.