信号器关闭连接

新手上路,请多包涵

我正在尝试在我的网络应用程序中创建一个停止按钮。 Web 应用程序创建不同文件的批量快捷方式。我试过使用 $.connection.shortcutHub.stop() 但这出现了一个错误说 Cannot read property 'shortcutHub' of undefined(anonymous function)

代码如下。单击 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 网站),但我仍然遇到同样的问题。

原文由 spovelec 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 311
2 个回答

你有没有尝试过:

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

它会起作用。

原文由 Ujjwal Kumar Gupta 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是我正在使用的工作代码:

 let connection;
let connectionUrl = 'https://someurl/hubEndpoint';

connection = new signalR.HubConnectionBuilder()
    .withUrl(connectionUrl)
    .build();

connection.serverTimeoutInMilliseconds = 60 * 10000;

connection.on("ReceiveMessage", (message) => {
    console.log(message);
    // to do appropriate coding
});

connection.start().then(function () {
    console.log('Connected to server');
    subject = new signalR.Subject();
});

setTimeout(() => {
    connection.stop().then(function() {
        console.log('Closed');
        connection = null;
    });
}, (2000));

原文由 Venugopal M 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题