2017-09-24 25 views
0

我对网络技术相当陌生,我得到了来自互联网的代码段&,并以某种方式设法形成了这段代码。“Uncaught TypeError:无法读取未定义在Websocket Angular JS的属性'延迟'”

我必须通过APIs从websocket获取数据到angularjs中开发的Web应用程序。

这里是代码,

angular.module("MyApp") 
     .service("myService", function ($rootScope, $q) { 
      return { 
       _prevTransmittingStatus: null, 
       transmitting: false, 
       _transmissionStatus: function() {}, 
       socket: null, 
       socket_msg_id: 0, 
       socket_history: {}, 
       url: "ws://localhost:4848/app/", 
       setOnTransmissionStatus: function (callback) { 
        var self = this; 
        this._transmissionStatus = function() { 
         if (self.transmitting != self._prevTransmittingStatus) { 
          self._prevTransmittingStatus = self.transmitting 
          callback(self.transmitting) 
         } else {} 
        } 
       }, 

       connect: function (callback) { 

        var deferred = $q.defer(); 
        var promise = deferred.promise; 

        var self = this 
        this.transmitting = true; 
        this._transmissionStatus(); 
        this.socket = new WebSocket(this.url); 
        this.socket.addEventListener('open', function (e) { 
         self.transmitting = false; 
         self._transmissionStatus(); 
         deferred.resolve("connected") 
        }); 
        this.socket.addEventListener('message', function (e) { 
         var asJson = angular.fromJson(e.data)     
         var replyId = asJson.id; 
         var histItem = self.socket_history[replyId] 
         self.transmitting = false; 
         self._transmissionStatus(); 
         if (angular.isUndefined(asJson.error)) { 
          $rootScope.$apply(histItem.defer.resolve(asJson.result)) 

         } else { 
          **GetActiveDoc();/*how to call this */** 
          console.log("---rejected-----"); 
          $rootScope.$apply(histItem.defer.reject(asJson.error)) 
         } 

        }); 
        return promise; 
       }, 
       disconnect: function() { 
        this.socket.close(); 
        this.transmitting = false; 
        this._transmissionStatus(); 
        return this; 
       }, 
       send: function (msg, scope, callback, onerror) { 
        console.info("SEND:", msg) 
        var _this = this; 
        this.transmitting = true; 
        this._transmissionStatus(); 
        if (!this.socket) { 
         return this.connect().then(
          function (histItem) { 
           console.log("CONNECT SUCCESS", histItem) 


          }, 
          function (histItem) { 
           console.log("CONNECT ERROR", histItem) 
          } 

         ).then(function() { 
          return _this.send(msg, scope); 
         }) 
        } else if (this.socket) { 
         console.log("socket is open") 
         var deferred = $q.defer(); 
         var promise = deferred.promise; 
         msg.id = this.socket_msg_id; 
         this.socket_history[msg.id] = { 
          id: msg.id, 
          defer: deferred, 
          scope: scope, 
          msg: msg 
         }; 
         this.socket_msg_id++ 
         this.socket.send(angular.toJson(msg)) 
         return promise 
        } 
       }, 
       isConnected: function() { 
        return this.socket ? true : false; 
       } 
      } 
     }); 

我只在第一次当我打开我的web应用程序收到一个错误"Uncaught TypeError: Cannot read property 'defer' of undefined at Websocket"。 有人可以解释如何解决这个问题。

回答

0

解决它自己,只是添加/修改的代码如下,

if(replyId >= 0){ 
     $rootScope.$apply(histItem.defer.resolve(asJson.result))       
     } 
相关问题