如何在整个应用程序(使用xmpp.js)中打开XMPP 连接?

How to open the XMPP connection in react native throughout the entire app (using xmpp.js)?

我在 React Native 中使用 XMPP.js 并使用网络套接字连接 xmpp 服务器。我可以成功连接到聊天服务器,但我必须导航到应用程序内的不同页面,所以我有 2 个选项:

  1. 每次导航到新页面(使用反应导航进行导航)并关闭旧连接时,我都必须打开一个到 xmpp 聊天服务器的新 XMPP 连接[我认为它不推荐]

  2. 我必须在开始时打开连接,并在整个应用程序中使用相同的连接。但是如何实现呢?

是否有任何替代方案,例如 React Native 中的服务,我可以使用它在后端打开 XMPP 连接,并且它必须在整个应用程序中工作。我需要 Android+IOS

的可行解决方案

您必须在开始时打开连接,并在整个应用程序中使用相同的连接。您需要有一些 class 之类的单例来容纳 xmpp 客户端,因此您将在整个应用程序中使用它。或者您甚至可以尝试使用 xmll 客户端在 App.js 中保存一个全局变量。

例如,这里是 ConnectyCube ReactNative SDK https://developers.connectycube.com/js/react-native there is a simple prototype object which holds an xmpp client. Then this object can be used in a flux container or as a global variable, something like here How to use global variables in React Native?