ReactJS 构造失败 'WebSocket': 子协议 '[object Object]' 无效

ReactJS Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid

我在使用 enigma.js (https://qlik.dev/apis/javascript/enigmajs) 的 React 应用程序中遇到以下错误。我正在尝试初始化 WebSocket 连接,但收到错误消息。 “构建失败 'WebSocket':子协议‘[object Object]’无效”。

WebSocket 连接 URL 是正确的,因为可以使用 return 数据的 https://catwalk.core.qlik.com/?engine_url=wss://sense-demo.qlik.com/app/133dab5d-8f56-4d40-b3e0-a6b401391bde 进行测试。您可以尝试编辑 URL,这会 return 一个错误。

密码是

async init() {
    
const appId = "133dab5d-8f56-4d40-b3e0-a6b401391bde";
    const url =
      "wss://sense-demo.qlik.com/app/133dab5d-8f56-4d40-b3e0-a6b401391bde"; 

    const session = enigma.create({
      schema,
      createSocket: () =>
        new WebSocket(url, {
        }),
    });

    const global = await session.open();
    const app = await global.openDoc(appId);
    const appLayout = await app.getAppLayout();

    console.log(appLayout);

  }

我找到了解决方案: qDoc.config.js

const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const SenseUtilities = require('enigma.js/sense-utilities');

const config = {
  host: 'sense-demo.qlik.com',
  secure: true,
  port: 443,
  prefix: '',
  appId: '133dab5d-8f56-4d40-b3e0-a6b401391bde',
};

const url = SenseUtilities.buildUrl(config);

async function init() {
  const session = enigma.create({
    schema,
    url,
    suspendOnClose: true,
  });

  const global = await session.open();
  const app = await global.openDoc(config.appId);
  const appLayout = await app.getAppLayout();

  console.log(appLayout);
}
init();

const session = enigma.create({ schema, url, suspendOnClose: true });

// open doc and return promise which will resolve to doc
export const openDoc = () => (
  session.open().then((global) => global.openDoc(config.appId))
);

// close session
export const closeSession = () => (
  session.close()
);

说明

  1. 下载这个project
  2. 删除package-lock.json 文件
  3. npm i
  4. npm run-script dev
  • 这是目录视图:

  • 这是结果日志:

这里解释了解决方法

https://github.com/qlik-oss/enigma.js/issues/889