SignalR和MVC包(SignalR and MVC bundle)

2019-06-26 10:11发布

我试图使用SignalR与MVC包,但有找出如何包含/ signalr /集线器脚本到包问题。 现在我必须插入路径jquery.signalR和我的代码之间。 这将导致在三个JavaScript文件的请求。

有没有什么办法,包括/ signalr /集线器到我的MVC包?

Answer 1:

默认的/ signalr /集线器脚本由所述运行时动态地生成在所述第一请求,然后高速缓存。

您可以使用hubify.exe(见http://weblogs.asp.net/davidfowler/archive/2012/06/10/signalr-0-5-1-released.aspx了解详细信息)预先生成的文件自己,这样你就可以将其添加到MVC束。



Answer 2:

有点晚了,但这里是我的贡献:
创建一个包含以下内容的JavaScript文件:

(function ($) {
    $.ajax({
        url: "/signalr/hubs",
        dataType: "script",
        async: false
    });
}(jQuery));

然后将文件添加到收藏捆绑。
这将加载“/ signalr /中心”的代码为您服务。



Answer 3:

我知道这是一个古老的线程,但我想补充以下为SignalR 2.x的 我真的很想捆绑使用SquishIt代理并通过试错我设法想出如下:

using Microsoft.AspNet.SignalR
using Microsoft.AspNet.SignalR.Hubs

var resolver = new DefaultHubManager(new DefaultDependencyResolver());
var proxy = new DefaultJavaScriptProxyGenerator(resolver, new NullJavaScriptMinifier());
string iCanHazScriptNao = proxy.GenerateProxy("/signalr");


Answer 4:

从asp.net ,使用SignalR.Utils NuGet包,我发现我需要在与具有所述轮毂在它的DLL的目录:

假设使用的是标准溶液的结构和使用的SignalR.Utils 2.2.0)

cd C:\YourSolution\YourProjectWithTheHub\bin\Debug
..\..\..\packages\Microsoft.AspNet.SignalR.Utils.2.2.0\tools\signalr.exe ghp

运行该工具后,会有一个server.js在你运行它的目录文件(在这种情况下, Debug )。

注:我不能让它与/ P标志指定路径时工作,由于某种原因,即使它的工作,它与它的signalr.exe文件创建一个临时目录



Answer 5:

我用此主题中提到@KTW响应和这里是完全改变

BundleConfig

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/modernizr-2.6.2.js",
            "~/Scripts/jquery-2.2.3.js",
            "~/Scripts/jquery-ui-1.11.4.js",
            "~/Scripts/jquery.multiselect.js",
            "~/Scripts/jquery.dataTables.js",
            "~/Scripts/jquery.jstepper.min.js",
            "~/Scripts/underscore.min.js"
        ));

        bundles.Add(new ScriptBundle("~/bundles/SignalRScripts").Include(
            "~/Scripts/jquery.signalR-2.2.2.min.js",
            "~/Scripts/signalRBundle.js",
            "~/Scripts/Views/Search/SignalRFunctions.js"));
    }
}

SignalRFunctions.js

    $(function() {
    // Declare a proxy to reference the hub.
    var usersHub = $.connection.currentUsersHub;
    //Create a function that the hub can call to broadcast messages.
    usersHub.client.broadcastMessage = function(reservationNumber, usrName) {
        //Message broadcast from server
        //now find the id with reservationNumber on the page and to that append the user name
        var id = '#' + reservationNumber;
        if ($(id).length) {
            if (usrName.length) {
                itemsOpened($(id), usrName);
            } else {
                itemsClosed($(id));
            }
        } 
        //else {
        //    //is it possible that server broad casted for a reservationNumber and is not available at the client?
        //}
    };

    //Accepts dictionary from hub and goes through search results
    //https://stackoverflow.com/questions/7776337/reading-c-sharp-dictionary-in-javascript
    usersHub.client.broadcastCollection = function (reservationNumberAndUsers) {
       for (var resNumKey in reservationNumberAndUsers) {
            if (reservationNumberAndUsers.hasOwnProperty(resNumKey)) {
                //Message broadcast from server
                //now find the id with ReservationNumber on the page and to that append the user name
                var id = '#' + resNumKey;
                if ($(id).length) {
                    if (reservationNumberAndUsers[resNumKey].length) {
                        itemsOpened($(id), reservationNumberAndUsers[resNumKey]);
                    } else {
                        itemsClosed($(id));
                    }
                }
            }
        }
    };

    $.connection.hub.start().done(function() {
                var searchedReservationNumbers = [];
                if (typeof jsData !== 'undefined') {
                    if (jsData && jsData.length) {
                        for (var i = 0; i < jsData.length; i++) {
                            searchedReservationNumbers.push(jsData[i].UReservationNumber);
                        }
                        if (searcheduReservationNumbers.length !== 0) {
                            usersHub.server.getWorkingUsersOnUReservationNumber(searcheduReservationNumbers);
                        }
                    }
                }
            }).fail(function () { console.log('Could not Connect To SignalrHub!'); });
        /*In case we would decide to continuously reconnect making connection to server.
        $.connection.hub.disconnected(function() {
            setTimeout(function() {
                    $.connection.hub.start();
                },
                5000); // Restart connection after 5 seconds.
        });*/

function itemsOpened(elem, id) {
    var item = "Opened By - " + id;
    elem.prop('title', item);
    elem.css('background-color', 'chocolate');
};

function itemsClosed(elem) {
    elem.prop('title', "");
    elem.css('background-color', '');
};
});

signalRBundle.js

(function ($) {
$.ajax({
    url: "/signalr/hubs",
    dataType: "script",
    async: false
});
}(jQuery));
/* Source https://stackoverflow.com/questions/11556110/signalr-and-mvc-bundle */

SomePartialView.cshtml代替在上述局部视图下方写的

@using Localization
@using Newtonsoft.Json
@model NameSpace.ViewModels.FilterVM

@{
    ViewBag.Title = Strings.Filter;
}

@using (Html.BeginForm())
{
    <div class="large-12 columns">
        ---SOME CODE HERE 
    </div>

}

@section scripts
{
<script type="text/javascript" language="javascript">
    var jsData = @Html.Raw(JsonConvert.SerializeObject(Model));
</script>
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/Views/Search/SignalRFunctions.js"></script>
}

这改变

 @using Localization
    @using Newtonsoft.Json
    @model NameSpace.ViewModels.FilterVM

    @{
        ViewBag.Title = Strings.Filter;
    }

    @using (Html.BeginForm())
    {
        <div class="large-12 columns">
            ---SOME CODE HERE 
        </div>

    }

    @section scripts
    {
    <script type="text/javascript" language="javascript">
        var jsData = @Html.Raw(JsonConvert.SerializeObject(Model));
    </script>
    @Scripts.Render("~/bundles/SignalRScripts")
    }

注意

@Scripts.Render("~/bundles/SignalRScripts") 

在上述above.Without文件@KTW局部视图(AJAX请求/ signalr /集线器)

var usersHub = $.connection.currentUsersHub;

总是来了空。



文章来源: SignalR and MVC bundle