Base solution for your next web application
Open Closed

SignalR client events not receiving #2330


User avatar
0
jonasm created

I created my own Hub for messages sending between clients. One part works ok: user enters price into the field, pushes button, and the new price is sent to server (hub). Inside the method in hub, a new price has to be sent to all other clients of the same group (user is added to the group on hub start). I enabled logging, and can see that required method (receiveNewPrice) is triggered (SignalR: Triggering client hub event 'receiveNewPrice' on hub 'AuctionsHub'), but nothing happens inside that method (not event console.log(newPrice)).

My hub:

public class AuctionsHub : Hub, ITransientDependency
    {
        public IAbpSession AbpSession { get; set; }
        public ILogger Logger { get; set; }
        private List<string> userGroups = new List<string>();

        public AuctionsHub()
        {
            AbpSession = NullAbpSession.Instance;
            Logger = NullLogger.Instance;
        }

        public void sendNewPrice(double newPrice, long auctionID)
        {
            Clients.Group("Auctions" + auctionID.ToString()).receiveNewPrice(newPrice.ToString());
        }

        public void AddUserToGroup(long auctionID)
        {
            userGroups.Add("Auctions" + auctionID.ToString());
            Groups.Add(Context.ConnectionId, "Auctions" + auctionID.ToString());
        }

        public override Task OnReconnected()
        {
            foreach(string gr in userGroups)
                userGroups.Add(gr);

            return base.OnReconnected();
        }
    }

My controller:

(function () {
    angular.module('app').controller('app.views.auctions.auction', [
        '$scope', '$state', '$rootScope', 'abp.services.app.auction',
        function ($scope, $state, $rootScope, auctionservice) {
            var vm = this;
            vm.localize = abp.localization.getSource('TitleAuctions');
            vm.AuctionDetails = null;
            vm.AuctionID = 0;
            vm.UserNewPrice = 0.0;
            vm.MinNewPrice = 0.0;

            var acHub = $.connection.auctionsHub;

            acHub.client.receiveNewPrice = function (newPrice) {
                $scope.$apply(function () {
                    console.log(newPrice);
                })
            };

            abp.ui.setBusy(null, {
                promise: auctionservice.getAuctionDetails({ AuctionID: $state.params.AuctionID }).success(function (data) {
                    vm.AuctionDetails = data.auction;
                    vm.AuctionID = data.auction.auctionID;

                    if (data.auction.winningPrice > data.auction.reservePrice)
                        vm.MinNewPrice = data.auction.winningPrice + data.auction.minPriceRaise;
                    else
                        vm.MinNewPrice = data.auction.reservePrice + data.auction.minPriceRaise;

                    $.connection.hub.start().done(function () {
                        acHub.server.addUserToGroup(vm.AuctionID);
                    });
                })
            });

            vm.updateNewPrice = function () {
                if (vm.UserNewPrice < vm.MinNewPrice) {
                    abp.notify.error(
                        abp.utils.formatString(vm.localize("BidPriceTooLow"),
                            vm.UserNewPrice, vm.localize("Currency"), vm.AuctionDetails.minPriceRaise, vm.localize("Currency")));
                }
                else {
                    abp.ui.setBusy(null, {
                        promise: auctionservice.addNewAuctionBid({ AuctionID: vm.AuctionID, BidPrice: vm.UserNewPrice })
                            .success(function () {
                                abp.notify.success(vm.localize("BidAddSuccess"));
                                vm.AuctionDetails.winningPrice = vm.UserNewPrice;
                                vm.MinNewPrice = vm.UserNewPrice + vm.AuctionDetails.minPriceRaise;

                                acHub.server.sendNewPrice(vm.UserNewPrice, vm.AuctionID);
                            })
                            .error(function () {
                                abp.notify.success(vm.localize("BidAddError"));
                            })
                    });
                }
            };
        }
    ]);
})();

No answer yet!