Socket.IO客户端库加载缓慢

时间:2022-06-21 15:49:05

Socket.IO客户端库加载缓慢

I start my server, and refresh the page in a browser, which then takes >2s to load the JS resource. If I then reload the page in any browser, it loads quickly.

我启动我的服务器,并在浏览器中刷新页面,然后花费> 2秒来加载JS资源。如果我然后在任何浏览器中重新加载页面,它会快速加载。

This is only happening the first request after the server has been started. I suppose it has something to do with it putting together the JS file the first time, and then after that it is cached on the server.

这仅在服务器启动后的第一个请求中发生。我想它与第一次将JS文件放在一起有关,然后在服务器上缓存它。

Can anything be done to cut down this time?

可以做些什么来减少这个时间?

I have tried both with and without the production settings (gzip, minify etc).

无论有没有生产设置(gzip,minify等),我都试过了。

Client code:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
</script>

Server code:

var express = require('express'),
    expressServer = express.createServer(),
    socketServer = require('socket.io').listen(expressServer);

expressServer.listen(1337);

2 个解决方案

#1


3  

There is currently a bug in socket.io that is causing this. Make sure you do NOT have this set and it should load MUCH faster:

目前socket.io中存在一个导致此问题的错误。确保你没有这个设置,它应该加载更快:

io.set('browser client gzip', true);          // gzip the file

The first call to load socket.io.js will try to compress it and store it in memory. You will run into these bugs:

加载socket.io.js的第一次调用将尝试压缩它并将其存储在内存中。你会遇到这些错误:

You can get some speed increase by using the minified version and allowing caching until this is fixed:

您可以通过使用缩小版本并允许缓存来提高速度,直到修复为止:

io.set('browser client minification', true);  // send minified client
io.set('browser client etag', true);          // apply etag caching logic based on version number

#2


0  

Somehow, your jQuery library, which is more than half as big as the socket.io library, downloads 50x faster. Perhaps it was cached from before? Ultimately, the browser is just downloading a file.

不知何故,你的jQuery库比socket.io库大一半,下载速度提高了50倍。也许它是从之前缓存的?最终,浏览器只是下载文件。

Anyways, this fellow claims to have shrunk it.

无论如何,这个家伙声称已经缩水了。

#1


3  

There is currently a bug in socket.io that is causing this. Make sure you do NOT have this set and it should load MUCH faster:

目前socket.io中存在一个导致此问题的错误。确保你没有这个设置,它应该加载更快:

io.set('browser client gzip', true);          // gzip the file

The first call to load socket.io.js will try to compress it and store it in memory. You will run into these bugs:

加载socket.io.js的第一次调用将尝试压缩它并将其存储在内存中。你会遇到这些错误:

You can get some speed increase by using the minified version and allowing caching until this is fixed:

您可以通过使用缩小版本并允许缓存来提高速度,直到修复为止:

io.set('browser client minification', true);  // send minified client
io.set('browser client etag', true);          // apply etag caching logic based on version number

#2


0  

Somehow, your jQuery library, which is more than half as big as the socket.io library, downloads 50x faster. Perhaps it was cached from before? Ultimately, the browser is just downloading a file.

不知何故,你的jQuery库比socket.io库大一半,下载速度提高了50倍。也许它是从之前缓存的?最终,浏览器只是下载文件。

Anyways, this fellow claims to have shrunk it.

无论如何,这个家伙声称已经缩水了。