Kadir Kalkan
Kadir Kalkan

Reputation: 248

JS Client doesn't invoke Method on SignalR Hub

I have a MVC project which has 2 button and 6 textbox. I want to call different methods on a Hub.

Client :

$(document).ready(function () {
            var bitcoinHub = $.connection.bitcoinHub;

            $("#btnBuy").click(function () {
                var tl = document.getElementById("bitcoinBuy").value;
                var btc = document.getElementById("aBitcoin").value;
                var total = document.getElementById("aTotal").value;
                bitcoinHub.server.trade("buy",@Model.user,tl,btc,total);
            });

            bitcoinHub.client.broadcastSell = function (model) {
                //   $("#sellGrid").append("<li>"+model.Sell+"</li>");
            };

            $("#btnSell").click(function () {
                var tl = document.getElementById("bitcoinSell").value;
                var btc = document.getElementById("sBitcoin").value;
                var total = document.getElementById("sTotal").value;
                bitcoinHub.server.trade("sell",@Model.user,tl,btc,total);
            });

            bitcoinHub.client.broadcastPurchase = function (model) {
                // $("#buyGrid").append("<li>"+model.Buy+"</li>");
            };
            $.connection.hub.start();   });

In the case I press "btnBuy" or "btnCell" it should invoke the trade methode on the server with different parameters.

Server:

BitcoinHub(name) Class Method

 public void trade(string parametre, Users cUser, string tl, string btc, string total)
    {
        switch (parametre)
        {
            case "sell":
                // Do something not important here
                break;
            case "buy":
                // Do something not important here
                break;
        }
    }

In the case I press "btnBuy" or "btnCell" it goes trough the clickeventhandler. The problem is that the method on the hub will not be called.

Upvotes: 0

Views: 371

Answers (1)

Stephu
Stephu

Reputation: 3334

You can't use the model in JS like that @Model.user. See Accessing MVC's model property from Javascript for details

Additional rearrange your code. Do not invoke a method on server until connection is established:

$(document).ready(function () {
                var bitcoinHub = $.connection.bitcoinHub;           

                bitcoinHub.client.broadcastSell = function (model) {
                    //   $("#sellGrid").append("<li>"+model.Sell+"</li>");
                };

                bitcoinHub.client.broadcastPurchase = function (model) {
                    // $("#buyGrid").append("<li>"+model.Buy+"</li>");
                };
                $.connection.hub.start().done(function(){
                    // DO not call methods on server until connection is established.
                    $("#btnBuy").click(function () {
                        var tl = document.getElementById("bitcoinBuy").value;
                        var btc = document.getElementById("aBitcoin").value;
                        var total = document.getElementById("aTotal").value;
                        bitcoinHub.server.trade("buy",@Model.user,tl,btc,total);
                    });
                    $("#btnSell").click(function () {
                        var tl = document.getElementById("bitcoinSell").value;
                        var btc = document.getElementById("sBitcoin").value;
                        var total = document.getElementById("sTotal").value;
                        bitcoinHub.server.trade("sell",@Model.user,tl,btc,total);
                });
    });   });

Upvotes: 1

Related Questions