本文整理匯總了TypeScript中amqplib.connect函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript connect函數的具體用法?TypeScript connect怎麽用?TypeScript connect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了connect函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: to
to(exchange: string, iO: Observable<any>): Subscription<any> {
let channel, conn, cachedItems = [];
let queue = uuid.v4();
connect(this.config.connString)
.then(c => conn = c && c.createChannel())
.then(c => {
channel = c;
channel.assertExchange(exchange, 'fanout');
cachedItems.forEach(item => {
channel.publish(exchange, '', new Buffer(item));
});
})
.catch(err => { throw err; });
return iO
.subscribe(
msg => {
if (!channel) {
return cachedItems.push(msg);
}
channel.publish(exchange, '', new Buffer(msg));
},
err => console.error(`rabbitSink err, queue: ${queue}`, err),
() => {
if (conn) {
channel.publish(exchange, '', new Buffer('__done'));
conn.close()
}
}
);
}
示例2: connect
public connect(
actionFunction: (message: any, responseCallback: Function) => void
): Bluebird.Thenable<Channel> {
if (!actionFunction) {
throw new Error('Cannot start RPC_Server without an actionFunction');
}
const self: RPC_Server = this;
return connect(this.uri)
.then((connection: Connection) => {
self._connection = connection;
return connection.createChannel();
})
.then((channel: Channel) => {
self._channel = channel;
// Assert Queue
channel.assertQueue(
self.queueName,
{ durable: false }
);
channel.prefetch(1);
if (self.verboseOn) {
console.log(`Awaiting RPC Requests On: ${self.queueName}`);
}
// Consume Messages
self.consumeChannel.apply(self, [channel, actionFunction]);
return self._channel;
});
}
示例3: connect
public connect(): Observable<Channel> {
if (!this._uri) {
return Observable.throw(
new Error('Uri must be defined in order to start connection')
);
}
const self: RPC_Client = this;
// Connect to RabbitMQ
const connPromise: any = connect(this.uri)
// Assign Connection and Create Channel
.then((connection: Connection) => {
self._connection = connection;
return connection.createChannel();
})
// Assign Channel and create consume listener
.then((channel: Channel) => {
self._channel = channel;
channel.setMaxListeners(0);
// Action to take when message received
self.consumeChannel.apply(self, [channel]);
return channel;
});
return Observable.fromPromise(connPromise);
}
示例4: getChannel
function getChannel(queue: string) {
return amqplib.connect('amqp://localhost')
.then(conn => {
return conn.createChannel();
})
.then(channel => {
channel.assertQueue(queue, {durable: false});
return channel;
});
}
示例5: sendSingleMessage
protected async sendSingleMessage(messageObj, callback: (err, result, disposed?: boolean) => void) {
const server = await amqp.connect(this.host);
const channel = await server.createChannel();
const { sub, pub } = this.getQueues();
channel.assertQueue(sub, { durable: false });
channel.assertQueue(pub, { durable: false });
channel.consume(pub, (message) => this.handleMessage(message, server, callback), { noAck: true });
channel.sendToQueue(sub, Buffer.from(JSON.stringify(messageObj)));
}
示例6: connect
function connect () {
amqp.connect('amqp://localhost')
.then((conn)=> conn.createChannel())
.then((ch)=> {channel = ch; return ch.assertExchange('auth', 'direct')})
.then(()=>channel.assertQueue('token.auth', {durable: false}))
.then((q)=>{tokenQueue = q; return channel.bindQueue('token.auth', 'auth', 'token.auth')})
.then(()=>channel.assertQueue('login.auth', {durable: false}))
.then((q)=>{loginQueue = q; return channel.bindQueue('login.auth', 'auth', 'login.auth')})
.then(()=>{
channel.consume('login.auth', (msg: auth.loginMessage)=>{
var content = JSON.parse(msg.content.toString());
channel.ack(msg);
if (content.login === 'a') {
var token = jwt.sign({
iss: 'a'
}, 'shhhhhhared-secret');
channel.publish('auth', 'token.auth', new Buffer(JSON.stringify({
token: token,
login: content.login
})));
} else {
channel.publish('auth', 'token.auth', new Buffer(JSON.stringify({
ERROR: 'Incorrect',
code: 401,
login: content.login
})));
}
});
})
.catch((e)=>{
console.error(e);
});
console.log('ok');
};
示例7: Observable
return new Observable(o => {
let conn;
connect(this.config.connString)
.then(c => conn = c && c.createChannel())
.then(ch => {
ch.assertExchange(exchange, 'fanout', {durable: true});
ch.assertQueue(queue, {durable: true});
ch.bindQueue(queue, exchange, '');
ch.consume(queue, msg => {
let msgStr = msg.content.toString();
ch.ack(msg);
if (msgStr === '__done') {
return o.complete();
}
o.next(msgStr);
}, {noAck: false});
})
.catch(err => o.error(err));
return () => conn && conn.close();
});
示例8:
.flatMap(() => Rx.Observable.fromPromise(AmqpLib.connect(url, options)))
示例9:
.defer(() => AmqpLib.connect(url, options))
示例10:
const makeAmqp = config => {
const amqp = amqplib
.connect(config.MN_AMQP_CONNECTION)
.then(conn => {
process.once('SIGINT', () => {
conn.close()
})
return conn.createChannel()
})
.then(channel => {
channel.assertQueue(q, { durable: false })
// Note: on Node 6 Buffer.from(msg) should be used
channel.sendToQueue(q, Buffer.from('Hello World!'))
// tslint:disable-next-line:no-console
console.log("[x] Sent 'Hello World!'")
return channel
})
.then(channel =>
channel.consume(
q,
msg => {
// tslint:disable-next-line:no-console
console.log('[x] Received %s', msg ? msg.content : msg)
},
{ noAck: true }
)
)
// .then(channel => {
// Promise.all(
// Object.entries(exchanges).reduce(
// (acc, [exchange, types]) =>
// acc.concat(
// types.map(type =>
// channel.assertExchange(exchange, type, { durable: false })
// )
// ),
// []
// )
// ).then(() => channel);
// })
// .then(channel => {
// })
// .then(channel => {
// Promise.all(
// Object.entries(exchanges).reduce(
// (acc, [exchange, types]) =>
// acc.concat(
// types.map(type =>
// channel.assertExchange(exchange, type, { durable: false })
// )
// ),
// []
// )
// ).then(() => channel);
// })
// .then(channel => {
// })
.catch(console.error)
}