-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoeSocket.js
More file actions
57 lines (53 loc) · 1.73 KB
/
poeSocket.js
File metadata and controls
57 lines (53 loc) · 1.73 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
import WebSocket from 'ws';
import { config } from "./config.js";
export class PoeSocket {
setting;
ws;
messageHandler;
constructor(
setting,
messageHandler,
) {
this.setting = JSON.parse(JSON.stringify(setting));
this.messageHandler = messageHandler;
this.connect();
}
getUrl(setting) {
const url = new URL(`wss://tch${Math.floor(1e6 * Math.random()) + 1}.tch.${setting.tchannelData.baseHost}`);
url.pathname = `/up/${setting.tchannelData.boxName}/updates`;
url.searchParams.set('min_seq', setting.tchannelData.minSeq);
url.searchParams.set('channel', setting.tchannelData.channel);
url.searchParams.set('hash', setting.tchannelData.channelHash);
return url.toString();
}
connect() {
let url = this.getUrl(this.setting)
this.ws = new WebSocket(url, { agent: config.proxyAgent })
this.ws.on('open', this.onOpen.bind(this));
this.ws.on('message', this.onMessage.bind(this));
this.ws.on('close', this.onClose.bind(this));
this.ws.on('error', this.onError.bind(this));
}
close() {
this.ws.close();
}
onOpen() {}
onMessage(data) {
if (data.min_seq) {
this.setting.tchannelData.minSeq = data.min_seq
}
if (data.messages) {
for (const ms of data.messages) {
const ms_obj = JSON.parse(ms)
if (this.messageHandler && ms_obj.payload.data.messageAdded) {
this.messageHandler(ms_obj.payload.data.messageAdded)
}
}
}
}
onClose() {}
onError(e) {
console.log('websocket error, please try again');
process.exit();
}
}