-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
30 lines (26 loc) · 869 Bytes
/
Copy pathutil.ts
File metadata and controls
30 lines (26 loc) · 869 Bytes
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
/// <reference path="./@types/global.d.ts" />
import { WebSocket } from "ws";
// define the function sendAsync which is added onto the WebSocket object
Object.defineProperty(WebSocket.prototype, "sendAsync", {
async value(this: Socket.SocketClient, d: any) {
// dont send if socket is closed
if (this.readyState !== WebSocket.OPEN) return;
console.log(
`[$wss] [Server>>Client] Sent OP Code >${d.op}< to ${this.address}`
);
return new Promise<void>((a, b) =>
// pack the data into a string and update the sequence
this.send(
typeof d !== "string"
? JSON.stringify({
...d,
s: d.s ?? ++this.props.sequence,
// resolve or reject the promise based on the result
})
: d,
(e) => (!e ? a() : b(e))
)
);
},
});
export default null;