0

我正在尝试在我的 React 项目中使用 Playfab SDK,但还没有成功..

我找不到解释清楚的文档。

  • 创建了一个反应应用程序:
npx create-react-app playfabtest
  • 为 nodejs 安装 PlayFab SDK:

    npm 安装 playfab-sdk

注意:(还有另一个用于 JavaScript 的 SDK(npm install playfab-web-sdk),但我假设对于反应应用程序,节点是正确的?)

这些 SDK 的文档

  • 使用以下代码修改了App.js项目中的文件:(实际上以多种方式进行了尝试,我尝试了最后一个也不起作用的方法)。

应用程序.js:

// import { PlayFabClient } from './playfab-sdk/Scripts/PlayFab/PlayFabClient';

// var PlayFabClient = require('./PlayFabSdk/Scripts/PlayFab/PlayFabClient.js')

function App() {
  const PlayFabClient = require('./PlayFabSdk/Scripts/PlayFab/PlayFabClient.js');

  PlayFabClient.settings.titleId = '';
  PlayFabClient.settings.developerSecretKey = '';

  PlayFabClient.GetTitleData({ Keys: ['Sample'] }, function (error, result) {
    if (error) {
      console.log('Got an error: ', error);
      return;
    }

    console.log('Reply: ', result);
  });

  return ...
}

  • 之后,如果我运行: npm start

得到这个错误:


错误:

Compiled with problems:

ERROR in ./src/PlayFabSdk/Scripts/PlayFab/PlayFab.js 4:10-24

Module not found: Error: Can't resolve 'url' in 'D:\Github\playfabtest\src\PlayFabSdk\Scripts\PlayFab'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "url": require.resolve("url/") }'
    - install 'url' If you don't want to include a polyfill, you can use an empty module like this:     resolve.fallback: { "url": false }


ERROR in ./src/PlayFabSdk/Scripts/PlayFab/PlayFab.js 6:12-28

Module not found: Error: Can't resolve 'https' in 'D:\Github\playfabtest\src\PlayFabSdk\Scripts\PlayFab'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "https": require.resolve("https-browserify") }'
    - install 'https-browserify' If you don't want to include a polyfill, you can use an empty module like this:    resolve.fallback: { "https": false }

如果可以解释如何使 PlayfabSDK 在 React 应用程序上运行良好,将会非常有帮助。

非常感谢!

4

1 回答 1

0

在 Create React App 的上下文中,您需要使用ES6 样式import语句,而不是requireplayfab-sdk NPM 包导入您的项目。此外,您需要在PlayFab对象上设置设置,而不是PlayFabClient.

例子:

import { useEffect, useState } from 'react';
import { PlayFab, PlayFabClient } from 'playfab-sdk'

PlayFab.settings.titleId = process.env.REACT_APP_PLAYFAB_TITLE_ID || ''
PlayFab.settings.developerSecretKey = process.env.REACT_APP_PLAYFAB_DEVELOPER_SECRET_KEY

// continue with component implementation
const App {
  // get title data
  PlayFabClient.GetTitleData(...)
  // etc...
}

export default App
于 2022-01-19T17:51:17.160 回答