4.2. 接收信息
此客户端程序使用 < connection-url
> 连接到服务器,为源 <address& gt;
创建一个接收器,并接收信息直到终止或到达 < 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