运行应用程序在最新版本的Socket.IO比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版本以来,小型化,etag和gzip已从socket.io中删除。 io.enable函数不再与API存在,这就是为什么它会抛出一个错误。 现在,SocketIO提供了一个具有gzip压缩和缩小版本的CDN。 使用这个来让你的客户使用CDN。

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

引入1.0.0时的SocketIO,指定了这里描述的 CDN传送方法。 具体来说,CDN(由Google zopfli提供)提供了高级的压缩压缩,适当的caching支持和内置的SSL支持。

来自SocketIO Author的build议是: 最初在这个封闭的Pull请求中列出

  • /socket/socket.io.js是为了开发。 它当然可以在生产环境中工作,但不会被优化
  • CDN提供了所有更高级的function,否则会导致我们的代码库(缩小,压缩,caching)膨胀。
  • 最终,如果select不使用CDN,最好使用socket.io作为构build系统的一部分(如webpack或browserify),并为整个构build引入缩小和压缩,而不是每个组件。

在这里存在一个同样的话题,但不包括CDN的最新版本。

如果您面对的问题不是io.enable ,那么有一个从0.9迁移到更高版本的SocketIO的指南 。

所以,你应该从版本0.9中使用io.enable函数删除行,并使客户端使用上面列出的CDN中的socket.io