我正在尝试使用React Native构建聊天应用程序,并尝试使用Azure Web Pub Sub来实现这一点。我正在遵循文档,因此我将@azure/web-pubsub依赖项添加到我的项目中,并复制了我在文档中找到的这段代码:
// other react native imports...
const WebSocket = require('ws');
const { WebPubSubServiceClient } = require('@azure/web-pubsub');
async function main() {
const hub = "pubsub";
let service = new WebPubSubServiceClient(process.env.WebPubSubConnectionString, hub);
let token = await service.getClientAccessToken();
let ws = new WebSocket(token.url);
ws.on('open', () => console.log('connected'));
ws.on('message', data => console.log('Message received: %s', data));
}
我在Android上运行应用程序,我得到这个错误,告诉我在这些目录中找不到url依赖项:节点模块@azure\网络发布子\节点模块
下面是来自控制台的完整错误:
error: Error: Unable to resolve module url from C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\@azure\web-pubsub\dist\index.js: url could not be found within the project or in these directories:
node_modules\@azure\web-pubsub\node_modules
node_modules
..\..\..\..\node_modules
11 | var jwt = _interopDefault(require('jsonwebtoken'));
12 | var logger$1 = require('@azure/logger');
> 13 | var url = require('url');
| ^
14 |
15 | /*
16 | * Copyright (c) Microsoft Corporation.
at ModuleResolver.resolveDependency (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:158:15)
at DependencyGraph.resolveDependency (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\node-haste\DependencyGraph.js:231:43)
at Object.resolve (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\lib\transformHelpers.js:129:24)
at resolve (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\DeltaBundler\traverseDependencies.js:396:33)
at C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\DeltaBundler\traverseDependencies.js:412:26
at Array.reduce (<anonymous>)
at resolveDependencies (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\DeltaBundler\traverseDependencies.js:411:33)
at processModule (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\DeltaBundler\traverseDependencies.js:140:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async addDependency (C:\Users\abdou\source\Mobile Apps\Test app\AwesomeTSProject\node_modules\metro\src\DeltaBundler\traverseDependencies.js:230:18)
1条答案
按热度按时间cclgggtu1#
所以我问是不可能使用Azure Web PubSub服务与React Native,还是我应该只使用ws包与PubSub服务通信?如果是这样,怎么做?
根据vicancy:
@azure/web-pubsub
包,只需使用ws
包与服务通信。根据abdou-tech:
@azure/web-pubsub
,该软件包适用于希望连接到Azure PubSub服务的节点客户端。ws
包也不能在React Native或浏览器中运行,它需要节点运行时。可以使用本机WebSocket
对象连接到服务