运行 应用程序在新版本 Socket.IO 大于 1.0

Run app on newest versions of Socket.IO gratter than 1.0

我需要一些帮助才能使写入 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

谢谢!

从 1.0.0 版开始,socket.io 中删除了缩小、etag 和 gzip。 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

因此,您应该从版本 0.9 中删除使用 io.enable 功能的行,并使客户端能够使用上面列出的 CDN 中的 socket.io