-
Notifications
You must be signed in to change notification settings - Fork 3
/
WS.ts
167 lines (148 loc) · 4.33 KB
/
WS.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import packNumber from './packNumber'
import connectLib from './connectLib'
import { add_event, sett } from './utils'
import { enrichConfig } from './config'
import './types'
import { T } from 'ramda'
const MAX_32 = 2**31 - 1
/* .send(your_data) wraps request to server with {id: `hash`, data: `actually your data`},
returns a Promise, that will be rejected after a timeout or
resolved if server returns the same signature: {id: `same_hash`, data: `response data`}
*/
class WebSocketClient {
private open = null
private ws = null
// in use by side functions.
private forcibly_closed = false
private reconnect_timeout: NodeJS.Timer = null
private queue = {}
private messages = []
private onReadyQueue = []
private onCloseQueue = []
private handlers = <{[event: string]: ((e: any) => void)[]}>{
open: [], message: [], close: [], error: []
}
private config = <wsc.Config>{}
private init_flush(): void {
this.queue = {} // data queuse
this.messages = [] // send() queue
}
private log(event: string, message: any = null, time: number = null): void {
const config = this.config
if(time !== null) {
config.log(event, time, message)
} else {
if(config.timer) {
config.log(event, null, message)
} else {
config.log(event, message)
}
}
}
private async connect() { // returns status if won't open or null if ok.
return new Promise((ff) => {
connectLib.call(this, ff)
})
}
public get socket() {
return this.ws
}
public async ready() {
return new Promise<void>((ff) => {
if(this.open) {
ff()
} else {
this.onReadyQueue.push(ff)
}
})
}
public on(
event_name: wsc.WSEvent,
handler: (data: any) => any,
predicate: (data: any) => boolean = T,
raw = false
) {
const _handler: wsc.EventHandler = (event) =>
predicate(event) && handler(event)
return raw
? add_event(this.ws, event_name, _handler)
: this.handlers[event_name].push(_handler)
}
public async close(): wsc.AsyncErrCode {
return new Promise((ff, rj) => {
if(this.ws === null) {
rj('WSP: closing a non-inited socket!')
} else {
this.open = null
this.onCloseQueue.push(() => {
this.init_flush()
this.ws = null
this.forcibly_closed = true
ff(null)
})
this.ws.close()
}
})
}
public async send<RequestDataType = any, ResponseDataType = any>(
message_data: RequestDataType,
opts = <wsc.SendOptions>{}
): Promise<ResponseDataType> {
this.log('send', message_data)
const config = this.config
const message = {}
const data_key = config.server.data_key
const first_time_lazy = config.lazy && !this.open
const message_id = packNumber((Math.random()*(MAX_32-10))|0)
if(typeof opts.top === 'object') {
if(opts.top[data_key]) {
throw new Error('Attempting to set data key/token via send() options!')
}
Object.assign(message, opts.top)
}
config.pipes.forEach(
(pipe) => message_data = pipe(message_data)
)
if(this.open === true) {
this.ws.send(config.encode(message_id, message_data, config))
} else if(this.open === false || first_time_lazy) {
this.messages.push({
send: () => this.ws.send(config.encode(message_id, message_data, config))
})
if(first_time_lazy) {
this.connect()
}
} else if(this.open === null) {
throw new Error('Attempting to send via closed WebSocket connection!')
}
return new Promise((ff, rj) => {
this.queue[message_id] = {
ff,
data_type: config.data_type,
sent_time: config.timer ? Date.now() : null,
timeout: sett(config.timeout, () => {
if(this.queue[message_id]) {
rj({
'Websocket timeout expired: ': config.timeout,
'for the message': message
})
delete this.queue[message_id]
}
})
}
})
}
constructor(user_config: wsc.UserConfig = {}) {
this.config = enrichConfig(user_config)
// Init.
this.init_flush()
// Flags.
this.open = false
this.reconnect_timeout = null
this.forcibly_closed = false
if(!this.config.lazy) {
this.connect()
}
}
}
export default WebSocketClient