gpt4 book ai didi

node.js - 使用 Node.js 的 AMQP,如何发布/订阅?

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:11 25 4
gpt4 key购买 nike

我正在创建一个操作 Node-AMQP 模块的类:https://github.com/postwait/node-amqp

但我无法使用此发布/订阅:

  var Queue = require('./Queue.js');
var queue = new Queue();
queue.addTaskToQueue('salut', 5);
queue.subscribeTaskQueue('salut');

这是我正在使用的类(我为那些不了解 CoffeeScript 的人提供了 CoffeeScript 中的代码,以及 Node.js 中的代码):

感谢您的帮助。

在 CoffeeScript 中:

amqp = require('amqp')

class Queue

constructor: (ip = 'localhost') ->
@ip = ip
@receivedObject
@connection = amqp.createConnection({ host: @ip })
@queue

subscribeTaskQueue: (queueToSubscribe) ->
self = @
self.connection.on('ready', ->
q = self.connection.queue(queueToSubscribe)
q.bind('#')

q.subscribe({ ack: true }, (message) ->
self.receivedObject = message
console.log(self.receivedObject)
)
)

addTaskToQueue: (queue, objectToSend) ->
@queue = @connection.queue("salut", { durable: true })
@connection.publish(queue, objectToSend)

module.exports = Queue

在 Node.js 中

(function() {
var Queue, amqp;
amqp = require('amqp');
Queue = (function() {
function Queue(ip) {
if (ip == null) {
ip = 'localhost';
}
this.ip = ip;
this.receivedObject;
this.connection = amqp.createConnection({
host: this.ip
});
this.queue;
}
Queue.prototype.subscribeTaskQueue = function(queueToSubscribe) {
var self;
self = this;
return self.connection.on('ready', function() {
var q;
q = self.connection.queue(queueToSubscribe);
q.bind('#');
return q.subscribe({
ack: true
}, function(message) {
self.receivedObject = message;
return console.log(self.receivedObject);
});
});
};
Queue.prototype.addTaskToQueue = function(queue, objectToSend) {
this.queue = this.connection.queue("salut", {
durable: true
});
return this.connection.publish(queue, objectToSend);
};
return Queue;
})();
module.exports = Queue;
}).call(this);
<小时/>

我按照你说的做了+我做了一些修改。

这是我的类 Queue.coffee :

amqp = require('amqp')

class Queue

constructor: (ip = "localhost", queueName = "salut") ->
@ip = ip
@receivedObject = "test"
@connection = amqp.createConnection({ host: 'localhost' })
@queueName = queueName

subscribeTaskQueue: () ->
@connection.on('ready', ->
q = @connection.queue(@queueName)
q.bind('#')

q.subscribe({ ack: true }, (message) ->
@receivedObject = message
console.log(@receivedObject)
)
)

addTaskToQueue: (objectToSend = "hello") ->
@connection.publish(@queueName, objectToSend)


queue = new Queue("localhost", "salut")

queue.connection.on 'ready', ->
queue.addTaskToQueue 'salut', 5
queue.subscribeTaskQueue 'salut'

这是rabbituser.coffee:

Queue = require('./Queue.js')
queue = new Queue("localhost", "salut")

queue.addTaskToQueue("hello")
queue.subscribeTaskQueue()

当我执行命令时:noderabbituser.js

我明白了:

node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property '' of undefined
at Connection.exchange (/home/armand/node_modules/amqp/amqp.js:1242:21)
at Connection.publish (/home/armand/node_modules/amqp/amqp.js:1258:60)
at Queue.addTaskToQueue (/home/armand/Desktop/RockSolidProject/coucheAMQP/Queue.js:36:30)
at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:5:9)
at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:7:4)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array.<anonymous> (module.js:421:10)

最佳答案

我不确定您遇到的错误是什么,但我会指出一些事情:

  1. 在构造函数中,当您编写 @receivedObject 时和@queue ——那没有任何作用。在 JavaScript 中,每个对象都是一个哈希,您可以随时附加属性;如果@receivedObject@queue最初在 Queue 中未定义实例,那么您不必在构造函数中定义它们。

  2. 问题是否有可能是您正在调用 @connection.queueaddTaskToQueue在连接存在之前(即 self.connection.on 'ready' 回调之前)?

也许如果您将代码更改为

queue = new Queue()
queue.connection.on 'ready', ->
queue.addTaskToQueue 'salut', 5
queue.subscribeTaskQueue 'salut'

如果这不能解决您的问题,能否请您准确描述您遇到的错误以及遇到该错误的位置?

关于node.js - 使用 Node.js 的 AMQP,如何发布/订阅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com