gpt4 book ai didi

node.js - 在高于 1.0 的新版本 Socket.IO 上运行应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 23:37:04 25 4
gpt4 key购买 nike

我需要一些帮助才能使写入 Socket.IO < 0.9 的代码适用于较新的版本,例如 1.3:

var port = 843;

var io = require('socket.io').listen( port );
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
//io.set('log level', 1);

io.sockets.on('connection', function (socket) {
socket.on('woot_send', function(op){
socket.broadcast.emit('woot_receive', op);
if( op.type == 'cursor-create' && Object.keys(io.connected).length == 1 )
socket.emit('woot_receive', {type: 'contents-init', contents: "Sample initial content that might be coming from permanent storage..."});
});
socket.on('woot_save', function(contents){
console.log(contents);
});
});

它会导致错误:

/home/celso/woot/node/app.js:4
io.enable('browser client minification'); // send minified client
^
TypeError: undefined is not a function
at Object.<anonymous> (/home/celso/woot/node/app.js:4:4)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

谢谢!

最佳答案

缩小、etag 和 gzip 已从 socket.io 中删除从 1.0.0 版本开始。 io.enable API 中不再存在该函数,这就是它抛出错误的原因。SocketIO 现在提供带有 gzip 压缩和缩小版本的 CDN。使用它可以让您的客户端使用 CDN。

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>

SocketIO 在 1.0.0 版本引入时,指定的 CDN 交付方式为 described here 。具体来说,CDN(由 Google zopfli 提供)提供高级别的 zip 压缩、对缓存的适当支持以及内置 SSL 支持。

SocketIO 作者的建议是:Originally listed in this closed Pull Request

  • /socket/socket.io.js 用于开发。当然,它可以在生产中工作,但不会被优化
  • CDN 提供了所有更高级的功能,否则会使我们的代码库变得臃肿(缩小、压缩、缓存)。
  • 最终,如果选择不使用 CDN,最好使用 socket.io 作为构建系统(如 webpack 或 browserify)的一部分,并为整个构建而不是每个组件引入缩小和压缩。

旧的SO Question此处存在同一主题,但不包括 CDN 的最新版本。

如果您遇到除 io.enable 以外的问题,有一个指南migrating from 0.9 to higher versions of SocketIO .

因此,您应该使用 io.enable 删除这些行从0.9版本开始新增功能,让客户端可以使用socket.io来自上面列出的 CDN。

关于node.js - 在高于 1.0 的新版本 Socket.IO 上运行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com