Skip to main content
Version: 4.x

日志记录和调试

Socket.IO 现在完全由 TJ Holowaychuk 开发的名为 debug 的简约但功能极其强大的实用程序实现。

¥Socket.IO is now completely instrumented by a minimalistic yet tremendously powerful utility called debug by TJ Holowaychuk.

在 1.0 之前,Socket.IO 服务器默认将所有内容记录到控制台。事实证明,这对于许多用户来说是令人烦恼的冗长(尽管对其他人来说非常有用),所以现在我们默认情况下完全保持沉默。

¥Before 1.0, the Socket.IO server would default to logging everything out to the console. This turned out to be annoyingly verbose for many users (although extremely useful for others), so now we default to being completely silent by default.

基本思想是 Socket.IO 使用的每个模块都提供不同的调试范围,让你深入了解内部结构。默认情况下,所有输出都会被抑制,你可以通过提供 DEBUG 环境变量 (Node.JS) 或 localStorage.debug 属性(浏览器)来选择查看消息。

¥The basic idea is that each module used by Socket.IO provides different debugging scopes that give you insight into the internals. By default, all output is suppressed, and you can opt into seeing messages by supplying the DEBUG env variable (Node.JS) or the localStorage.debug property (Browsers).

你可以在我们的主页上看到它的实际效果:

¥You can see it in action for example on our homepage:

https://i.cloudup.com/transcoded/IL9alTr0eO.mp4

可用的调试范围

¥Available debugging scopes

查看可用信息的最佳方法是使用 *

¥The best way to see what information is available is to use the *:

DEBUG=* node yourfile.js

或者在浏览器中:

¥or in the browser:

localStorage.debug = '*';

然后按你感兴趣的范围进行筛选。你可以在 * 前面加上范围,如果有多个范围,则用逗号分隔。例如,要仅在 Node.js 上查看来自 socket.io 客户端的调试语句,请尝试以下操作:

¥And then filter by the scopes you’re interested in. You can prefix the * with scopes, separated by comma if there is more than one. For example, to only see debug statements from the socket.io client on Node.js try this:

DEBUG=socket.io:client* node yourfile.js

要查看来自引擎和 socket.io 的所有调试消息:

¥To see all debug messages from the engine and socket.io:

DEBUG=engine,socket.io* node yourfile.js

从浏览器包中删除调试

¥Removing debug from your browser bundle

虽然在开发过程中很有用,但调试包为最终打包包增加了额外的重量(压缩和压缩后大约 4KB),这就是为什么它被排除在精简打包包之外(有关各种浏览器打包包的更多详细信息可以在 此处 中找到)。

¥While useful during development, the debug package adds an extra weight to the final bundle (about 4KB minified and gzipped), that's why it is excluded from the slim bundle (more details about the various browser bundles can be found here).

如果你使用的是 webpack,可以使用 webpack-remove-debug 将其删除:

¥If you are using webpack, you can remove it with webpack-remove-debug:

{
module: {
rules: [
{
test: /\.js$/,
loader: 'webpack-remove-debug'
}
]
}
}

浏览器控制台中的错误日志

¥Error logs in the browser console

请注意错误日志,例如:

¥Please note that error logs such as:

  • net::ERR_INTERNET_DISCONNECTED

  • net::ERR_CONNECTION_REFUSED

  • WebSocket is already in CLOSING or CLOSED state

  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at xxx. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

  • The connection to xxx was interrupted while the page was loading

不是由 Socket.IO 库触发的,而是由浏览器本身触发的,因此不受我们控制。

¥are not emitted by the Socket.IO library but by the browser itself, and are therefore out of our control.