ASP .Net SignalR没有调用客户端的方法

时间:2016-07-09 18:08:47

标签: c# asp.net-mvc razor signalr

我一直在使用asp .net SignalR,我的网络应用程序索引页面与SignalR配合良好,但当我开始使用此推送通知系统制作详细信息页面时,我遇到错误 - SignalR没有调用我的客户端方法详情页面。

以下是使用razor的详细信息页面代码:

<h2>last bidders</h2>
<table class="table-info" style="width: 500px;">
    <thead>
        <th>bidder</th>
        <th>price</th>
        <th>time</th>
        <th>state</th>
    </thead>
    @for (int i = 0; i < 10; i++)
    {
        if (i < ViewBag.count) // Bidder exists
        {
            <tr>
                <td id="@Html.Raw("bidder" + i)">@ViewBag.bidders[i]</td>
                <td id="@Html.Raw("price" + i)">@ViewBag.prices[i]</td>
                <td id="@Html.Raw("time" + i)">@ViewBag.times[i]</td>
                <td id="@Html.Raw("state" + i)">@ViewBag.states[i]</td>
            </tr>
        }
        else // No bidder - fill with blank
        {
            <tr>
                <td id="@Html.Raw("bidder" + i)">&nbsp;</td>
                <td id="@Html.Raw("price" + i)">&nbsp;</td>
                <td id="@Html.Raw("time" + i)">&nbsp;</td>
                <td id="@Html.Raw("state" + i)">&nbsp;</td>
            </tr>
        }
    } 
</table>

<input type="hidden" id="IDAuction" value="@ViewBag.IDAuction" />

@section scripts {
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>        
    $(function () {

        var my_hub = $.connection.myHub;

        my_hub.client.auctionDetailsUpdate = function (IDAuction, newBidder, newPrice, newTime, newState) {
            alert("auctionDetailsUpdate()");
            var x = document.getElementById("IDAuction").value;
            alert('1');
            if (x == IDAuction) {
                alert('2');
                moveRowsDown();
                alert('3');
                document.getElementById("time0").innerHTML = newTime;
                document.getElementById("price0").innerHTML = newPrice;
                document.getElementById("bidder0").innerHTML = newBidder;
                document.getElementById("state0").innerHTML = newState;
                alert('4');
            }
        };

        // Client register on MyHub
        $.connection.hub.start().done(function () {
            $(document).ready(function () {
                //my_hub.server.registerConId($("#clientEmail").val());
            });
        });

    });

    function moveRowsDown() {
        alert('moverowsdown')
        for (var i = 9; i > 0; i--) {
            document.getElementById("time" + i).innerHTML = document.getElementById("time" + (i - 1)).innerHTML;
            document.getElementById("price" + i).innerHTML = document.getElementById("price" + (i - 1)).innerHTML;
            document.getElementById("bidder" + i).innerHTML = document.getElementById("bidder" + (i - 1)).innerHTML;
            document.getElementById("state" + i).innerHTML = document.getElementById("state" + (i - 1)).innerHTML;
        }
    }

</script>

这是我的索引页面(我只将相关代码放入SignalR,我试图用代码中的无用信息填写我的问题):

@section scripts {
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>

    $(function () {

        var my_hub = $.connection.myHub;

        // Server reply on bid attempt
        my_hub.client.clientBidsUpdate = function (IDAuction, newState, newDuration, newLastBidder, newPrice, warningNoTokens) {
            var clientEmail = $("#" + "email" + IDAuction).val();
            $("#" + "state" + IDAuction).text(newState);
            $("#" + "time" + IDAuction).text(newDuration);
            $("#" + "price" + IDAuction).text(newPrice);
            // Unsuccessful bid
            if (warningNoTokens == "true") {
                if (newLastBidder == clientEmail) {
                    alert("Not enough tokens to place bigger bid. Please buy some tokens!");
                }
            // Succesful bid
            } else {
                $("#" + "lastbidder" + IDAuction).text(newLastBidder);
                $("#" + "spanPrice" + IDAuction).addClass("glyphicon glyphicon-arrow-up");
                $("#" + "spanPrice2" + IDAuction).addClass("glyphicon glyphicon-arrow-up");
            }
        };

        // Increase previous bidder token number - someone has bigger bid
        my_hub.client.setTokenNumber = function (clientSelector, tokenNumber, clientAlertSelector) {
            document.getElementById(clientSelector).innerHTML = tokenNumber;
            document.getElementById(clientAlertSelector).style.display = "inline-block";
        }

        // Timer Update method // each 1 second
        my_hub.client.timerUpdate = function (IDAuction, newState, newDuration, newLastBidder, newPrice) {
            $("#" + "state" + IDAuction).text(newState);
            $("#" + "time" + IDAuction).text(newDuration);
            $("#" + "lastbidder" + IDAuction).text(newLastBidder);
            $("#" + "price" + IDAuction).text(newPrice);
            // Disable auction on client side
            if (newState == "Sold" || newState == "Expired") {
                if (newState == "Sold") {
                    $("#" + "btn" + IDAuction).html("Sold");
                    $("#" + "btn" + IDAuction).removeClass("btn-warning btn").addClass("btn-info btn");
                    $("#" + "btn" + IDAuction).css("background-color", "blueviolet");
                    $("#" + "btn" + IDAuction).css("color", "white");
                    $("#" + "state" + IDAuction).css("color", "blueviolet");
                    $("#" + "spanLastBidder" + IDAuction).removeClass("glyphicon glyphicon-user").addClass("glyphicon glyphicon-ok");
                    $("#" + "picture" + IDAuction).css("-webkit-filter", "blur(8px)").css("filter", "blur(8px)");
                    $("#" + "btn" + IDAuction).prop("disabled", true);
                }
                else if (newState == "Expired") { 
                    $("#" + "btn" + IDAuction).html("Expired");
                    $("#" + "btn" + IDAuction).removeClass("btn-warning btn").addClass("btn-default btn");
                    $("#" + "state" + IDAuction).css("color", "rgb(255, 54, 40)");
                    $("#" + "spanLastBidder" + IDAuction).removeClass("glyphicon glyphicon-user").addClass("glyphicon glyphicon-remove");
                    $("#" + "picture" + IDAuction).css("-webkit-filter", "blur(8px)").css("filter", "blur(8px)");
                    $("#" + "btn" + IDAuction).prop("disabled", true);
                    $("#" + "btn" + IDAuction).css("background-color", "rgb(255, 54, 40)");
                }
            }
            $("#" + "spanPrice" + IDAuction).removeClass("glyphicon glyphicon-arrow-up");
            $("#" + "spanPrice2" + IDAuction).removeClass("glyphicon glyphicon-arrow-up");
            var clientEmail = document.getElementById("clientEmail").value;
            var clientEmailReplaced = clientEmail.replace("@@", "_");
            document.getElementById("alertToken" + clientEmailReplaced).style.display = "none";
        };

        // Client sends bid to Server
        $.connection.hub.start().done(function () {
            $('.btnBid').click(function (event) {
                var idBtnJquery = $(event.target).attr('id');
                var IDAuc = idBtnJquery.substring(3, idBtnJquery.length);
                var clientEmail = $("#" + "email" + IDAuc).val();
                my_hub.server.send(IDAuc, clientEmail);
            });
        });

        // Client register on MyHub
        $.connection.hub.start().done(function () {
            $(document).ready(function () {
                my_hub.server.registerConId($("#clientEmail").val());
            });
        });

    });

    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }

    // Variables
    var toolbarVisible = false;

    // Search - Menu toolbar preview
    $("#showHideBtn").click(function () {
        $("#menuSearchToolbar").slideToggle("slow", "swing");
        if (toolbarVisible == true) {
            toolbarVisible = false;
            $("#showHideBtn").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
        } else {
            toolbarVisible = true;
            $("#showHideBtn").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
        }                
    });

</script>
}

这是我的中心服务器类

namespace IEP_Projekat.Hubs
{

public static class mutex
{
    public static string lockObject = "MutEx";
}

public class MyHub : Hub
{
    private static Dictionary<string, string> hashUsersConnIds = new Dictionary<string, string>(512);

    public void Send(long IDAuc, string lastBidderEmail)
    {
        lock (mutex.lockObject)
        {
            if ((productNewPrice <= user.TokenNumber))
            {       
                if (previousBidderID != null)
                {         
                    if (hashUsersConnIds.ContainsKey(previous.Email))
                    {
                        Clients.Client(hashUsersConnIds[previous.Email]).setTokenNumber(clientSelector, newTokenCount, clientAlertSelector);
                    }  
                    if (previous.Email != lastBidderEmail && hashUsersConnIds.ContainsKey(lastBidderEmail)) {
                        Clients.Client(hashUsersConnIds[lastBidderEmail]).setTokenNumber(clientSelector, newBidderCount, clientAlertSelector);
                    }
                } 
                else
                {
                    if (hashUsersConnIds.ContainsKey(lastBidderEmail))
                    {
                        Clients.Client(hashUsersConnIds[lastBidderEmail]).setTokenNumber(clientSelector, newBidderCount, clientAlertSelector);
                    }
                }

                Clients.All.clientBidsUpdate(IDAuc, auction.state, remainingToEnd, lastBidderEmail, auction.price + auction.increment, "false");
                Clients.All.auctionDetailsUpdate(IDAuc, lastBidderEmail, auction.price + auction.increment, newBid.bidTime, "Open");
                return;

            }

            else if (auction.lastbidder == user.Email)
            {
                if (user.TokenNumber > 0) // can place next bid
                {
                    if (hashUsersConnIds.ContainsKey(lastBidderEmail))
                    {
                        Clients.Client(hashUsersConnIds[lastBidderEmail]).setTokenNumber(clientSelector, user.TokenNumber, clientAlertSelector);
                    }

                    Clients.All.clientBidsUpdate(IDAuc, auction.state, remainingToEnd, lastBidderEmail, auction.price + auction.increment, "false");
                    Clients.All.auctionDetailsUpdate(IDAuc, lastBidderEmail, auction.price + auction.increment, newBid.bidTime, "Open");                      
                    return;
                }
            }

            Clients.All.clientBidsUpdate(IDAuc, auction.state, remaining, lastBidderEmail, auction.price + auction.increment, "true");
        }
    }

    // Registring client
    public void registerConId(string email)
    {
        hashUsersConnIds[email] = Context.ConnectionId;
    }

}

public class MyRegistry : Registry
{
    public MyRegistry()
    {          
        Schedule(() =>
        {
            lock (mutex.lockObject)
            {
                var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
                foreach (var auction in auctionsList)
                {
                    if (now >= end)
                    {

                        if (edited.increment == 0)
                        {
                            hubContext.Clients.All.timerUpdate(auction.IDAuc, edited.state, newDurationExpired, " - ", edited.price);
                        }
                        else
                        {
                            hubContext.Clients.All.timerUpdate(auction.IDAuc, edited.state, newDurationSold, edited.lastbidder, soldPrice);
                            hubContext.Clients.All.auctionDetailsUpdate(auction.IDAuc, edited.lastbidder, soldPrice, newDurationSold, "Sold");                               
                        }
                    }
                    hubContext.Clients.All.timerUpdate(auction.IDAuc, auction.state, newDuration, auction.lastbidder, actualPrice);
                }
            }
        }).ToRunNow().AndEvery(1).Seconds();
    }
}
}

这是我的服务器逻辑。我使用MyHub.cs作为集线器类,使用Registry作为计时器。我已经清理了数据库访问和一些计算的代码,因为该部分没有问题,我想避免使用350行代码,所以我只是在这个SignalR交互中留下了重要的代码。

我遇到Clients.All.auctionDetailsUpdate (...)的问题,在调试Web应用程序时它说一切都很好,而其他方法SignalR方法也可以,但是我没有得到任何方法来自AuctionDetailsUdate()方法中指定的我的详细信息页面的警报,因为出于某种原因Hub没有调用它。

1 个答案:

答案 0 :(得分:0)

我通过实现在客户端(javascript)上调用的方法解决了这个问题。

所以我在details.cshtml上添加了(带有空白正文)

my_hub.client.timerUpdate = function (IDAuction, newState, newDuration, newLastBidder, newPrice) { }
my_hub.client.setTokenNumber = function (clientSelector, tokenNumber, clientAlertSelector) { }
my_hub.client.clientBidsUpdate = function (IDAuction, newState, newDuration, newLastBidder, newPrice, warningNoTokens) { }

似乎问题是SignalR需要在视图页面上实现所有javascript方法,即使它们有空白体。