根本无法使SignalR(asp.net mvc4)和require.js一起工作

Rob*_*ert 5 requirejs asp.net-mvc-4 signalr marionette

我在网上看过类似的帖子,没有任何人建议为我工作.在这一点上,我真的面临着倾倒一个或另一个的选择.

这个"SignalR和MVC 4入门教程":

http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc-4

说你需要两个脚本包括使signalR工作:

    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-1.0.1.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
Run Code Online (Sandbox Code Playgroud)

我不知道如何制作第二个,即自动生成的SignalR集线器脚本,发生在require.js中.除非我遗漏了某些东西,否则似乎不是一个可行的require.js语法,用于包含自动生成的脚本.没有它,你会在jquery.signalR-1.1.2.js的第159行得到这个错误:

"JavaScript运行时错误:SignalR:加载集线器时出错.请确保您的集线器引用正确,例如"

jquery.signalR中该点的代码是这样做的:

    signalR.hub = {
            start: function () {
                // This will get replaced with the real hub connection start method when hubs is referenced correctly
                throw new Error("SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>.");
        }
    };
Run Code Online (Sandbox Code Playgroud)

有没有人通过require.js实际发生这种自动生成的脚本事件?

一直在研究这个.让我补充一些细节:

我正在使用这种方法 - 构建可扩展的客户端应用程序:(http://johndavidmathis.wordpress.com/2013/04/23/structuring-scalable-client-side-applications/)以构建更具可扩展性的结构.该系列的第二部分"允许模块使用多个文件和一个逻辑文件夹结构" http://johndavidmathis.wordpress.com/2013/04/23/structuring-scalable-client-side-applications/让我分裂我的实际信号器代码输入一个单独的Marionette聊天模块(与我的主app.js文件分开),以实现更好的文件结构.我真的很喜欢这种方法.我的项目的其余部分现在就像这样设置,它在寻找代码方面确实显示出优势.我认为额外的分裂是我被困的地方.似乎无法将第二个依赖项(自动生成的脚本)放入单独的聊天模块文件中.我还在研究这个,但在这一点上我觉得这样.require.js获取我的Marionette应用程序的依赖:

    require(["marionette","handlebars", "signalr", "signalr.hubs"], function (Marionette) {
        window.App = new Marionette.Application();

        App.addRegions({
            headerRegion: "#header",
            contentRegion: "#content",
            footerRegion: "#footer",
        });            

        require(["modules/main/loader", "modules/chat/loader"], function () {
            App.start();
        });
    })
Run Code Online (Sandbox Code Playgroud)

如果我希望聊天依赖进入应用程序,进入另一个文件的聊天模块?

就像是?

    define(dependencies,
        function () {
            App.module("ChatModule", function (ChatModule, App, Backbone, Marionette, $, _, "signalr.hubs", "signalr.hubs") {

            // SignalR Proxy created on the fly
                var chat = $.connection.chatHub;

                // Start the connection
                $.connection.hub.start();

    //more chat code...
Run Code Online (Sandbox Code Playgroud)

更新:

以下答案适用于我的开发环境.但是当我将代码发布到真实的生产服务器时,它不起作用.

当代码发布到真实的生产服务器(Windows Server Enterprise 2008 R2上的IIS 6.1)时,浏览器控制台再次显示自动生成的引用的"404".

具体来说,控制台显示"?" 正在被添加到".js"之前的参考路径中,就像这样......

http://mydomain.com/myapp/Scr​​ipts/application/signalr/hubs?.js ...

试过"?" 然后它会从路径中删除我的应用名称,就像这样......

http://mydomain.com/signalr/hubs.js.

我想有什么能让我得到第一个,没有"?",就像......

http://mydomain.com/myapp/Scr​​ipts/application/signalr/hubs.js

我只是没有看到如何实现这一目标.

最终更新:

生产服务器的最后一块拼图是网站的虚拟目录.这是最终的代码,对我有用.感谢Raciel R的帮助:

    requirejs.config({        
        paths: {
            //core
            "jquery": "jquery-1.9.1",

            "signalr": "jquery.signalR-1.1.2",
            "signalr.hubs": "/productionservervirtualdirectory/signalr/hubs?"
        },
        shim: {
            "jquery": {exports: "$"},            
            "signalr": { deps: ["jquery"] },
            "signalr.hubs": { deps: ["signalr"] }
        });
    //Then all you have to do is to make signalr.hubs required in your modules. Ie:

    require(["signalr.hubs"], function(){
         //your code here
    });
Run Code Online (Sandbox Code Playgroud)

Rac*_* R. 11

requirejs.config({        
    paths: {
        //core
        "jquery": "jquery-1.9.1",

        "signalr": "jquery.signalR-1.1.2",
        "signalr.hubs": "/signalr/hubs?"
    },
    shim: {
        "jquery": {exports: "$"},            
        "signalr": { deps: ["jquery"] },
        "signalr.hubs": { deps: ["signalr"] }
    });
Run Code Online (Sandbox Code Playgroud)

然后,您所要做的就是在模块中使用signalr.hubs.即:

require(["signalr.hubs"], function(){
     //your code here
});
Run Code Online (Sandbox Code Playgroud)

  • 无论客户端代码组织和技术如何使用都应该有效.这里棘手的部分(我在集成信号器时遇到了同样的问题)是自动生成的文件,可以通过添加?来解决这个问题.最后,以防止require.js搞砸它.我也试过noext插件,但我没有运气.我建议的方法是由另一个SO研究员向我建议的.Require.js填充程序和路径非常强大,可以描述不以require.js方式编写的模块之间的依赖关系,对于您自己的模块,只需正确定义它们就可以了. (2认同)