-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.ts
84 lines (76 loc) · 1.53 KB
/
client.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import net from 'net';
/**
* This class stores the information regarding a connected Client.
*
* @export
* @class Client
*/
export class Client {
/**
* This key is used to uniquely identify the client inside the server.
*
* @type {string}
*/
key: string;
/**
* The socket instance corresponding to the connection.
*
* @type {net.Socket}
*/
socket: net.Socket;
private options: ClientOptions;
constructor(
key: string,
socket: net.Socket,
options: ClientOptions = DEFAULT_OPTIONS
) {
this.key = key;
this.socket = socket;
this.options = options;
}
updateOptions(data: Partial<ClientOptions>) {
this.options = { ...this.options, ...data };
}
sendPong() {
this.socket.write('PONG\r\n');
}
/**
* Send an OK message if the client option has verbose set to true.
*/
sendOk() {
if (this.options.verbose) {
this.socket.write('+OK\r\n');
}
}
}
/**
* Refer to https://docs.nats.io/reference/reference-protocols/nats-protocol#connect
*
* @export
* @interface ClientOptions
*/
export interface ClientOptions {
verbose: boolean;
pedantic: boolean;
tls_required: boolean;
auth_token?: string;
user?: string;
pass?: string;
name?: string;
lang: string;
version: string;
protocol?: number;
echo?: boolean;
sig?: string;
jwt?: string;
no_responders?: boolean;
headers?: boolean;
nkey?: string;
}
export const DEFAULT_OPTIONS: ClientOptions = {
verbose: true,
pedantic: true,
tls_required: false,
lang: '',
version: ''
};