-slack: interval management

This commit is contained in:
Flummi 2020-03-01 13:00:20 +01:00
parent e13a494ea2
commit a382d0fda7
2 changed files with 14 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "cuffeo", "name": "cuffeo",
"version": "1.0.5-3", "version": "1.0.6",
"description": "A multi-protocol chatbot library with nearly zero dependencies.", "description": "A multi-protocol chatbot library with nearly zero dependencies.",
"main": "src/index.mjs", "main": "src/index.mjs",
"scripts": {}, "scripts": {},
@ -17,7 +17,7 @@
"author": "Flummi & jkhsjdhjs", "author": "Flummi & jkhsjdhjs",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"flumm-fetch-cookies": "^1.3.4" "flumm-fetch-cookies": "^1.3.5"
}, },
"bugs": { "bugs": {
"url": "https://github.com/kein-Bot/cuffeo/issues" "url": "https://github.com/kein-Bot/cuffeo/issues"

View File

@ -12,6 +12,7 @@ export default class slack extends EventEmitter {
this.network = "Slack"; this.network = "Slack";
this.api = "https://slack.com/api"; this.api = "https://slack.com/api";
this.socket = null; this.socket = null;
this.interval = null;
this.server = { this.server = {
set: this.set, set: this.set,
channel: new Map(), channel: new Map(),
@ -56,7 +57,7 @@ export default class slack extends EventEmitter {
this.server.wss.socket = sock; this.server.wss.socket = sock;
this.server.wss.socket.setDefaultEncoding("utf-8"); this.server.wss.socket.setDefaultEncoding("utf-8");
setInterval(async () => await this.ping(), 3e4); // 30 seconds lul this.interval = setInterval(async () => await this.ping(), 3e4); // 30 seconds lul
this.server.wss.socket.on("data", async data => { this.server.wss.socket.on("data", async data => {
try { try {
@ -104,6 +105,7 @@ export default class slack extends EventEmitter {
async reconnect() { async reconnect() {
this.server.wss.url = null; this.server.wss.url = null;
this.server.wss.socket = null; this.server.wss.socket = null;
clearTimeout(this.interval);
this.emit("data", [ "info", "reconnecting slack" ]); this.emit("data", [ "info", "reconnecting slack" ]);
return await this.connect(); return await this.connect();
} }
@ -139,7 +141,7 @@ export default class slack extends EventEmitter {
} }
async ping() { async ping() {
await this.write({ return await this.write({
type: "ping" type: "ping"
}); });
} }
@ -171,10 +173,14 @@ export default class slack extends EventEmitter {
if (!this.server.wss.socket) if (!this.server.wss.socket)
await this.reconnect(); await this.reconnect();
this.server.wss.socket.cork(); try {
this.server.wss.socket.write(frame); this.server.wss.socket.cork();
this.server.wss.socket.write(Buffer.from(msg)); this.server.wss.socket.write(frame);
this.server.wss.socket.uncork(); this.server.wss.socket.write(Buffer.from(msg));
this.server.wss.socket.uncork();
} catch(err) {
console.error(err);
}
} }
reply(tmp) { reply(tmp) {