2016-07-28 221 views
0

我正在尝试在我的webapp中创建一个停止按钮。 webapp为不同的文件创建批量快捷方式。我曾尝试使用$.connection.shortcutHub.stop()但是,这出现了一个错误说Cannot read property 'shortcutHub' of undefined(anonymous function)Signalr关闭连接

代码如下。一旦单击stop按钮,我需要停止连接。停止按钮的ID是stopButton

 $(document).ready(function() { 
     // initialize the connection to the server 
     var progressNotifier = $.connection.shortcutHub; 

     // client-side sendMessage function that will be called from the server-side 
     progressNotifier.client.sendMessage = function (message, percent) { 
      // update progress 
      UpdateMessage(message, percent); 
     }; 

     progressNotifier.client.redo = function() { 
      redo(); 
     }; 

     progressNotifier.client.success = function() { 
      success(); 
     }; 

     progressNotifier.client.fail = function() { 
      fail(); 
     }; 


     // establish the connection to the server and start server-side operation 
     $.connection.hub.start().done(function() { 
      $('#confirmbutton').click(function() { 
       jQuery.noConflict(); 
       document.getElementById('closeButton').setAttribute("class", "btn btn-default hidden"); 
       $('#myModal').modal('show'); 
       //document.getElementById('confirmbutton').disabled = true; 
       //document.getElementById('barcodepanel').setAttribute("class", "panel panel-default"); 
       var ticket = getCookie('ticket'); 
       var path = getCookie('CBSShortcut_Path'); 
       var checkeddocs = getCheckedBoxes("dcheck"); 
       var checkedfolders = getCheckedBoxes("fcheck"); 
       progressNotifier.server.createshortcuts(ticket, path, checkeddocs, checkedfolders); 
      }); 

      $('#stopButton').click(function() { 
       document.getElementById('closeButton').setAttribute("class", "btn btn-default"); 
       document.getElementById('confirmbutton').disabled = false; 


       //What do I put here? 
      }); 



     }); 



     function UpdateMessage(message, percent) { 
      // get result div 
      var msg = $("#result"); 
      // set message 
      msg.html(message); 
      //set value of progress bar 
      document.getElementById('closeButton').setAttribute("class", "btn btn-default hidden") 
      $('#progressbar').css('width', percent + '%').attr('aria-valuenow', percent); 
     } 

     function getCookie(cname) { 
      var name = cname + "="; 
      var ca = document.cookie.split(';'); 
      for (var i = 0; i < ca.length; i++) { 
       var c = ca[i]; 
       while (c.charAt(0) == ' ') c = c.substring(1); 
       if (c.indexOf(name) == 0) return c.substring(name.length, c.length); 
      } 
      return ""; 
     } 

     function redo() { 
      document.getElementById('confirmbutton').disabled = false; 
      jQuery.noConflict(); 
      $('#myModal').modal('hide'); 
     } 


     // Pass the checkbox name to the function 
     function getCheckedBoxes(chkboxclass) { 
      var checkboxes = document.getElementsByClassName(chkboxclass); 
      var checkboxesChecked = []; 
      var ids = ""; 
      // loop over them all 
      for (var i = 0; i < checkboxes.length; i++) { 
       // And stick the checked ones onto an array... 
       if (checkboxes[i].checked) { 
        checkboxesChecked.push(checkboxes[i]); 
        ids = ids + checkboxes[i].getAttribute("Name") + ","; 
       } 
      } 
      // Return the array if it is non-empty, or null 
      //return checkboxesChecked.length > 0 ? checkboxesChecked : null; 
      return ids; 
     } 
    } 
);` 

任何帮助表示赞赏。我已经尝试了一切,谷歌已经抛出我的方式(这主要是stackoverflow网站),我仍然有同样的问题。

回答

2

你试过:

$.connection.hub.stop().done(function() { 
    alert('stopped'); 
}); 

它会奏效。

+0

我会在哪里把这个?我在一些地方尝试过,但是我一直得到相同的错误。 – JohZant

+0

好的。当你开始连接时,把$ .connection放在一个变量中,稍后使用该变量来像这样停下来 - var ChatConnection = $。connection;当你想停止使用ChatConnection.stop();注意: - 保持变量不变。 –

+0

我已将'progressNotifier'分配为保存连接的全局变量。当我输入'progressNotifier.stop();'它告诉我,它不是一个函数。 – JohZant

1

你想,因为集线器共享一个连接使用全局SignalR中心客户端连接(又名不使用progressNotifier与连接做任何事情,只侦听和发送事件。)

你的代码测试这可能看起来像:

$('#stopButton').click(function() { 
       document.getElementById('closeButton').setAttribute("class", "btn btn-default"); 
       document.getElementById('confirmbutton').disabled = false; 


       $.connection.hub.stop(); 
       //try to send a server event. Will throw an error 
       //Uncaught Error: SignalR: Connection must be started before data can be sent. Call .start() before .send() 
      }); 
+0

我得到'Uncaught TypeError:无法读取未定义的属性'hub'。 – JohZant

+0

如果您的代码与问题中的确切位置相同,您是否可以在控制台中粘贴带有错误的屏幕截图? –

+0

http://imgur.com/a/CGlbD - 我希望这有助于 – JohZant