Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 信号员和AngularJS_.net_Angularjs_Signalr - Fatal编程技术网

.net 信号员和AngularJS

.net 信号员和AngularJS,.net,angularjs,signalr,.net,Angularjs,Signalr,我在我的应用程序中使用SignalR,它可以与简单的javascript一起工作,但现在我试图用angular wrapper重写它,但在尝试连接到我的集线器时遇到了错误: Error: SignalR: Connection must be started before data can be sent. Call .start() before .send() at Object.signalR.fn.signalR.send (http://localhost:47124/Scripts/

我在我的应用程序中使用SignalR,它可以与简单的javascript一起工作,但现在我试图用angular wrapper重写它,但在尝试连接到我的集线器时遇到了错误:

Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()
at Object.signalR.fn.signalR.send (http://localhost:47124/Scripts/jquery.signalR-2.1.1.js:789:23)
at Object.hubProxy.fn.hubProxy.invoke (http://localhost:47124/Scripts/jquery.signalR-2.1.1.js:2609:24)
at Hub.invoke (http://localhost:47124/Scripts/angular-signalr-hub.js:30:28)
at Hub.(anonymous function) [as loadRecentPhotos] (http://localhost:47124/Scripts/angular-signalr-hub.js:49:24)
at Object.PhotoMarkers.load (http://localhost:47124/Scripts/AngApp.js:47:17)
at http://localhost:47124/Scripts/angular.js:10836:21
at Scope.$eval (http://localhost:47124/Scripts/angular.js:12673:28)
at pre (http://localhost:47124/Scripts/angular.js:19943:15)
at nodeLinkFn (http://localhost:47124/Scripts/angular.js:6684:13)
at compositeLinkFn (http://localhost:47124/Scripts/angular.js:6098:13) <div class="modal-body" ng-init="markers.load()"> 

有人知道吗?

根据信号集线器项目的源代码,在创建新集线器时应启动连接。是否已检查浏览器控制台是否存在可能的连接错误

该库还提供对
start()
promise的访问。请遵守此承诺,检查集线器是否已成功连接到服务器。例如:

hub.promise
    .done(function(){ console.log('connection established'); })
    .fail(function(){ console.log('connection error'); });

promise对象是对signalR start方法的promise的访问。

您一定遇到了异步问题。调用方法时,连接尚未打开

尝试修复你的
signarhub.js
,我认为我们应该使用不同的停止和启动承诺

    Hub.disconnect = function () {
        Hub.stopPromise = Hub.connection.stop();
    };
    Hub.connect = function () {
        Hub.startPromise = Hub.connection.start();
    };
以及:

或:

不要忘记将
$q
注入您的中心。这将返回一个角度承诺,而不是jQuery承诺。因此,angular知道该事件,我们不需要在回调中使用
$rootScope.$apply()

以及:

对此进行了讨论

vmlf01的评论如下:

创建的中心有一个“.promise”属性,该属性对应于“.start()”的返回值。我使用承诺初始化连接,并在建立连接时在done()回调中解析它,如下所示:

    function initialize() {
        var deferred = $q.defer();

        foldersHubConnection = new Hub('Folders', {
            listeners: folderHubListeners(),
            methods: folderServerCalls(),
            rootPath: config.eventsServerEndpoint
        });

        foldersHubConnection.promise.done(function () {
            deferred.resolve(foldersHubClient);
        });

        foldersHubConnection.promise.fail(function (error) {
            deferred.reject(new Error("Error connecting to Events Server: " + error));
        });

        return deferred.promise;
    }

我在Github上有一个基本的工作示例。 希望能有帮助。虽然它确实使用身份验证,但这对您来说并不重要。 您只需删除身份验证部分

问候


Louis

确保安装并参考jquery.signalR-2.1.2.js文件

包装signalR供AngularJS使用时,帮助我的是:

**将所有信号器(jQuery)承诺转换为角度$q承诺。**

好处:

  • 所有调用者都有一个一致的承诺接口(例如,
    finally()
    而不是
    always()

  • 承诺因错误而被拒绝(无需
    尝试
    捕获
    ),例如,根据您的问题,如果未启动连接

  • 角度的
    $digest
    循环被触发

  • 记住这一点,您可以将
    Hub.invoke
    方法更改为:

    Hub.invoke = function(method, args) {
        try {
           var jQueryPromise = Hub.proxy.invoke.apply(Hub.proxy, arguments);
    
            return $q.when(jQueryPromise);    // <- return as $q promise
    
        } catch(e) {
            return $q.reject(e);              // <- exception to $q.reject
        }
    

    错误消息说调用
    start
    。你试过了吗?正如我所理解的,这个包装器(angular service)的想法是控制hub启动并简化客户端上hub侦听器和方法的声明。这与signalr无关,但为什么要使用jquery模式,尝试使用angularJS指令来做好这一点,我在angular方面是新手,只做第一步。但是下次我会用的,谢谢@AlexanderSmith如果您愿意使用其他包装解决方案,我可以发布一个更灵活的定制包装,请告诉我!这正是我在答覆中指出的问题。只是解决方案不同而已。
    angular.forEach(options.methods, function (method) {
            Hub[method] = function () {
                var args = $.makeArray(arguments);
                args.unshift(method);
    
                var def = $q.def();
                Hub.startPromise.then(function(){
                        //invoke method only after startPromise is resolved
                      Hub.invoke.apply(Hub, args).done(function(){
                             def.resolve();
                      });
                }
    
                return def.promise;
            };
     });
    
    //Adding additional property of promise allows to access it in rest of the application.
       Hub.startPromise = Hub.connection.start();
    
        function initialize() {
            var deferred = $q.defer();
    
            foldersHubConnection = new Hub('Folders', {
                listeners: folderHubListeners(),
                methods: folderServerCalls(),
                rootPath: config.eventsServerEndpoint
            });
    
            foldersHubConnection.promise.done(function () {
                deferred.resolve(foldersHubClient);
            });
    
            foldersHubConnection.promise.fail(function (error) {
                deferred.reject(new Error("Error connecting to Events Server: " + error));
            });
    
            return deferred.promise;
        }
    
    Hub.invoke = function(method, args) {
        try {
           var jQueryPromise = Hub.proxy.invoke.apply(Hub.proxy, arguments);
    
            return $q.when(jQueryPromise);    // <- return as $q promise
    
        } catch(e) {
            return $q.reject(e);              // <- exception to $q.reject
        }
    
    Hub.connect = function (queryParams) {
        ...
        return $q.when(Hub.connection.start(startOptions));  // <- return $q promise
    };