4.2. 接收消息
这个客户端程序使用 <connection-url>
连接到服务器,为源 <address>
创建一个接收器,并在其终止或到达 <count>
信息前接收信息。
示例:接收消息
"use strict"; var rhea = require("rhea"); var url = require("url"); if (process.argv.length !== 4 && process.argv.length !== 5) { console.error("Usage: receive.js <connection-url> <address> [<message-count>]"); process.exit(1); } var conn_url = url.parse(process.argv[2]); var address = process.argv[3]; var desired = 0; var received = 0; if (process.argv.length === 5) { desired = parseInt(process.argv[4]); } var container = rhea.create_container(); container.on("receiver_open", function (event) { console.log("RECEIVE: Opened receiver for source address '" + event.receiver.source.address + "'"); }); container.on("message", function (event) { var message = event.message; console.log("RECEIVE: Received message '" + message.body + "'"); received++; if (received == desired) { event.receiver.close(); event.connection.close(); } }); var opts = { host: conn_url.hostname, port: conn_url.port || 5672, // To connect with a user and password: // username: "<username>", // password: "<password>", }; var conn = container.connect(opts); conn.open_receiver(address);
运行示例
要运行示例程序,将其复制到本地文件中并使用 python
命令调用它。如需更多信息,请参阅 第 3 章 入门。
$ node receive.js amqp://localhost queue1