-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathRabbit.ts
More file actions
60 lines (52 loc) · 1.08 KB
/
Rabbit.ts
File metadata and controls
60 lines (52 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as amqp from 'amqplib'
/**
*
*
* @export
* @class Rabbit
*/
export class Rabbit {
private connect
private url: string
/**
* Creates an instance of Rabbit.
* @param {string} [url]
* @memberof Rabbit
*/
constructor (url?: string) {
url = url || 'amqp://Treasure:Sunshine@localhost:5672'
this.url = url
this.connect = this.initConnect(url)
}
/**
* @method getConnection
* @returns {Promise<amqp.Connection>}
* @memberof Rabbit
*/
async getConn (): Promise<amqp.Connection> {
try {
const conn = await (this.connect as Promise<amqp.Connection>)
return conn
} catch (error) {
console.log(error)
}
}
/**
* @method getChannel
* @returns {Promise<amqp.Channel>}
* @memberof Rabbit
*/
async getChannel (): Promise<amqp.Channel> {
const conn = await this.getConn()
const ch = await conn.createChannel()
return ch
}
private initConnect (url): any {
return amqp.connect(url)
}
}
export const rabbit = new Rabbit()
interface IOption {
url: string
[key: string]: any
}