-
Notifications
You must be signed in to change notification settings - Fork 0
/
method.js
208 lines (205 loc) · 4.3 KB
/
method.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const WebSocket = require('ws')
const fs = require('fs')
class WS {
ip = ""
port = ""
path = ""
wss = false
/** @type {WebSocket} */
ws
get url() {
return `${this.wss ? "wss://" : "ws://"}${this.ip}${stringIsnullOrEmpty(this.port) ? "" : ":" + this.port}${stringIsnullOrEmpty(this.path) ? "" : this.path}`
}
#delegats = new Delegates()
#retry = 0
realclose = true
#hbData = ""
#hbTime = 30000
/** @type {NodeJS.Timer|null} */
#hbInterval = null
constructor(ip = "", port = "", path = "", wss = false) {
this.ip = ip
this.port = port
this.path = path
this.wss = wss
}
async connect(callback) {
this.ws = new WebSocket(this.url)
this.ws.onmessage = e => {
this.#delegats.run(e.data.toString())
}
this.ws.onclose = async e => {
if (!this.realclose) {
clearInterval(this.#hbInterval)
this.#hbInterval = null
if (this.#retry >= 5) await RList.Push()
this.#retry++
this.connect()
}
}
this.ws.onopen = e => {
this.#retry = 0
if (this.#hbData !== "") this.#hbInterval = setInterval(() => {
this.ws.send(this.#hbData)
}, this.#hbTime)
return callback ? callback() : Promise.resolve()
}
}
send(data) {
this.ws.send(data)
}
message(callback) {
return this.#delegats.push(callback)
}
close() {
this.realclose = true
this.ws.close()
}
setHeartbeat({ data = "", time = 0 }) {
if (typeof data == "object") data = JSON.stringify(data)
this.#hbTime = time
this.#hbData = data
}
}
// // 建立连接对象
// let socket = new WS("127.0.0.1", "8080")
// // 写入收到消息的方法
// socket.message(data => {
// console.log(data)
// })
// // 开始连接
// socket.connect()
// // 发送消息
// socket.send("Hello")
/**
* 生成随机 Uuid
* @returns {string}
*/
const getUuid = (lowercase = false) => {
let s = []
let hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for (let i = 0; i < 36; i++) {
const random = Math.floor(Math.random() * 0x10)
s[i] = hexDigits.substring(random, random + 1)
}
s[14] = "4"
s[19] = hexDigits.substring((s[19] & 0x3) | 0x8, (s[19] & 0x3) | 0x8 + 1)
s[8] = s[13] = s[18] = s[23] = "-"
let uuid = s.join("")
if (lowercase) {
uuid = uuid.toLowerCase()
}
return uuid
}
class Delegates {
/**
* @type {{
* id: string,
* e: Function
* }[]}
*/
list
item = class {
/** @type {string} */
id
/** @type {Function} */
e
/**
*
* @param {Function} e
*/
constructor(e) {
this.id = getUuid()
this.e = e
}
}
constructor() {
this.list = new Array()
}
run(...t) {
if (this.list.length != 0) for (let i = 0; i < this.list.length; i++) {
this.list[i].e(...arguments)
}
}
/**
*
* @param {Function} e
* @returns
*/
push(e) {
let z = new this.item(e)
this.list.push(z)
return z.id
}
/**
*
* @param {string|number} e
* @returns
*/
remove(e) {
i = this.check(e)
if (typeof i === "number") {
this.list.splice(i, 1)
return true
} else {
return false
}
}
/**
*
* @param {string|number} e
* @returns
*/
check(e) {
let i
if (typeof e == "number") {
i = e
} else {
let z = -1
for (let a = 0; a < this.list.length; a++) {
const q = this.list[a]
if (q.id != e) continue
z = i = a
break
}
if (z == -1) {
return false
}
}
return i
}
}
const RList = new class {
time = 3000
#list = -1
snooze = ms => new Promise(resolve => setTimeout(resolve, ms))
async Push() {
this.#list++
await this.snooze(this.#list * this.time)
Promise.resolve().finally(() => {
setTimeout(() => { this.#list-- }, (this.#list + 1) * this.time)
})
}
}
const stringIsnullOrEmpty = (str) => {
if (str == null || str == undefined || str == "") return true
return false
}
/**
*
* @param {string} path
* @returns {string}
*/
const ReadFile = (path) => {
// 判断文件是否存在
if (!fs.existsSync(path)) {
return ""
}
// 读取文件
const main = fs.readFileSync(path, "utf-8")
return main
}
exports.getUuid = getUuid
exports.WS = WS
exports.stringIsnullOrEmpty = stringIsnullOrEmpty
exports.ReadFile = ReadFile