我有一个自耕农 webapp
项目,我在其中添加了 socket.io-client
使用bower
。
当我使用 grunt 服务器
运行 Web 应用程序时,一切正常。但是当我使用 grunt build
构建它时,出现以下错误:
Uncaught TypeError: Cannot call method 'push' of undefined
通过在 Gruntfile.js
中启用源映射 (generateSourceMaps: true
),我设法追踪 socket 中的错误来源。 io.js
:
/**
* Add the transport to your public io.transports array.
*
* @api private
*/
io.transports.push('websocket');
运行grunt build
后,什么会使io.transports
变得未定义?
更新:
可能值得一提的是,我使用 RequireJS 并且它的配置如下:
require.config({
paths: {
jquery: '../bower_components/jquery/jquery',
// ...
// socket.io: Try the node server first
'socket.io': ['/socket.io/socket.io', '../bower_components/socket.io-client/dist/socket.io'],
},
shim: {
// Export io object: https://gist.github.com/guerrerocarlos/3651490
'socket.io': {
exports: 'io'
}
}
});
require(['jquery', 'socket.io'], function ($, io) {
'use strict';
// ...
});
最佳答案
您应该只在路径中定义 socket.io-client:
paths: {
'jquery': '../bower_components/jquery/jquery',
'socket.io': '../bower_components/socket.io-client/dist/socket.io'
},
此外,您不应设置 io 参数(它是全局设置的)(或使用 sio 等其他参数)
require(['jquery', 'socket.io'], function ($) {
'use strict';
// ...
});
关于运行 grunt build 时 socket.io-client 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165532/