无法在本机应用程序中连接到rabbitmq 服务器 [英] Can't connect to rabbitmq server in react-native app

查看:37
本文介绍了无法在本机应用程序中连接到rabbitmq 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 rabbitmq 集成到本机应用程序上

参考这个库:https://www.npmjs.com/package/react-native-rabbitmq

我尝试使用适当的配置属性创建一个 Connection 对象.但是当我尝试检查 let connection = new Connection(config); 的结果时,我收到了以下内容:

Connection {rabbitmqconnection: Object, callbacks: Object, connected: false} 在连接对象中.如您所见,它给出了 connected: false

我已经提到在节点端创建 rabbit mq 服务器:https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html 来创建一个正常工作的节点兔 mq 服务器.现在我需要在客户端接收消息

我确定配置属性 &rabbitmq 服务器运行正常,因为使用相同的我可以从单独的节点服务器连接到 rabbit mq.尝试在 connection.on('error')connection.on('error') 中添加记录器在 connection.on('connected') 中但没有得到任何日志

无法理解问题出在哪里.有人可以帮助或建议一个更好的图书馆吗?需要在 react-native 上集成 RabbitMQ

const 配置 = {主机:'192.0.0.1',//虚拟值端口:5672,用户名:'用户名',密码:'密码',虚拟主机:'虚拟主机'};const 连接 = 新连接(配置);console.log('连接配置改变');控制台日志(连接);connection.on('error', (event) => {console.log('错误');控制台日志(事件);});connection.on('connected', (event) => {const queue = new Queue(this.connection, {name: 'queue_name',被动:假,耐用:真实,独家:假,消费者参数:{'x 优先级':1}});const 交换 = 新交换(连接,{name: 'exchange_name',类型:'直接',耐用:真实,自动删除:假,内部:假});queue.bind(exchange, 'queue_name');//消息到达时接收一条消息queue.on('message', (data) => {console.log('收到一条消息');控制台日志(数据);});//接收一秒钟内发送的所有消息queue.on('messages', (data) => {console.log('收到多条消息');控制台日志(数据);});});

解决方案

我已经能够将 rabbitmq 连接到我的 react-native 应用程序.我设法在 Windows 10 PC 上执行此操作.

第 1 步:如果您安装了适用于 windows 的 rabbitmq,则可以开始使用,否则,请访问 https://www.rabbitmq.com/install-windows.html.访问安装文件中的 sbin 文件夹(在程序文件下),复制路径,例如( C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin )并将其添加到您的环境变量中(转到此电脑/我的电脑,右键单击并选择属性,单击高级系统设置,选择环境变量,在系统变量,突出显示路径并点击编辑,点击新建,然后粘贴复制的路径,然后点击确定.以上步骤可帮助您在 cmd 上运行 rabbitmq 命令

第 2 步:访问 Roaming 文件夹中的 Rabbitmq 文件夹,

C:\Users\PcName\AppData\Roaming\RabbitMQ

打开 enabled_plugins 文件并输入以下代码:

[rabbitmq_management].

保存并退出

打开config文件夹,在里面创建一个rabbitmq.config文件,

输入以下代码片段:

<预><代码>[{兔子, [{tcp_listeners, [{127.0.0.1", 5672},{::1",5672}]}]}].

保存并关闭文件

第 3 步:访问您的命令行(以管理员身份运行)在命令行的任何地方,运行以下命令来设置用户名,密码和管理员权限:

rabbitmqctl add_user dummy dummyrabbitmqctl set_user_tags 虚拟管理员rabbitmqctl set_permissions -p/dummy ".*";.*".*"

之后运行以启用rabbitmq管理:

rabbitmq-plugins 启用 rabbitmq_management

运行完成后,运行以下命令:

rabbitmq-server

它应该显示已完成 n 个插件(其中 n 是一个数字)

第 4 步:打开您的 App.js 文件(在您的 react-native 文件夹中)并复制以下代码:

/*** 示例 React Native 应用程序* https://github.com/facebook/react-native* @流*/import React, { Component } from 'react';进口 {平台,样式表,文本,看法来自'反应原生';进口{连接,队列,交换来自'react-native-rabbitmq';const 指令 = Platform.select({ios: '按 Cmd+R 重新加载,\n' +'Cmd+D 或摇动开发菜单',android: '在键盘上双击 R 重新加载,\n' +'摇动或按下开发菜单的菜单按钮',});类型道具 = {};导出默认类 App extends Component{构造函数(道具){超级(道具)}componentWillMount() {常量配置 = {主机:'10.0.2.2',端口:5672,用户名:'dummy',密码:'虚拟',虚拟主机:'/'};让连接 = 新连接(配置)连接.connect()让连接=假;让排队;让交换;connection.on('connected', (event) => {队列 = 新队列(连接,{name: 'queue_name',被动:假,耐用:真实,独家:假,消费者参数:{'x 优先级':1}});交换 = 新交换(连接,{name: 'exchange_name',类型:'直接',耐用:真实,自动删除:假,内部:假});queue.bind(exchange, 'queue_name');});connection.on('错误', 事件 => {连接=假;控制台日志(连接);控制台日志(事件);});}使成为() {返回 (<视图样式={styles.container}><文本样式={styles.welcome}>欢迎使用 React Native!</文本><文本样式={styles.instructions}>首先,编辑 App.js</文本><文本样式={styles.instructions}>{指示}</文本></查看>);}}const 样式 = StyleSheet.create({容器: {弹性:1,justifyContent: '中心',alignItems: '中心',背景颜色:'#F5FCFF',},欢迎: {字体大小:20,文本对齐:'居中',保证金:10,},指示: {文本对齐:'居中',颜色:'#333333',底边距:5,},});

主机 ip (10.0.2.2) 是默认的 android ip(对于那些运行 android 模拟器的用户).

连接代码必须放在 componentWillMount() 函数中,因为它是异步的.

第 5 步: 启动您的 android 模拟器并构建您的 react-native 应用程序.android 屏幕上应该没有错误.

第 6 步:通过以下链接在浏览器上访问 rabbitmq 管理:

本地主机:15672

使用您的用户名和密码登录(在这种情况下,用户名:dummy,密码:dummy),您应该在 Connections 下看到一个连接和一个队列(queue_name)在队列下

干杯.

I am trying to integrate rabbitmq on a react native app

Referring to this library : https://www.npmjs.com/package/react-native-rabbitmq

I tried to create a Connection object using the appropriate config properties. But when I tried to check the result of let connection = new Connection(config); I received following :

Connection {rabbitmqconnection: Object, callbacks: Object, connected: false} in the connection object. As you can see, it gives connected: false

I have referred to create rabbit mq server on node side : https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html to create a node rabbit mq server which works properly. Now I need to receive messages on the client side

I am sure of the config properties & rabbitmq server functioning properly because using same I can connect to rabbit mq from a separate node server. Tried to add loggers in both connection.on('error') & in connection.on('connected') but did not get any log

Unable to understand where the issue is. Can anybody help or suggest a better library? Need to integrate RabbitMQ on react-native

const config = {
        host: '192.0.0.1', //dummy values
        port: 5672,
        username: 'username',
        password: 'password',
        virtualhost: 'vhost'
    };
    const connection = new Connection(config);
    console.log('connection config changed');
    console.log(connection);
    connection.on('error', (event) => {
            console.log('error');
            console.log(event);
    });

    connection.on('connected', (event) => {
        const queue = new Queue(this.connection, {
            name: 'queue_name',
            passive: false,
            durable: true,
            exclusive: false,
            consumer_arguments: { 'x-priority': 1 }
        });

        const exchange = new Exchange(connection, {
            name: 'exchange_name',
            type: 'direct',
            durable: true,
            autoDelete: false,
            internal: false
        });

        queue.bind(exchange, 'queue_name');

        // Receive one message when it arrives
        queue.on('message', (data) => {
                console.log('Single message received');
                console.log(data);
        });

        // Receive all messages send with in a second
        queue.on('messages', (data) => {
            console.log('Multiple messages received');
            console.log(data);
        });
    });

解决方案

I have been able to connect rabbitmq to my react-native app. I managed to do this on a windows 10 PC.

Step 1: If you have rabbitmq for windows installed, you're good to go, otherwise, go to https://www.rabbitmq.com/install-windows.html . Access the sbin folder inside your installation files(under program files), copy the path, e.g. ( C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.4\sbin ) and add it to your environment variables (Go to This PC/My Computer, right click and select properties, click on Advanced system settings, select environment variables, under system variables, highlight path and click edit, click new, then paste the copied path in, and then click ok. The steps above help you to run rabbitmq commands on your cmd

Step 2: Access the Rabbitmq folder inside your Roaming folder,

C:\Users\PcName\AppData\Roaming\RabbitMQ

Open the enabled_plugins file and type in the following code:

[rabbitmq_management].

Save and exit

Open the config folder and create a rabbitmq.config file inside,

type in the following snippets of code:

[
  {rabbit, [
    {tcp_listeners, [{"127.0.0.1", 5672},
    {"::1",       5672}]}
  ]}
].

save and close the file

Step 3: Access your command line (Run as Admin) From anywhere on your command line, run the following to set username, password and admin privileges:

rabbitmqctl add_user dummy dummy
rabbitmqctl set_user_tags dummy administrator
rabbitmqctl set_permissions -p / dummy ".*" ".*" ".*" 

After that run to enable rabbitmq management:

rabbitmq-plugins enable rabbitmq_management

Once it is done running, run the following:

rabbitmq-server

it should show "completed with n plugins (where n is a number)

Step 4: Open your App.js file(in your react-native folder) and replicate the code below:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';
import { Connection,
          Queue,
          Exchange
       } from 'react-native-rabbitmq';
       
const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {

  constructor(props) {
    super(props)
    
  }

  componentWillMount() {

    const config = {
      host: '10.0.2.2',
      port: 5672,
      username: 'dummy',
      password: 'dummy',
      virtualhost: '/'
    };

    let connection = new Connection(config)
    connection.connect()

    let connected = false;
    let queue;
    let exchange;
    
    connection.on('connected', (event) => {

      queue = new Queue(connection, {
        name: 'queue_name',
        passive: false,
        durable: true,
        exclusive: false,
        consumer_arguments: { 'x-priority': 1 }
      });

      exchange = new Exchange(connection, {
        name: 'exchange_name',
        type: 'direct',
        durable: true,
        autoDelete: false,
        internal: false
      });

      queue.bind(exchange, 'queue_name');
      
    });

    

    

    connection.on('error', event => {
      
      connected = false;
      console.log(connection);
      console.log(event);
    });

  }
  
  render() {

    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit App.js
        </Text>
        <Text style={styles.instructions}>
          {instructions}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

The host ip (10.0.2.2) is the default android ip (for those running an android emulator).

The connection code had to be put within a componentWillMount() function because it is asynchronous.

Step 5: Power up your android emulator and build your react-native app. There should be no errors on the android screen.

Step 6: Access rabbitmq management on your browser via the link below:

localhost:15672

sign in with your username and password (username: dummy, password: dummy in this case), you should see a connection under Connections and a queue(queue_name) under Queues

Cheers.

这篇关于无法在本机应用程序中连接到rabbitmq 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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