2017-10-10 364 views
-1

我終於得到了我的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錯誤,直到我修正了這個錯誤。他們的答案基本上是「它應該工作」。這就是爲什麼我來真正的專業人士。 –

回答

0

我重新訪問了SDK,它看起來像開發人員發佈了更新。我是,奇怪地使用更新的JavaScript,但沒有更新的HTML文件引用新的JS。憑藉API密鑰和祕密,我現在可以完美握手。

沒有開始定製UI和內容的漫長戰鬥!

相關問題