未捕获TypeError:无法读取未定义的属性'indexOf'

未捕获TypeError:无法读取未定义的属性'indexOf'

问题描述:

我终于得到了我的javascript引用没有404。我填写到html表单的第一个信息给我控制台错误。未捕获TypeError:无法读取未定义的属性'indexOf'

这里是我的JavaScript是导致我一个问题:

/* global define, module, require */ 
(function (root, factory) { 
    if (typeof define === 'function' && define.amd) { 
     // AMD. Register as an anonymous module. 
     define(['crypto-js', 'ws'], factory); 
    } else if (typeof module === 'object' && module.exports) { 
     // Node. Export. 
     module.exports = factory(require('crypto-js'), require('ws')); 
    } else { 
     // Browser globals (root is window) 
     root.GameSparks = factory(root.CryptoJS, root.WebSocket || root.MozWebSocket); 
    } 
}(this, function(CryptoJS, WebSocket) { 

var GameSparks = function() {}; 

GameSparks.prototype = { 

    init: function(options) { 
     this.options = options; 
     this.socketUrl = options.url; 

     this.pendingRequests = {}; 
     this.requestCounter = 0; 

     this.connect(); 
    }, 

    buildServiceUrl: function(live, options) { 
     var stage; 
     var urlAddition = options.key; 
     var credential; 
     var index; 

     if (live) { 
      stage = "live"; 
     } else { 
      stage = "preview"; 
     } 

     if (!options.credential || options.credential.length === 0) { 
      credential = "device"; 
     } else { 
      credential = options.credential; 
     } 

     index = options.secret.indexOf(":"); 
     if (index > 0) { 
      credential = "secure"; 

      urlAddition = options.secret.substr(0, index) + "/" + urlAddition; 
     } 

     return "wss://" + stage + "-" + urlAddition + ".ws.gamesparks.net/ws/" + credential + "/" + urlAddition; 
    }, 

    initPreview: function(options) { 
     options.url = this.buildServiceUrl(false, options); 
     this.init(options); 
    }, 

    initLive: function(options) { 
     options.url = this.buildServiceUrl(true, options); 
     this.init(options); 
    }, 

    reset: function() { 
     this.initialised = false; 
     this.connected = false; 
     this.error = false; 
     this.disconnected = false; 

     if (this.webSocket != null){ 
      this.webSocket.onclose = null; 
      this.webSocket.close(); 
     } 
    }, 

    connect: function() { 
     this.reset(); 

     try { 
      this.webSocket = new WebSocket(this.socketUrl); 
      this.webSocket.onopen = this.onWebSocketOpen.bind(this); 
      this.webSocket.onclose = this.onWebSocketClose.bind(this); 
      this.webSocket.onerror = this.onWebSocketError.bind(this); 
      this.webSocket.onmessage = this.onWebSocketMessage.bind(this); 
     } catch(e) { 
      this.log(e.message); 
     } 
    }, 

    disconnect: function() { 
     if (this.webSocket && this.connected) { 
      this.disconnected = true; 
      this.webSocket.close(); 
     } 
    }, 

    onWebSocketOpen: function(ev) { 
     this.log('WebSocket onOpen'); 

     if (this.options.onOpen) { 
      this.options.onOpen(ev); 
     } 

     this.connected = true; 
    }, 

    onWebSocketClose: function(ev) { 
     this.log('WebSocket onClose'); 

     if (this.options.onClose) { 
      this.options.onClose(ev); 
     } 

     this.connected = false; 

     // Attemp a re-connection if not in error state or deliberately disconnected. 
     if (!this.error && !this.disconnected) { 
      this.connect(); 
     } 
    }, 

    onWebSocketError: function(ev) { 

     this.log('WebSocket onError: Sorry, but there is some problem with your socket or the server is down'); 

     if (this.options.onError) { 
      this.options.onError(ev); 
     } 

     // Reset the socketUrl to the original. 
     this.socketUrl = this.options.url; 

     this.error = true; 
    }, 

    onWebSocketMessage: function(message) { 
     this.log('WebSocket onMessage: ' + message.data); 

     var result; 
     try { 
      result = JSON.parse(message.data); 
     } catch (e) { 
      this.log('An error ocurred while parsing the JSON Data: ' + message + '; Error: ' + e); 
      return; 
     } 

     if (this.options.onMessage) { 
      this.options.onMessage(result); 
     } 

     // Extract any auth token. 
     if (result['authToken']) { 
      this.authToken = result['authToken']; 
      delete result['authToken']; 
     } 

     if (result['connectUrl']) { 
      // Any time a connectUrl is in the response we should update and reconnect. 
      this.socketUrl = result['connectUrl']; 
      this.connect(); 
     } 

     var resultType = result['@class']; 

     if (resultType === '.AuthenticatedConnectResponse') { 
      this.handshake(result); 
     } else if (resultType.match(/Response$/)){ 
      if (result['requestId']) { 
       var requestId = result['requestId']; 
       delete result['requestId']; 

       if (this.pendingRequests[requestId]) { 
        this.pendingRequests[requestId](result); 
        this.pendingRequests[requestId] = null; 
       } 
      } 
     } 

    }, 

    handshake: function(result) { 

     if (result['nonce']) { 

      var hmac; 

      if (this.options.onNonce) { 
       hmac = this.options.onNonce(result['nonce']); 
      } else { 
       hmac = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(result['nonce'], this.options.secret)); 
      } 

      var toSend = { 
       '@class' : '.AuthenticatedConnectRequest', 
       hmac : hmac 
      }; 

      if (this.authToken) { 
       toSend.authToken = this.authToken; 
      } 

      if (this.sessionId) { 
       toSend.sessionId = this.sessionId; 
      } 

      const browserData = this.getBrowserData(); 
      toSend.platform = browserData.browser; 
      toSend.os = browserData.operatingSystem; 

      this.webSocketSend(toSend); 

     } else if (result['sessionId']) { 
      this.sessionId = result['sessionId']; 
      this.initialised = true; 

      if (this.options.onInit) { 
       this.options.onInit(); 
      } 

      this.keepAliveInterval = window.setInterval(this.keepAlive.bind(this), 30000); 
     } 
    }, 

    keepAlive: function() { 
     if (this.initialised && this.connected) { 
      this.webSocket.send(' '); 
     } 
    }, 

    send: function(requestType, onResponse){ 
     this.sendWithData(requestType, {}, onResponse); 
    }, 

    sendWithData: function(requestType, json, onResponse) { 
     if (!this.initialised) { 
      onResponse({ error: 'NOT_INITIALISED' }); 
      return; 
     } 

     // Ensure requestType starts with a dot. 
     if (requestType.indexOf('.') !== 0) { 
      requestType = '.' + requestType; 
     } 

     json['@class'] = requestType; 

     json.requestId = (new Date()).getTime() + "_" + (++this.requestCounter); 

     if (onResponse != null) { 
      this.pendingRequests[json.requestId] = onResponse; 
      // Time out handler. 
      setTimeout((function() { 
       if (this.pendingRequests[json.requestId]) { 
        this.pendingRequests[json.requestId]({ error: 'NO_RESPONSE' }); 
       } 
      }).bind(this), 32000); 
     } 

     this.webSocketSend(json); 
    }, 

    webSocketSend: function(data) { 

     if (this.options.onSend) { 
      this.options.onSend(data); 
     } 

     var requestString = JSON.stringify(data); 
     this.log('WebSocket send: ' + requestString); 
     this.webSocket.send(requestString); 
    }, 

    getSocketUrl: function() { 
     return this.socketUrl; 
    }, 

    getSessionId: function() { 
     return this.sessionId; 
    }, 

    getAuthToken: function() { 
     return this.authToken; 
    }, 

    setAuthToken: function(authToken) { 
     this.authToken = authToken; 
    }, 

    isConnected: function() { 
     return this.connected; 
    }, 

    log: function(message) { 
     if (this.options.logger) { 
      this.options.logger(message); 
     } 
    }, 

    getBrowserData: function() { 

     var browsers = [ 
      { 
       string: navigator.userAgent, 
       subString: 'Chrome', 
       identity: 'Chrome' 
      }, 
      { string: navigator.userAgent, 
       subString: 'OmniWeb', 
       versionSearch: 'OmniWeb/', 
       identity: 'OmniWeb' 
      }, 
      { 
       string: navigator.vendor, 
       subString: 'Apple', 
       identity: 'Safari', 
       versionSearch: 'Version' 
      }, 
      { 
       prop: window.opera, 
       identity: 'Opera', 
       versionSearch: 'Version' 
      }, 
      { 
       string: navigator.vendor, 
       subString: 'iCab', 
       identity: 'iCab' 
      }, 
      { 
       string: navigator.vendor, 
       subString: 'KDE', 
       identity: 'Konqueror' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'Firefox', 
       identity: 'Firefox' 
      }, 
      { 
       string: navigator.vendor, 
       subString: 'Camino', 
       identity: 'Camino' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'Netscape', 
       identity: 'Netscape' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'MSIE', 
       identity: 'Explorer', 
       versionSearch: 'MSIE' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'Gecko', 
       identity: 'Mozilla', 
       versionSearch: 'rv' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'Mozilla', 
       identity: 'Netscape', 
       versionSearch: 'Mozilla' 
      } 
     ]; 

     var operatingSystems = [ 
      { 
       string: navigator.platform, 
       subString: 'Win', 
       identity: 'Windows' 
      }, 
      { 
       string: navigator.platform, 
       subString: 'Mac', 
       identity: 'Mac' 
      }, 
      { 
       string: navigator.userAgent, 
       subString: 'iPhone', 
       identity: 'iPhone/iPod' 
      }, 
      { 
       string: navigator.platform, 
       subString: 'Linux', 
       identity: 'Linux' 
      } 
     ]; 

     function searchForIdentity(data) { 
      for (var i = 0; i < data.length; i++) { 
       var string = data[i].string; 
       var prop = data[i].prop; 

       if (string) { 
        // Look for the sub string in the string. 
        if (string.indexOf(data[i].subString) !== -1) { 
         return data[i].identity; 
        } 
       } else if (prop) { 
        return data[i].identity; 
       } 
      } 
     } 

     return { 
      browser: searchForIdentity(browsers), 
      operatingSystem: searchForIdentity(operatingSystems) 
     }; 
    } 
}; 

return GameSparks; 

})); 

正如你可能看到的,这是给我的问题是该行:

index = options.secret.indexOf(":"); 
      if (index > 0) { 
       credential = "secure"; 

我不知道是什么问题是。它是一个后端服务器,我知道他们建议我使用第三方服务器来加密我的API密钥...

+0

这似乎更特定于GameSparks而不是JavaScript。无论是将选项变量传递给initLive还是initPreview,都不会将options.secret设置为任何内容。 – spectacularbob

+0

我正在骚扰GameSparks的家伙,以获得关于此的具体信息。到目前为止,沉默。所以这是留给我找到一种方式!感谢Tommy O的编辑。 –

+0

我从GameSparks的家伙那里听到了。他们问我是否以任何方式修改了html,而我没有这样做。除了引用我保存java的地方。我之前得到了404错误,直到我修正了这个错误。他们的答案基本上是“它应该工作”。这就是为什么我来真正的专业人士。 –

我重新访问了SDK,它看起来像开发人员发布了更新。我是,奇怪地使用更新的JavaScript,但没有更新的HTML文件引用新的JS。凭借API密钥和秘密,我现在可以完美握手。

没有开始定制UI和内容的漫长战斗!