无法连接到react-native应用程序中的rabbitmq服务器

时间:2017-11-09 12:24:47

标签: react-native 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来创建一个正常工作的节点rabbit mq服务器。现在我需要在客户端接收消息

我确信配置属性& rabbitmq服务器正常运行,因为使用相同的我可以从一个单独的节点服务器连接到兔mq。试图在connection.on('error')和{}中添加记录器在connection.on('connected')但没有得到任何记录

无法理解问题所在。任何人都可以帮助或建议更好的图书馆吗?需要将RabbitMQ集成在本地反应

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);
        });
    });

1 个答案:

答案 0 :(得分:3)

我已经能够将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) 并将其添加到您的环境变量中 (转到此PC /我的电脑,右键单击并选择属性,然后单击 高级系统设置,选择环境变量,在 系统变量,突出显示路径,然后点击修改, 点击,然后将复制的路径粘贴到其中,然后点击确定。 上述步骤可帮助您在cmd上运行rabbitmq命令

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

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

打开 enabled_plugins 文件,然后输入以下代码:

[rabbitmq_management].

保存并退出

打开 config 文件夹并在其中创建 rabbitmq.config 文件,

输入以下代码片段:

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

保存并关闭文件

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

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

在运行之后启用rabbitmq管理:

rabbitmq-plugins enable rabbitmq_management

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

rabbitmq-server

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

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



/**
 * 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,
  },
});
&#13;
&#13;
&#13;

主机ip(10.0.2.2)是默认的android ip(对于那些运行android模拟器的人来说)。

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

第5步:启动Android模拟器并构建您的react-native应用。 在Android屏幕上应该没有错误。

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

localhost:15672

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

干杯。