简体   繁体   English

在 ReactNative 中使用 Strophe.js

[英]Using Strophe.js with ReactNative

I am trying to build a ReactNative chat app.我正在尝试构建一个 ReactNative 聊天应用程序。 The backend consists of a Prosody XMPP/Bosh server.后端由 Prosody XMPP/Bosh 服务器组成。 Now I want to connect by using the Strophe.js Javascript library.现在我想使用 Strophe.js Javascript 库进行连接。 The problem that I have is that the Strophe.js library depends on the global document variable for generating XML nodes.我遇到的问题是 Strophe.js 库依赖于用于生成 XML 节点的全局document变量。 Did anyone of you already have the same problem or has a clue how might get it working?你们中有人已经遇到过同样的问题,或者知道如何让它工作吗?

I found a working solution: Im using a ReactNative WebView running parallel and not Visible to my ReactNativeApp, similar to this Crypto (but with ReactNativeWebview! not React Native WebView Javascript Bridge)我找到了一个可行的解决方案:我使用并行运行的 ReactNative WebView,对我的 ReactNativeApp 不可见,类似于这个 Crypto(但使用 ReactNativeWebview!不是 React Native WebView Javascript Bridge)

In the WebView you can use Strophe, you just need to write a Wrapper for the Communication between RN and Strophe in the WebView.在WebView中可以使用Strophe,只需要在WebView中为RN和Strophe之间的通信编写一个Wrapper即可。

I'll try to make gitRepo with a working Basic Version我将尝试使用可工作的基本版本制作 gitRepo

(Check the Crypto Example: https://github.com/saulshanabrook/react-native-webview-crypto ) (检查加密示例: https : //github.com/saulshanabrook/react-native-webview-crypto

Javascript engine used in ReactNative is headless, meaning it does not provide global objects like navigator , document , location , etc. Other than that the browser is fully functional and has all XHR functionalities of a normal browser. ReactNative 中使用的 Javascript 引擎是无头的,这意味着它不提供全局对象,如navigatordocumentlocation等。除此之外,浏览器功能齐全,并具有普通浏览器的所有XHR功能。

Storephe's communication with the server is all based on XHR requests. Storephe 与服务器的通信都是基于 XHR 请求的。 You only need to mock the document object to prevent Strophe from raising an exception when it can't find the document object.您只需要模拟文档对象,以防止 Strophe 在找不到document对象时引发异常。 mock-browser build for that exact reason.出于这个确切原因,模拟浏览器构建。 A simple example of how to use it:如何使用它的一个简单示例:

let MockBrowser = require('mock-browser').mocks.MockBrowser;
let mock = new MockBrowser();
GLOBAL.window = mock.getWindow()
GLOBAL.document = mock.getDocument()
GLOBAL.navigator = mock.getNavigator()

There are a few xmpp javscript libraries which may work with react-native.有一些 xmpp javscript 库可以与 react-native 一起使用。 But they require further configuration.但它们需要进一步配置。

There are also quite actively maintained xmpp libraries for react native.还有一些非常积极维护的 xmpp 库用于 react native。 XMPP.js is one of them. XMPP.js就是其中之一。 They also provide plugin support.他们还提供插件支持。 But, they are quite early in development and of course libraries like strophe.js are much more advanced.但是,它们还处于开发初期,当然像 strophe.js 这样的库要先进得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM