运行 grunt build 时 socket.io-client 中断

标签 socket.io requirejs gruntjs yeoman

我有一个自耕农 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/

相关文章:

reactjs - 与 Socket.io react - 在 "socket.on"事件上连续重新渲染

javascript - 将 JSON 文件导入运行在 Grunt 服务器上的 Angular 应用程序

handlebars.js - 将变量附加到 Handlebars 中的部分名称( mustache )

node.js - WAMP.ws : How to manage security and privacy?

node.js - 让socket和http服务器监听同一个端口

javascript - 将两个 socket.io 客户端连接在一起(建立套接字到套接字,跨浏览器连接)

javascript - 使用 RequireJS 加载外部 javascript 库

jquery - 我可以将 angular-selectize 与 require.js 一起使用吗?

jquery - RequireJS - 具有多个 jQuery 版本的 jQuery 插件

javascript - 如何通过 grunt-contrib-uglify 按顺序缩小 js 文件?