SignalR and MVC bundle

后端 未结 5 1195
迷失自我
迷失自我 2020-12-15 17:52

I\'m trying to use SignalR with MVC bundle, but having problem finding out how to include the /signalr/hubs script into the bundle. For now I have to insert the path in betw

5条回答
  •  失恋的感觉
    2020-12-15 18:10

    I used @KTW response mentioned on this Thread and here is the complete change

    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 Instead of writing below in above partial view

    @using Localization
    @using Newtonsoft.Json
    @model NameSpace.ViewModels.FilterVM
    
    @{
        ViewBag.Title = Strings.Filter;
    }
    
    @using (Html.BeginForm())
    {
        
    ---SOME CODE HERE
    } @section scripts { }

    This changed to

     @using Localization
        @using Newtonsoft.Json
        @model NameSpace.ViewModels.FilterVM
    
        @{
            ViewBag.Title = Strings.Filter;
        }
    
        @using (Html.BeginForm())
        {
            
    ---SOME CODE HERE
    } @section scripts { @Scripts.Render("~/bundles/SignalRScripts") }

    Notice

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

    in partial view above.Without @KTW file above(ajax request to /signalr/hubs)

    var usersHub = $.connection.currentUsersHub;
    

    was always coming as null.

提交回复
热议问题