Skip to main content
Changes after discussions with author of question
Source Link
Stephu
  • 3.4k
  • 5
  • 26
  • 33

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); }); }); }); 

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); }); }); }); 
Source Link
Stephu
  • 3.4k
  • 5
  • 26
  • 33

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