如何在整个应用程序中打开 React Native 的 XMPP 连接(使用 xmpp.js)? [英] How to open the XMPP connection in react native throughout the entire app (using xmpp.js)?

查看:59
本文介绍了如何在整个应用程序中打开 React Native 的 XMPP 连接(使用 xmpp.js)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 react native 中使用 XMPP.js 并使用 web socket 与 xmpp 服务器连接.我可以成功连接到聊天服务器,但我必须导航到应用程序内的不同页面,所以我有 2 个选项:

I am using XMPP.js inside react native and using web socket to connect with the xmpp server. I can successfully connect with the chat server but I have to navigate to different pages inside the App so I have 2 options:

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

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

I must open the connection in the starting and use the same connection throughout the entire app. But how to achieve that?

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

Is there any alternative like services in react native with which I can open the XMPP connection in the backend and it must work throughout the entire app. I need the feasible solution for both Android+IOS

推荐答案

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

You must open the connection in the starting and use the same connection throughout the entire app. You need to have some class like singleton or so which will hold an xmpp client, so you will use it across the entire app. Or you even can try to hold a global variable inside App.js with xmll client.

例如,在 ConnectyCube ReactNative SDK https://developers.connectycube.com/js/react-native 有一个简单的原型对象,它包含一个 xmpp 客户端.然后这个对象可以在通量容器中使用或作为全局变量使用,就像这里 如何在 React Native 中使用全局变量?

For example, here at 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?

这篇关于如何在整个应用程序中打开 React Native 的 XMPP 连接(使用 xmpp.js)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆